Shiny DT datatable Make only certain columns editable by the user
Asked Answered
A

2

7

A simple app:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("blah"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(),

        # Show a plot of the generated distribution
        mainPanel(
            DT::DTOutput('ex_df')
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
    
    output$ex_df <- DT::renderDT(data.frame(
        x = 1:10,
        y = 1,
        z = 11:20
    ), 
    
    selection = 'none', editable = 'cell', server = TRUE, rownames = FALSE,
    list(target = 'cell', disable = list(columns = c(1,2)))
    )
}

# Run the application 
shinyApp(ui = ui, server = server)

With this line: disable = list(columns = c(1,2) I intended to make the 1st and second column un editable. However, all columns seem to be editable: enter image description here

How can I set it so that only the 3rd column is editable?

Abagael answered 15/9, 2020 at 16:18 Comment(0)
F
12

According to the documentation for DT::datatable(), the editable argument should be structured differently:

This argument can also be a list of the form list(target = TARGET, disable = list(columns = INDICES)), where TARGET can be cell, row, column, or all, and INDICES is an integer vector of column indices.

Try editable = list(target = "column", disable = list(columns = c(1,2))

Fears answered 15/9, 2020 at 16:31 Comment(1)
Thanks I tried that, yet all the columns are still showing as editableAbagael
S
4

You need to use 0 based indices to indicate the disabled columns. Try using editable = list(target = "column", disable = list(columns = c(0, 1))

Steelyard answered 30/1, 2022 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.