Create Dynamic checkboxGroupInput in Shiny package Using R
Last Updated :
28 Apr, 2025
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.
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:
Dynamic checkboxGroupInput based on user inputDynamic checkboxGroupInput Based on CSV File
Now let's look at another example in which we will upload a .csv file and then checkboxes will be created based on the columns header which are there in the CSV file.
R
library(shiny)
library(readr)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file", "Upload CSV file")
),
mainPanel(
checkboxGroupInput("columns", "Select Columns")
)
)
)
server <- function(input, output, session) {
data <- reactive({
req(input$file)
read_csv(input$file$datapath)
})
observe({
columns <- names(data())
updateCheckboxGroupInput(session, "columns",
label = NULL,
choices = columns,
selected = NULL)
})
}
shinyApp(ui, server)
Output:
Dynamic checkboxGroupInput based on the CSV file
Here is another a bit simpler example based on the mtcars dataset in R.
R
library(shiny)
library(DT)
data <- mtcars
shinyApp(
ui = fluidPage(
selectInput("select1", "select cyl:",
choices = unique(data$cyl)),
uiOutput("checkbox"),
dataTableOutput("table")
),
server = function(input, output) {
output$checkbox <- renderUI({
choice <- unique(data[data$cyl %in% input$select1, "gear"])
checkboxGroupInput("checkbox","Select gear",
choices = choice,
selected = choice[1])
})
}
)
Output:
Dynamic checkboxGroupInput can be useful in many situations where the user needs to select multiple options. For example, in a survey app, the user may want to select multiple options for a question. In a data analysis app, the user may want to select multiple variables to plot or analyze. Dynamic checkboxGroupInput allows for flexibility in the number and options presented to the user, making the app more user-friendly and efficient.
Similar Reads
Create An Interactive Web App Using Shiny Package In R Perhaps a quick introduction to what Shiny is would be helpful before moving on. Creating interactive web applications with R Programming Language is simple thanks to the Shiny package. Shiny's advantage is that it enables you to extend your R code to the web, which essentially increases the usabili
4 min read
Deploying a basic Streamlit app using Shiny package in R In this article, we will be discussing the steps to deploy a basic Streamlit app using the Shiny package in R. Streamlit is a Python library that allows you to build interactive web applications for machine learning and data science. Shiny is an R package that allows you to build interactive web app
4 min read
Change the Color of Action Button in Shiny using R Shiny is an R package that enables users to create web applications directly from R which resembles a framework for developing web apps also known as apps In Shiny apps, one general thing to do is to change the properties of widgets to enhance the usability of the Shiny app. In this article, you are
5 min read
Hide Certain Columns in a Responsive Data Table Using DT Package in R The DT package in R Shiny provides a powerful and interactive way to display data tables in web applications. One useful feature is the ability to hide certain columns based on user interactions or display requirements. In this guide, weâll explore how to hide columns in a responsive data table usin
3 min read
React Suite Checkbox Group React Suite is an open-source front-end library that comes with a set of React components to make it easier for developers to develop a scalable and responsive website. In this article, we will be seeing React Suite Checkbox Group. The CheckboxGroup component is used to group similar checkboxes toge
3 min read
Creating Interactive Plots using Shiny Data visualization is an essential part of data analysis, allowing us to explore trends, patterns, and relationships within our data. While static plots are informative, interactive plots take visualization to the next level, enabling users to interact with the data dynamically. Shiny AppShiny is an
7 min read