1

The dropdownButton function from shinyWidgets is great because I can use it to easily create Check / Uncheck all options in a checkboxGroupInput like this.

However, let's say I want to put two (or more) of these dropdownButtons together in another dropdownButton. I can't seem to make this work. Here is my attempt. (I am building the ui items within the server.R script because in my actual project, I need them to work that way.)

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
    titlePanel("Dropsdowns in your Dropdowns"),
    sidebarLayout(sidebarPanel(
        uiOutput("io_c1"),
        uiOutput("io_c2"),
        uiOutput("io_combine")
    ),
    mainPanel())
)

server <- function(input, output, session) {
    ## drop down 1 items ##
    # drop down 1 choices
    c1 <- checkboxGroupInput(
        inputId = "check1",
        label = "pick carb",
        choices = unique(mtcars$carb),
        selected = unique(mtcars$carb)
    )

    # select all / none drop down 1
    sa1 <- actionButton(inputId = "c1_check_all",
                        label = "(un)select all")

    # set up observe on check box 1
    observeEvent(input$c1_check_all, {
        if (is.null(input$check1)) {
            updateCheckboxGroupInput(
                session = session,
                inputId = "check1",
                selected = unique(mtcars$carb)
            )
        } else {
            updateCheckboxGroupInput(
                session = session,
                inputId = "check1",
                selected = ""
            )
        }
    })

    # put all / none button and check box 1 into a dropdownbutton
    output$io_c1 <- renderUI({
        dropdownButton(sa1,
                       c1,
                       circle = FALSE,
                       label = "Carbs",
                       status = "default")
    })

    ## drop down 2 items ##
    # drop down 2 choices
    c2 <- checkboxGroupInput(
        inputId = "check2",
        label = "pick gear",
        choices = unique(mtcars$gear),
        selected = unique(mtcars$gear)
    )

    # select all / none drop down 2
    sa2 <- actionButton(inputId = "c2_check_all",
                        label = "(un)select all")

    # set up observe on check box 2
    observeEvent(input$c2_check_all, {
        if (is.null(input$check2)) {
            updateCheckboxGroupInput(
                session = session,
                inputId = "check2",
                selected = unique(mtcars$gear)
            )
        } else {
            updateCheckboxGroupInput(
                session = session,
                inputId = "check2",
                selected = ""
            )
        }
    })

    # put all / none button and check box 2 into a dropdownbutton
    output$io_c2 <- renderUI({
        dropdownButton(sa2,
                       c2,
                       circle = FALSE,
                       label = "Gears",
                       status = "default")
    })

    ## now try to make this all one thing ##
    d1 <-  dropdownButton(sa1,
                          c1,
                          circle = FALSE,
                          label = "Carbs",
                          status = "default")

    d2 <- dropdownButton(sa2,
                         c2,
                         circle = FALSE,
                         label = "Gears",
                         status = "default")

    output$io_combine <-
        renderUI(dropdownButton(
            d1,
            d2,
            circle = FALSE,
            label = "Drop Downs Here",
            status = "default"
        ))

}

shinyApp(ui = ui, server = server)

So I got this working with each of the individual dropdownButtons , but when I try to put the results all together into one in that third part of the code, it does not work. The dropdownButton is created and the two separate dropdownButtons are in there, but they don't respond at all. Any ideas what I might be missing?

Nick Criswell
  • 1,733
  • 2
  • 16
  • 32

0 Answers0