rshinygroupingselectinput

Grouped select input with only one item


I wanted to group my selectInput data as explained here: https://shiny.rstudio.com/gallery/option-groups-for-selectize-input.html. Everything works except the situation where there is only one item in the group.

Here is an example (with correct first selectInput and strange second one):

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) 

And the effect:

enter image description here

Do you know how to deal with that?


Solution

  • You need to use list() instead of c() if there is a single element. If there is more than one element you can use either list() or 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")
      ))
    )