rshinyshinyjsshinywidgets

How to hide/show a shinyWidgets dropdown with shinyjs?


Is there a way to hide shinyWidgets dropdown with shinyjs? Since dropdown has an id argument, i thought it could be hidden/shown like any other input. But somehow its not working. Below you find a minimal Reprex:

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  shinyjs::useShinyjs(),# this enables shinyjs functionality
    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
          shinyjs::hidden(
            shinyWidgets::dropdown(inputId = "dropdown",
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
              )
            )
          ),


        # Show a plot of the generated distribution
        mainPanel(
          actionButton("showdd", "show dropdown"),
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  observeEvent(input$showdd,{
    print("test")
    shinyjs::show(id = "dropdown")
    shinyjs::show(id = "bins")
  } )
  
  
    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white',
             xlab = 'Waiting time to next eruption (in mins)',
             main = 'Histogram of waiting times')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Solution

  • The id gets passed to the button element, but you would like to show or hide the outer div. This internally gets an "sw-drop-" prepended on its id. Hence, you can use shinyjs::show(id = "sw-drop-dropdown"):

    library(shiny)
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
      shinyjs::useShinyjs(),# this enables shinyjs functionality
      # Application title
      titlePanel("Old Faithful Geyser Data"),
      
      # Sidebar with a slider input for number of bins 
      sidebarLayout(
        sidebarPanel(
          shinyjs::hidden(
            shinyWidgets::dropdown(inputId = "dropdown",
                     sliderInput("bins",
                                 "Number of bins:",
                                 min = 1,
                                 max = 50,
                                 value = 30)
            )
          )
        ),
        
        
        # Show a plot of the generated distribution
        mainPanel(
          actionButton("showdd", "show dropdown"),
          plotOutput("distPlot")
        )
      )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
      
      observeEvent(input$showdd,{
        shinyjs::show(id = "sw-drop-dropdown")
        shinyjs::show(id = "bins")
      } )
      
      
      output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        
        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white',
             xlab = 'Waiting time to next eruption (in mins)',
             main = 'Histogram of waiting times')
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)