How to add a conditional 'selectInput' based on a selected checkboxInput in shiny?

Viewed 2386

I would like that if the checkboxInput Factorial parameters is selected, this would do the app to show a new selectInput with several option. Here is a minimum working example.

library(shiny)
ui <- fluidPage(

   titlePanel("Old Faithful Geyser Data"),
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         checkboxInput("fixed", "Factorial parameters"),
         conditionalPanel(condition = "fixed == 'TRUE'",
                      selectInput("choice",
                                  "Choose your fixed parameter", 
                                  c("alpha"= "Alpha", "beta"="Beta"),
                                  selected = "alpha"))
                      )
  ,
     mainPanel(
         plotOutput("distPlot")
      )
   )
)


  server <- function(input, output) {

   output$distPlot <- renderPlot({

      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}


shinyApp(ui = ui, server = server)

Can anyone tell me what am I doing wrong?

Many thanks!

1 Answers

Update

I figured it out! edit as follows:

conditionalPanel(condition = "input.fixed == 1",

Previous answers

Generally, you would need input.fixed in a conditionalPanel although this doesn't seem to work for checkboxInput for some reason (maybe someone else can explain why?). There are a few alternatives. I would suggest the shinyjs package.

library(shiny)
library(shinyjs)
ui <- fluidPage(
  useShinyjs(),
  titlePanel("Old Faithful Geyser Data"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30),
      checkboxInput("fixed", "Factorial parameters"),
      hidden(selectInput("choice",
                         "Choose your fixed parameter", 
                         c("alpha"= "Alpha", "beta"="Beta"),
                         selected = "alpha"))
    )
    ,
    mainPanel(
      plotOutput("distPlot")
    )
  )
)


server <- function(input, output) {

  output$distPlot <- renderPlot({

    x    <- faithful[, 2] 
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
    observeEvent(input$fixed, {
      toggle("choice")
    }, ignoreInit = TRUE)
  })
}


shinyApp(ui = ui, server = server)

Instead of toggle you could be more explicit with an if...else statement using shinysj::show() and shinyjs::hide() although I think this is neater (just note the ignoreInit = TRUE).

As mentioned above using checkboxGroupInput for example seems to work with conditionalPanels:

checkboxGroupInput("fixed", label = "", choices = "Factorial parameters"),
    conditionalPanel(condition = "input.fixed == 'Factorial parameters'",
                     selectInput("choice",
                                 "Choose your fixed parameter", 
                                 c("alpha"= "Alpha", "beta"="Beta"),
                                 selected = "alpha"))

slightly hacky but does the job.

Related