In a Shiny app with a data table, how do I programmatically select and de-select rows…
- User
In a Shiny app with a data table, how do I programmatically select and de-select rows via proxy?
Assistant
Here is how you can programmatically select and de-select rows in a Shiny data table using proxy:
library(shiny) library(DT) ui <- fluidPage( DT::dataTableOutput("mytable") ) server <- function(input, output, session) { df <- mtcars output$mytable = DT::renderDataTable({ DT::datatable(df) }) # Select rows where mpg > 20 observeEvent(input$somebutton, { session$sendCustomMessage(type = "selectRows", params = list(rows = which(df$mpg > 20))) }) # Deselect all rows observeEvent(input$clearbutton, { session$sendCustomMessage(type = "deselectRows", params = list(rows = NULL)) }) } shinyApp(ui, server)The key is to use
session$sendCustomMessageto send a message to the data table's JavaScript proxy, specifying the row indices you want to select/deselect. This allows programmatic control over row selection from the server side.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.