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

时间:2020-09-15 16:18:17

标签: r shiny dt

一个简单的应用程序:

#
# 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)我打算使第一和第二列不可编辑。但是,所有列似乎都是可编辑的: enter image description here

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

1 个答案:

答案 0 :(得分:3)

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

此参数也可以是列表形式的列表(target = TARGET, disable = list(columns = INDICES)),其中TARGET可以是单元格,行, 列或全部列,而INDICES是列索引的整数向量。

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