【问题标题】:Grouped select input with only one item只有一项的分组选择输入
【发布时间】:2019-12-06 11:23:06
【问题描述】:

我想将我的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) 

以及效果:

你知道怎么处理吗?

【问题讨论】:

  • 使用list() 而不是c() 列出每个组中的国家 - ... "North America" = list("Canada")
  • 太棒了! @H1,把它作为一个答案,我会接受它。

标签: r shiny grouping selectinput


【解决方案1】:

我不知道为什么会这样,但这对我来说是这样的

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 的H1 解决方案更好。
  • 是的,我也这么认为。在发布我的答案之前没有看到它。
【解决方案2】:

如果只有一个元素,您需要使用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")
  ))
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2015-09-01
    • 1970-01-01
    相关资源
    最近更新 更多