繁体   English   中英

如何在 shiny 的可编辑数据表中指定文件名并限制列编辑

[英]How to specify file name and restrict column editing in editable datatable in shiny

我在这里有一个示例 shiny 应用程序。 它使用DT package 显示可编辑的数据表。

为了能够下载显示在多个页面上的所有数据,我将server=FALSErenderDT一起使用。

我现在想要实现的是

  1. 限制用户编辑某些特定列。 以下代码似乎不起作用。

    editable = list(target = 'cell', disable = list(column = c("Sepal.Length", "Sepal.Width")))

  2. 我想在导出到 csv 时指定一个默认文件名,例如data.csv 那可能吗?

如果有人可以帮助我,非常感谢。 非常感谢。

    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)

要禁用某些列进行编辑,您必须提供列索引,而不是列名。 此外,关键字是columns ,而不是column

editable = list(target = 'cell', disable = list(columns = c(1,2)))

要指定文件名,请执行以下操作:

        buttons = list(
          list(extend = "csv", text = "Export to CSV", filename = "iris")
        )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM