zesla
zesla

Reputation: 11793

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

I have a example shiny app here. It displays editable datatable using DT package.

To enable downloading all data shown on multiple pages, I use server=FALSE together with renderDT.

What I want to achieve now is

  1. 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")))

  2. I want to specify a default file name when exporting to csv, something like 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)

Upvotes: 1

Views: 2099

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

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:

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")
        )

Upvotes: 5

Related Questions