-
Notifications
You must be signed in to change notification settings - Fork 6
Description
Guidelines
- I agree to follow this project's Contributing Guidelines.
Description
I'd like the ability to uncheck/check all checkboxes created with checkbox_extra().
Problem
Below is a reprex of my issue. I'd like to be able to uncheck or check all boxes without individually clicking each one. I am using reactiveValues to record which boxes have been checked. I tried using actionButtons along with Shiny.setInputValue to change all the values to TRUE or FALSE. This successfully gets reflected next to "Checked Letters:"), but does not change the actual checked/unchecked appearance of the checkbox.
library(dplyr)
library(reactable)
library(reactable.extras)
library(shiny)
library(shinyjs)
df <- data.frame(
letter = letters[1:10],
checked = sample(c(TRUE, FALSE), length(letters[1:10]), TRUE)
)
ui <- fluidPage(
useShinyjs(),
reactable_extras_dependency(),
reactableOutput("table"),
actionButton("check_all", "Check All"),
actionButton("uncheck_all", "Uncheck All"),
h5("Checked Letters:", textOutput("checked_letters"))
)
server <- function(input, output, session) {
df <- df %>%
select(letter, checked) %>%
distinct(letter, .keep_all = TRUE)
output$table <- renderReactable({
reactable(
df,
columns = list(
checked = colDef(
name = "",
cell = checkbox_extra("checkbox", key = "letter"),
)
)
)
})
rv <- reactiveValues(data = df)
observeEvent(input$checkbox, {
rv$data$checked[rv$data$letter == input$checkbox$row] <- input$checkbox$value
})
output$checked_letters <- renderText({
checked_letters <- rv$data
checked_letters <- checked_letters %>%
filter(checked == TRUE)
checked_letters <- checked_letters$letter
return(checked_letters)
})
observeEvent(input$check_all, {
for (i in unique(df$letter)) {
js2run <- paste(
"Shiny.setInputValue('checkbox', { row: ",
"'", i, "', ",
"value: 'TRUE', column: 'checked' })",
sep = ""
)
shinyjs::runjs(js2run)
}
input$check_all
})
observeEvent(input$uncheck_all, {
for (i in unique(df$letter)) {
js2run <- paste(
"Shiny.setInputValue('checkbox', { row: ",
"'", i, "', ",
"value: 'FALSE', column: 'checked' })",
sep = ""
)
shinyjs::runjs(js2run)
}
})
}
shinyApp(ui, server)
Proposed Solution
I believe what I'm seeking is an updateCheckboxInput()-like function that works with checkboxes created with checkbox_extra().
Alternatives Considered
None at this time