4

如何从selectInput具有分组选项的下拉框中的选定输入中获取组名称?Building例如,在我选择BankBuilding和选择内Nature之后如何获得?BankNature

更新示例:

# 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)
    })
  }
)

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

4

1 回答 1

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)
    })
  }
)
于 2019-01-29T16:20:28.017 回答