2

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

以及效果:

在此处输入图像描述

你知道如何处理吗?

4

2 回答 2

2

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

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", "")。这给了我

输出

于 2019-07-29T06:55:23.273 回答
1

如果只有一个元素,您需要使用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")
  ))
)
于 2019-07-29T07:41:06.623 回答