getReactableState()
gets the state of a reactable instance within a Shiny application.
getReactableState(outputId, name = NULL, session = NULL)
The Shiny output ID of the reactable
instance.
Name of a state value to get. One of "page"
, "pageSize"
,
"pages"
, or "selected"
. If unspecified, all values will be returned
in a named list.
The Shiny session object. Defaults to the current Shiny session.
If name
is specified, one of the following values:
page
: the current page
pageSize
: the page size
pages
: the number of pages
selected
: the selected rows - a numeric vector of row indices, or NULL
if no rows are selected
If name
is unspecified, getReactableState()
returns a named list containing all values.
If the table has not been rendered yet, getReactableState()
returns NULL
.
# Run in an interactive R session
if (interactive()) {
library(shiny)
library(reactable)
ui <- fluidPage(
actionButton("prev_page_btn", "Previous page"),
actionButton("next_page_btn", "Next page"),
reactableOutput("table"),
verbatimTextOutput("table_state")
)
server <- function(input, output) {
output$table <- renderReactable({
reactable(
iris,
showPageSizeOptions = TRUE,
selection = "multiple"
)
})
output$table_state <- renderPrint({
state <- req(getReactableState("table"))
print(state)
})
observeEvent(input$prev_page_btn, {
# Change to the previous page
page <- getReactableState("table", "page")
if (page > 1) {
updateReactable("table", page = page - 1)
}
})
observeEvent(input$next_page_btn, {
# Change to the next page
state <- getReactableState("table")
if (state$page < state$pages) {
updateReactable("table", page = state$page + 1)
}
})
}
shinyApp(ui, server)
}