Search code examples
rshinyshinywidgetsshinytree

How to build a Shiny pickerInput with unclickable subtitles that group options together


I have a simple Shiny app that includes a pickerInput with 8 options. Some of the options belong to the same group in that they are similar. Therefore, I would like to include a header above a group of options, without the header being an option itself, i.e. not being clickable and thus not being registered as an input. Underneath I included a picture of what my pickerInput currently looks like (on the left) and what I hope to achieve (on the right):

enter image description here

I know of shinyTree and that it allows to create a tree structure similar to what I aim to achieve here, but I explicitly don't want the header to be a clickable input, which unfortunately is always the case when using shinyTree.

Here is the code for my app:

library(shiny)

ui <- fluidPage(
  pickerInput(
    "Grouped options",
    label = "Select one or multiple options",
    choices = list(
      "Option 1" = "Option 1", # group 7, 8 and 9 as 'A'
      "Option 2" = "Option 2",
      "Option 3" = "Option 3",
      "Option 4" = "Option 4", # group 4, 5 and 6 as 'B'
      "Option 5" = "Option 5",
      "Option 6" = "Option 6",
      "Option 7" = "Option 7", # group 7 and 8 as 'C'
      "Option 8" = "Option 8"
    ),
    selected = NULL,
    multiple = TRUE,
    options = list(`actions-box` = TRUE),
    choicesOpt = NULL,
    width = NULL,
    inline = FALSE
  )
)

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

shinyApp(ui, server)

Solution

  • ui <- fluidPage(
      pickerInput(
        "Grouped options",
        label = "Select one or multiple options",
        choices = list(
          `Group 1` = list("Option 1", # group 7, 8 and 9 as 'A'
                           "Option 2",
                           "Option 3"),
          `Group 2` = list("Option 4", # group 4, 5 and 6 as 'B'
                         "Option 5",
                         "Option 6"),
          `Group 3` = list("Option 7", # group 7 and 8 as 'C'
                    "Option 8")
        ),
        selected = NULL,
        multiple = TRUE,
        options = list(`actions-box` = TRUE),
        choicesOpt = NULL,
        width = NULL,
        inline = FALSE
      )
    )
    
    server <- function(input, output, session) {
    }
    
    shinyApp(ui, server)
    

    Gives: enter image description here

    And (for me at least) the subheadings are not clickable.