闪亮的 DT 数据表仅使某些列可由用户编辑

Shiny DT datatable Make only certain columns editable by the user

一个简单的应用程序:

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

这一行:disable = list(columns = c(1,2) 我打算让第一列和第二列不可编辑。但是,所有列似乎都是可编辑的:

我如何设置它以便只有第 3 列是可编辑的?

根据 DT::datatable() 的文档,editable 参数的结构应该不同:

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.

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

您需要使用基于 0 的索引来指示禁用的列。尝试使用 editable = list(target = "column", disable = list(columns = c(0, 1))