# Possible choices and selected configurations
# Choices as list of unnamed options
# Names are the same as values in the component (if not precised elsewhere)
choices_unnamed <- list(
fruits = c("orange", "apple", "lemon"),
vegetables = c("potato", "carrot", "broccoli")
)
# selected only fruits plus orange one within
selected_unnamed <- list(
fruits = c("orange")
)
# Names for each group precised separately
choices_names = list(
fruits = c("Orange", "Apple", "Lemon"),
vegetables = c("Potato", "Carrot", "Broccoli")
)
# Choices as list of named options
# Names are treated as checkbox options labels
choices_named <- list(
fruits = c("Orange" = "orange", "Apple" = "apple", "Lemon" = "lemon"),
vegetables = c("Potato" = "potato", "Carrot" = "carrot", "Broccoli" = "broccoli")
)
# selected: fruits plus orange and vegetables carrot
selected_named <- list(
fruits = c("orange"),
vegetables= c("carrot")
)
# Same but vegetables selected but empty
# Set group as NA to no options checked (same effect in server input)
selected_named_empty <- list(
fruits = c("orange"),
vegetables = NA
)
# Specifying picker and group labels ("key" = "name" rule)
choices_labels <- list("fruits" = "Fruits", "vegetables" = "Vegetables")
if (interactive()) {
library(shiny)
ui <- fluidPage(
sidebarLayout(sidebarPanel(
pickCheckboxInput(
"pci1", "1. No names at all",
choices = choices_unnamed, selected = selected_unnamed
), hr(),
pickCheckboxInput(
"pci2", "2. Names provided as `choicesNames`",
choices = choices_unnamed, selected = selected_unnamed, choicesNames = choices_names
), hr(),
pickCheckboxInput(
"pci3", "3. Names provided directly in choices",
choices = choices_named, selected = selected_named
), hr(),
pickCheckboxInput(
"pci4", "4. Group as NA value to select group (without any choices)",
choices = choices_named, selected = selected_named_empty
), hr(),
pickCheckboxInput(
"pci5", "5. Group names provided as `choicesLabels`",
choices = choices_named, selected = selected_named_empty, choicesLabels = choices_labels
)
),
mainPanel(
verbatimTextOutput("out1"),
verbatimTextOutput("out2"),
verbatimTextOutput("out3"),
verbatimTextOutput("out4"),
verbatimTextOutput("out5")
))
)
server <- function(input, output, session) {
output$out1 <- renderPrint({print("Result 1."); input$pci1})
output$out2 <- renderPrint({print("Result 2."); input$pci2})
output$out3 <- renderPrint({print("Result 3."); input$pci3})
output$out4 <- renderPrint({print("Result 4."); input$pci4})
output$out5 <- renderPrint({print("Result 5."); input$pci5})
}
shinyApp(ui, server)
}
Run the code above in your browser using DataLab