[英]How to specify file name and restrict column editing in editable datatable in shiny
I have a example shiny app here.我在这里有一个示例 shiny 应用程序。 It displays editable datatable using
DT
package.它使用
DT
package 显示可编辑的数据表。
To enable downloading all data shown on multiple pages, I use server=FALSE
together with renderDT
.为了能够下载显示在多个页面上的所有数据,我将
server=FALSE
与renderDT
一起使用。
What I want to achieve now is我现在想要实现的是
restrict user to edit some specific columns.限制用户编辑某些特定列。 The following code does not seem to work.
以下代码似乎不起作用。
editable = list(target = 'cell', disable = list(column = c("Sepal.Length", "Sepal.Width")))
I want to specify a default file name when exporting to csv, something like data.csv
.我想在导出到 csv 时指定一个默认文件名,例如
data.csv
。 Is that possible?那可能吗?
Super appreciate it if someone can help me out on that.如果有人可以帮助我,非常感谢。 Thanks a lot.
非常感谢。
library(shiny)
library(DT)
library(dplyr)
# UI
ui = fluidPage(
selectInput("nrows",
"select n entries",
choices = 100:150,
selected = 100,
multiple = FALSE),
DT::dataTableOutput('tbl'),
checkboxGroupInput('datacols',
label='Select Columns:',
choices= c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Specie'),
selected = c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Specie'),
inline=TRUE )
)
# SERVER
server = function(input, output) {
df = reactiveValues()
observe ({
df$dat = iris %>% .[1:input$nrows, ]
})
# render DT
output$tbl = renderDT(server=FALSE, {
datatable(df$dat %>% select(one_of(input$datacols)),
editable = list(target = 'cell', disable = list(column = c("Sepal.Length", "Sepal.Width"))), #"cell",
extensions = "Buttons",
options = list(
dom = "Bfrtip", buttons = list("csv")))
})
observeEvent(input[["tbl_cell_edit"]], {
cellinfo <- input[["tbl_cell_edit"]]
df$dat <- editData(df$dat, input[["tbl_cell_edit"]])
})
}
shinyApp(ui=ui, server = server)
To disable some columns for editing, you have to give the column indices, not the column names.要禁用某些列进行编辑,您必须提供列索引,而不是列名。 Moreover the key word is
columns
, not column
:此外,关键字是
columns
,而不是column
:
editable = list(target = 'cell', disable = list(columns = c(1,2)))
To specify the file name, do:要指定文件名,请执行以下操作:
buttons = list(
list(extend = "csv", text = "Export to CSV", filename = "iris")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.