簡體   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