分组 select 输入只有一项

Grouped select input with only one item

我想按照此处的说明对 selectInput 数据进行分组:https://shiny.rstudio.com/gallery/option-groups-for-selectize-input.html。除了组中只有一项的情况外,一切正常。

这是一个例子(第一个正确selectInput,第二个奇怪):

library(shiny)

ui <- fluidPage(
    selectInput("country", "Select country", list(
        "Europe" = c("Germany", "Spain"),
        "North America" = c("Canada", "United States" = "USA")
    )),

    selectInput("country", "Select country", list(
        "Europe" = c("Germany", "Spain"),
        "North America" = c("Canada")
    ))
)

server <- function(input, output, session) {
}

shinyApp(ui = ui, server = server) 

效果:

你知道怎么处理吗?

我不知道为什么会这样,但这就是我的工作方式

library(shiny)

ui <- fluidPage(
  selectInput("country", "Select country", list(
    "Europe" = c("Germany", "Spain"),
    "North America" = c("Canada", "United States" = "USA")
  )),

  selectInput("country", "Select country", list(
    "Europe" = c("Germany", "Spain"),
    "North America" = c("Canada", "")
  ))
)

server <- function(input, output, session) {
}

shinyApp(ui = ui, server = server)

代码的唯一区别是我在此处添加了"" "North America" = c("Canada", "")。这给了我

如果只有一个元素,您需要使用 list() 而不是 c()。如果有多个元素,您可以使用 list()c().

  ui <- fluidPage(
  selectInput("country", "Select country", list(
    "Europe" = list("Germany", "Spain"),
    "North America" = list("Canada", "United States" = "USA")
  )),

  selectInput("country", "Select country", list(
    "Europe" = list("Germany", "Spain"),
    "North America" = list("Canada")
  ))
)