how to make two menu dependent or hierarchical in Rshiny?

Viewed 73

I have two inputmenu in the UI. One is the case menu and the second one is the slider menu.

I want to when I fist click case menu and select the slider 1. It only shows the slider menu which is slider1 , and when I click case menu and select the slider2. It only shows the slider menu which is slider2.


ui <- fluidPage(
   
    fluidRow(
        column(5,
               selectInput("case", "case",c("slider1"="slider1","slider2"="slider2"
               ))),
        
        
        column(5,    
               sliderInput("slider1", "slider1",
                           min = 0, max = 9, value = 5)),
        
        
        column(5,    
               sliderInput("slider2", "slider2",
                           min = 0, max = 20, value = 2))
        
        
    ))

server <- function(input, output) {}

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

1 Answers

You can use conditionalPanel here. This will create a panel that is visible or invisible based on the value of a Javascript expression. In this case, your expression will check the value of input.case, the selectInput to choose which slider you want.

library(shiny)

ui <- fluidPage(
  
  fluidRow(
    column(5,
           selectInput("case", "case", c("slider1" = "slider1", "slider2" = "slider2"
           ))),
    conditionalPanel(
      condition = "input.case == 'slider1'",
      column(5,    
             sliderInput("slider1", "slider1",
                       min = 0, max = 9, value = 5))),
    conditionalPanel(
      condition = "input.case == 'slider2'",
      column(5,    
             sliderInput("slider2", "slider2",
                       min = 0, max = 20, value = 2)))
  )
)
Related