My goal is to create a reactive checkboxGroupInput which changes based on user selection. I have done this using observe() and updateCheckboxGroupInput. The problem is that the options I would like to provide the user vary in length and value. Is there another way to achieve this which is more flexible? It seems like one solution would be to use several conditionalPanels but I feel like there has to be a better way to do this. Thanks in advance!
Here is a reproducible example. Desired checkbox options to display for each condition:
Condition1 - a, b, c
Condition2 - a, b, c, d
Condition3 - a
Below is what ends up being displayed. I have to add a value to the list of Condition1 to avoid an error, and Condition3 is duplicated to match the length of Condition2.
Condition1 - a, b ,c, extra_val_so_no_error
Condition2 - a, b, c, d
Condition3 - a, a, a, a
library(shiny)
ui <- fluidPage(
titlePanel("reproducible example checkboxGroupInput question"),
mainPanel(
selectInput("condition",
label = h5("Select a condition:"),
choices = list("Condition 1" = 1,
"Condition 2" = 2,
"Condition 3" = 3),
selected = "1"
),
checkboxGroupInput("dynamic",
label = h5("Select one or more:"),
choices = NULL,
selected = NULL
),
)
)
server <- function(input, output, session) {
observe(updateCheckboxGroupInput(session = session,
inputId = "dynamic",
choices = case_when(input$condition == "1" ~ list("a", "b", "c", "extra_val_so_no_error"),
input$condition == "2" ~ list("a", "b", "c", "d"),
input$condition == "3" ~ list("a")),
selected = "a")
)
}
shinyApp(ui = ui, server = server)