从SelectInput(RShiny)中的选项分组列表中获取组标签

时间:2019-01-28 20:20:59

标签: r shiny selectinput

如何在具有分组选项的selectInput下拉框中从选定的输入中获取组名?例如,如何在Building中选择Bank并在Building中选择Nature之后选择Bank后得到Nature

更新示例:

# demoing optgroup support in the `choices` arg
shinyApp(
  ui = fluidPage(
    selectInput("state", "Choose a word:",
      list(`Building` = list("Apartment", "Bank", "Hospital"),
           `Nature` = list("Bank", "River", "Orange"),
           `Color` = list("Blue", "Orange", "Red"))
    ),
    textOutput("result")
  ),
  server = function(input, output) {
    output$result <- renderText({
      paste("You chose", input$state)
    })
  }
)

一种方法是存储所有选项及其分组标签的变量,然后搜索该选项来自哪个组。但是,当组之间的选择重叠时,这是行不通的。

1 个答案:

答案 0 :(得分:0)

您可以给每个输入一个值,而不是直接使用它们的名称,如下所示:

shinyApp(
  ui = fluidPage(
    selectInput("state", "Choose a word:",
                list(`Building` = list("Apartment"="ap", "Bank"="bk", "Hospital"="hp"),
                     `Nature` = list("Bank"="bk1", "River"="rv", "Orange"="or"),
                     `Color` = list("Blue"="bl", "Orange"="or1", "Red"="rd"))
    ),
    textOutput("result")
  ),
  server = function(input, output) {
    output$result <- renderText({
      paste("You chose", input$state)
    })
  }
)