Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get sorting state in Shiny #265

Closed
pvictor opened this issue Aug 18, 2022 · 3 comments
Closed

Get sorting state in Shiny #265

pvictor opened this issue Aug 18, 2022 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@pvictor
Copy link

pvictor commented Aug 18, 2022

Hello,

Is it possible to get sorting options in a shiny application ? On which columns is the table sorted and in which order.
Something like :

getReactableState("table", "sort")

will be great !

@glin
Copy link
Owner

glin commented Aug 28, 2022

Hi, that's not possible with getReactableState() today, but would be fairly straightforward to add - added as a feature request. I just didn't know if anyone would find it useful to get the sorted state, so I'm curious what you want to use it for, if you have time to share.

There is a a way to get sorted state in the JavaScript API though, using Reactable.getState(). This could be used as a partial workaround (send it to Shiny via Shiny.setInputValue()), although there's also not currently a way to listen on state changes and automatically send the sorting state up.

@glin glin added the enhancement New feature or request label Aug 28, 2022
@pvictor
Copy link
Author

pvictor commented Aug 29, 2022

I want to export the table data in a special format (an Excel file with added metadata), and I would like to keep the sorting done by the user in the export.
Reactable.getState().sorted is perfect for my use-case I think, I can get the value when export button is clicked and reproduce the sorting in R (I just tested with simple sorting for the moment, have to check how to manage NA or more complicated sorting).
Otherwise getting the row indice sorted (and maybe with filters applied) could be nice, but less straightforward.

Here's my code using the JavaScript API :

library(shiny)
library(reactable)

# custom handler otherwise JSON is over simplified
registerInputHandler("reactable_sort", function(x, ...) {
  x
}, force = TRUE)

ui <- fluidPage(
  downloadButton(
    "downloadData", "Download",
    onClick = "Shiny.setInputValue('table_state:reactable_sort', Reactable.getState('cars_table').sorted)"
  ),
  reactableOutput("cars_table"),
  verbatimTextOutput("test")
)

server <- function(input, output) {
  output$cars_table <- renderReactable({
    reactable(MASS::Cars93)
  })
  output$test <- renderPrint(input$table_state)
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      data <- if (is.null(input$table_state)) {
        MASS::Cars93
      } else {
        data <- MASS::Cars93
        table_state <- input$table_state
        for (i in rev(seq_along(input$table_state))) {
          col <- table_state[[i]]$id
          desc <- table_state[[i]]$desc
          data <- data[order(data[[col]], decreasing = isTRUE(desc)), ]
        }
        data
      }
      write.csv(data, file)
    }
  )
}

shinyApp(ui, server)

@glin glin added this to the v0.4.0 milestone Oct 9, 2022
@glin
Copy link
Owner

glin commented Oct 9, 2022

getReactableState() now includes the sorted state in the development version. If columns are sorted, getReactableState(outputId, "sorted") will returned a named list of sorted columns like list(Model = "asc", Type = "desc").

And then I also added a way to listen on table state changes in the JavaScript API, so you can send table state to Shiny even if getReactableState() doesn't support it.

  • New Reactable.onStateChange() method in the JavaScript API that sets up a function to be called whenever the table state changes (#265).
  • getReactableState() now includes the current sorted columns (#265).

@glin glin closed this as completed Oct 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants