在 rhandsontable 中禁用列编辑?

Posted

技术标签:

【中文标题】在 rhandsontable 中禁用列编辑?【英文标题】:Disable column editing in rhandsontable? 【发布时间】:2016-09-23 00:34:03 【问题描述】:

假设我有以下闪亮的应用程序:

library(shiny)
library(rhandsontable)

ui <- shinyUI(fluidPage(
  titlePanel("Handsontable"),
  sidebarLayout(
    sidebarPanel(
      helpText("Handsontable demo output. Column add/delete does work ",
               "for tables with defined column properties, including type."),
      radioButtons("useType", "Use Data Types", c("TRUE", "FALSE"))
    ),
    mainPanel(
      rHandsontableOutput("hot", width = 350)
    )
  )
))

server <- shinyServer(function(input, output, session) 
  values = reactiveValues()

  data = reactive(
    if (!is.null(input$hot)) 
      DF = hot_to_r(input$hot)
     else 
      if (is.null(values[["DF"]]))
        DF = data.frame(val = 1:10, bool = TRUE, nm = LETTERS[1:10],
                        dt = seq(from = Sys.Date(), by = "days", length.out = 10),
                        stringsAsFactors = F)
      else
        DF = values[["DF"]]
    


    values[["DF"]] = DF
    DF
  )

  output$hot <- renderRHandsontable(
    DF = data()
    if (!is.null(DF))
      rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all")
  )
)

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

如何禁用前两列的编辑功能?

【问题讨论】:

【参考方案1】:

整个表和个别列可以设置为只读以防止用户进行更改。(http://jrowen.github.io/rhandsontable/#read-only)

library(shiny)
library(rhandsontable)

ui <- shinyUI(fluidPage(
  titlePanel("Handsontable"),
  sidebarLayout(
    sidebarPanel(
      helpText("Handsontable demo output. Column add/delete does work ",
               "for tables with defined column properties, including type."),
      radioButtons("useType", "Use Data Types", c("TRUE", "FALSE"))
    ),
    mainPanel(
      rHandsontableOutput("hot", width = 350)
    )
  )
))

server <- shinyServer(function(input, output, session) 
  values = reactiveValues()

  data = reactive(
    if (!is.null(input$hot)) 
      DF = hot_to_r(input$hot)
     else 
      if (is.null(values[["DF"]]))
        DF = data.frame(val = 1:10, bool = TRUE, nm = LETTERS[1:10],
                        dt = seq(from = Sys.Date(), by = "days", length.out = 10),
                        stringsAsFactors = F)
      else
        DF = values[["DF"]]
    


    values[["DF"]] = DF
    DF
  )

  output$hot <- renderRHandsontable(
    DF = data()
    if (!is.null(DF))
      rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all") %>%
      hot_col("val", readOnly = TRUE) %>%
      hot_col("bool",readOnly = TRUE )
  )
)

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

【讨论】:

以上是关于在 rhandsontable 中禁用列编辑?的主要内容,如果未能解决你的问题,请参考以下文章

获取 Rhandsontable 的选定行

允许在 Rhandsontable 列下拉列表中进行多选

使用 rhandsontable 包在闪亮上编辑多个数据框

Shiny and rhandsontable - 基于列总和的条件单元格/列格式

具有行突出显示和复选框列的 rhandsontable

Rhandsontable 从逻辑 == TRUE 收集值