【问题标题】:How to specify file name and restrict column editing in editable datatable in shiny如何在闪亮的可编辑数据表中指定文件名并限制列编辑
【发布时间】:2020-02-01 02:48:25
【问题描述】:

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

为了能够下载显示在多个页面上的所有数据,我将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)

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 谢谢!但是,我希望用户只编辑特定的列。我的数据表列正在更改,具体取决于用户希望通过表下方的选中/取消选中框显示的列。简单的列索引似乎不起作用。有什么方法可以让“Sepal Width”列可编辑?
    • @zesla 执行 dat &lt;- df$dat %&gt;% select(one_of(input$datacols)) 并获取列的索引,执行 which(names(dat) != "Sepal.Width")
    猜你喜欢
    • 2020-12-27
    • 2019-10-25
    • 2023-03-19
    • 2019-08-01
    • 2019-09-25
    • 1970-01-01
    • 2015-05-22
    • 2020-12-24
    • 1970-01-01
    相关资源
    最近更新 更多