I am making a shiny app with a reactable
where the user can choose how to sort the table. The trouble is when the table refreshes the sorting goes back to the default sorting specified when the table is created. I want to be able to have the table refresh to the sorting specifications the user has chosen.
I know this is possible by using a selectInput
or pickerInput
or some other input along those lines which can feed to the defaultSorted
argument in the reactable
but I am looking for something a little bit more organic without the need of an external input. Currently the getReactableState
only provides page size/number and selection references.
Below is a toy example where if the user sorts by the first 2 columns and then clicks toggle, the table refreshes and goes back to its original sort state.
library(shiny)
library(reactable)
ui <- fluidPage(
actionButton("toggle", "Toggle"),
reactableOutput("table")
)
server <- function(input, output, session){
df <- eventReactive(input$toggle, {
mtcars[c(1:2,sample(3:ncol(mtcars), size = 3))]
})
output$table <- renderReactable({
reactable(df(),
filterable = FALSE,
sortable = TRUE)
})
}
shinyApp(ui, server)