有没有办法为 rhandsontable 中的不同行设置不同的下拉选项?

Is there a way to have different dropdown options for different rows in an rhandsontable?

我正在制作一个闪亮的应用程序,用户需要在 table 中选择 select 下拉选项。我正在使用 rhandsontable,但似乎我只能为单个列设置一组下拉选项。所以第 1 行与第 800 行具有相同的选项。我希望第 1 行具有与第 800 行不同的一组下拉选项。

有办法吗?

我考虑过创建单独的 table 并将它们组合在一起看起来像一个 table。只有第一个 table 会有列 headers,而它下面的所有其他 table 不会有列 headers。我试图避免这种情况,因为 rhandsontables 有水平和垂直滚动条,并且没有办法在多个 tables 之间同步滚动条。这会使 "one table" 看起来有点不对劲。实际上,将所有数据放在一个 table 中看起来和功能会好得多。

我在下面有一个超级简单的例子:

  require(shiny)
  require(rhandsontable)

  ui <- fluidPage(
    hr(""),

    # Display table
    mainPanel(rHandsontableOutput("ExampleTable"))
  )

  server <- function(input, output) {

    output$ExampleTable <- renderRHandsontable({

      # creating table that will be displayed 
      df <- data.frame(Object = c("car", "car", "car", "house", "house", "house"), Needs = NA, stringsAsFactors = FALSE)

      # defining dropdown options
      dropdownOptions <- c("tires", "wipers", "headlights", "gutters", "carpet")

      rhandsontable(df, rowHeaders = NULL, stretchH = "all") %>%
        hot_col("Object", readOnly = TRUE) %>%
        hot_col("Needs", type = "dropdown", source = dropdownOptions)

    })
  }

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

对于 Object 列值为 [=27 的所有行,我希望下拉选项仅包括 "tires"、"wipers" 和 "headlights" =].因为汽车永远不需要排水沟或地毯,所以我不希望用户能够 select 这些选项中的任何一个。

对于 "house" 是 Object 列中的值的每一行,用户应该只在下拉列表中显示两个选项..."gutters" 和 "carpet".这将有助于避免用户错误。

这是一个基于我分享的示例的有效解决方案 here(和上面提到的@Ben):

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  hr(),
  # Display table
  mainPanel(rHandsontableOutput("ExampleTable"))
)

server <- function(input, output) {
  
  # creating table that will be displayed 
  DF <- reactiveVal(data.frame(Object = c("car", "car", "car", "house", "house", "house"), Needs = NA_character_, stringsAsFactors = FALSE))
  
  # update df() on user changes
  observeEvent(input$ExampleTable, {
    DF(hot_to_r(input$ExampleTable))
  })
  
  output$ExampleTable <- renderRHandsontable({

    # defining dropdown options
    carOptions <- c(NA_character_, "tires", "wipers", "headlights")
    houseOptions <- c(NA_character_, "gutters", "carpet")
    
    tmpExampleTable <- rhandsontable(DF(), rowHeaders = NULL, stretchH = "all", selectCallback = TRUE, width = 300, height = 300) %>%
      hot_col("Object", readOnly = TRUE) %>%
      hot_col("Needs", allowInvalid = FALSE, type = "dropdown", source = NA_character_, readOnly = TRUE)
    
    if(!is.null(input$ExampleTable_select$select$r)){
      
      selectedObject <- DF()[input$ExampleTable_select$select$r, "Object"]
      
      if(selectedObject == "car"){
        tmpExampleTable <- hot_col(tmpExampleTable, col = "Needs", allowInvalid = FALSE, type = "dropdown", source = carOptions) %>% hot_cell(row = input$ExampleTable_select$select$r, col = "Needs", readOnly = FALSE)
      }
      
      if(selectedObject == "house"){
        tmpExampleTable <- hot_col(tmpExampleTable, col = "Needs", allowInvalid = FALSE, type = "dropdown", source = houseOptions) %>% hot_cell(row = input$ExampleTable_select$select$r, col = "Needs", readOnly = FALSE)
      }
    }
    
    tmpExampleTable
    
  })
}

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

编辑: Here 您可以找到一种用于依赖 rhandsontable 下拉列表的通用方法。