Search code examples
rcheckboxshinyshinydashboard

R Shiny checkboxGroup filter when unchecked


I have a series of grouped checkboxes on a shinydashboard side panel that is used to subset data. All bar one group has a selected box checked. The final group has just one option and the default is unchecked. It points to a binary variable (0, 1) on the data set. If unchecked no filter should apply and if checked it subsets on the 1 value.

So far I have this in ui

checkboxGroupInput("patFilt", 
                   label = h5("Filter by patient cohort"),
                   choices = c("Patient cohort" = 1),
                   selected = 0,
                   width = "100%")

and in my server.R I have

subsetArg <- reactive(list(
    input$group1,
    input$group2,
    input$group3,
    etc.,
    ifelse(is.null(input$patFilt), c(0, 1), 1)
  ))

Subsetting is done in a function with subsetArg passing the arguments.

However, for this single checkbox the default option only returns the observations marked 0 until the box is checked then it correctly returns the observations marked 1. Interestingly if I change c(0, 1) to c(1,0) the unchecked option returns observations marked 1. So subsetArg is only returning the first value in the vector.

I've got this to work when there is two check boxes with both selected as default but I cannot get the single checkbox to work when unchecked.

How do I get it so that when the checkbox is unchecked all observations are returned?

Thanks


Solution

  • Solved using

    if(is.null(input$patFilt)) c(0,1) else 1
    

    Don't know why ifelse() didn't work.