0

In the sidebar of my Shiny app, I am looking to offer filtering by the values of a certain variable only when a checkbox activating that filter has been checked before. I am trying to implement this as follows:

checkboxInput("filterByDistrict", "Activate filtering by district", FALSE),

  conditionalPanel(
    condition = "input.filterByDistrict == true",
    checkboxGroupInput(
      "districts", 
      label = "Choose a district:", 
      choices = choicesList,
      selected = choicesList
    )
  )

What I found is that the districts input variable never gets initiated. I am using it on the server side, where the check for its existence always fails:

...
if(exists(input$districts)) {
...

What am I missing/doing wrong?

matt_jay
  • 1,241
  • 1
  • 15
  • 33
  • I assume you meant to write: `... if(exists("input$districts")) { ...` since `exists()` requires a string input but regardless it wouldn't work for list input as described below. – Jon Calder Aug 19 '15 at 13:20

1 Answers1

1

Your input is defined, but exists doesn't work with list elements, try :

"districts" %in% names(input)
# or
!is.null(input$districts)
Victorp
  • 13,636
  • 2
  • 51
  • 55
  • For more info, you can refer to [this thread](http://stackoverflow.com/questions/7719741/how-to-test-if-list-element-exists) – Jon Calder Aug 19 '15 at 13:19
  • This put me on the right track, thanks! Indeed nothing was wrong in the bits I described, the erroneous behavior I observed was caused elsewhere. – matt_jay Aug 19 '15 at 14:05