【问题标题】:Is there a way to have different dropdown options for different rows in an rhandsontable?有没有办法为 rhandsontable 中的不同行提供不同的下拉选项?
【发布时间】:2019-12-29 05:45:18
【问题描述】:

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

有没有办法做到这一点?

我考虑过创建单独的表格并将它们组合在一起看起来像一张表格。只有第一个表会有列标题,它下面的所有其他表都没有列标题。我试图避免这种情况,因为 rhandsontables 具有水平和垂直滚动条,并且没有办法在多个表之间同步滚动条。这会使“一张桌子”看起来有点不对劲。实际上,将所有数据放在一个表中会看起来更好,功能也会更好。

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

  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)

我希望下拉选项仅包含对象列值为“汽车”的所有行的“轮胎”、“雨刷”和“前照灯”。因为汽车永远不需要排水沟或地毯,我不希望用户能够选择其中任何一个选项。

对于“房子”是“对象”列中的值的每一行,用户应该只有两个选项显示在下拉列表中……“排水沟”和“地毯”。这将有助于避免用户错误。

【问题讨论】:

  • 看看这个与行相关的下拉菜单:github.com/jrowen/rhandsontable/issues/105 我想你可以设置reactiveValuesobserve 来存储数据,并使用selectCallback = TRUErhandsontable -- - 如果您通过input$ExampleTable_select$select$r 获得行号,您可以将source 的选项限制为汽车或房屋类型的选项...希望这会有所帮助...
  • @Ben 感谢分享链接。如果您有兴趣,我在下面留下了一个工作示例。
  • 感谢@Ben,成功了!

标签: r shiny rhandsontable


【解决方案1】:

这是一个基于我分享的示例的有效解决方案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 下拉列表。

【讨论】:

  • DF可以定义为eventReactive()而不是reactiveVal()吗?
猜你喜欢
  • 2021-01-06
  • 2016-11-24
  • 1970-01-01
  • 2014-07-16
  • 2021-12-13
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多