Dynamic checkboxGroupInput Based on User Input
In this example, the user can input a number between 1 and 5 and the app will generate that many checkboxes in the checkboxGroupInput.
R
library (shiny) ui <- fluidPage ( sidebarLayout ( sidebarPanel ( numericInput ( "num_boxes" , "Number of Boxes" , 1, min = 1, max = 5) ), mainPanel ( checkboxGroupInput ( "boxes" , "Select Boxes" ) ) ) ) server <- function (input, output, session) { observe ({ boxes <- 1:input$num_boxes updateCheckboxGroupInput (session, "boxes" , label = NULL , choices = boxes, selected = NULL ) }) } shinyApp (ui, server) |
Output:
Create Dynamic checkboxGroupInput in Shiny package Using R
The Shiny is an R package that allows us to build interactive web applications with R Programming Language. One of the core components of a Shiny app is the ability to dynamically update the content of the app based on user input.
- One way to do this is by using the checkboxGroupInput.
- which allows users to select multiple options from a list of checkboxes.
- The checkboxGroupInput can be dynamically updated based on user input.
- The checkboxGroupInput is a UI element that allows the user to select multiple options from a list of choices.
- To create a dynamic checkboxGroupInput in Shiny.
- We can use the updateCheckboxGroupInput() function in the server code.
Syntax :
checkboxGroupInput(inputId, label, choices, selected = NULL, inline = FALSE)
where,
- inputId: the unique identifier for input control.
- label: The text to display above the input control.
- choices: A vector of options to display as checkboxes.
- selected: The default selected options.
- inline: Whether to display the checkboxes inline or on separate lines.
Contact Us