reactable icon indicating copy to clipboard operation
reactable copied to clipboard

Get filtered or searched set of rows

Open chrisoswald opened this issue 5 years ago • 2 comments

Hi, It‘s posible to get a list or vector of the result from custom filtered or a custom search subset? getReactableState gives me a vector of the selected rows but not of the filtered or searched rows. I want to calculate a total from the filtered subset. I can only calculate the total of the whole data frame but not from the filtered subset?

chrisoswald avatar Dec 10 '20 00:12 chrisoswald

I am stuck at exactly the same situation. @glin I would be happy to help and create a PR for this. Do you know where I can start or if React Table exports this feature or would we need to get the data using plain JS?

DavZim avatar Sep 29 '22 07:09 DavZim

If I have a table in shiny called "a_table" I can get the filtered/selected/sorted data from the javascript console with Reactable.getState("a_table").sortedData.

Or even better using Reactable.getInstance("a_table").sortedFlatRows.map(x => x.index + 1); I can retrieve the indices of the data (indexed by 1 for R).

First a MWE / POC

For example, an (awkward) MWE using shinyjs could look like this:

(note the app is a bit awkward as I don't fully understand the hooks needed to make the JS code react to changes in the reactable. Instead, the js$getSortedData() function is called every 100 ms and the result is stored in the reactive values).

library(shiny)
library(reactable)
library(shinyjs)

jsCode <- 'shinyjs.getSortedData = function() {
  try {
    var idx = Reactable.getInstance("a_table").sortedFlatRows.map(x => x.index + 1);
    Shiny.onInputChange("sorted_data", idx);
  } catch {}
}'

ui <- fluidPage(
  useShinyjs(),
  extendShinyjs(text = jsCode, functions = "getSortedData"),
  reactableOutput("a_table"),
  verbatimTextOutput("out")
)

server <- function(input, output, session) {
  output$a_table <- renderReactable({
    reactable(mtcars, filterable = TRUE, searchable = TRUE)
  })
  
  observe({ # the awkward part...
    invalidateLater(100)
    js$getSortedData()
  })
  
  output$out <- renderPrint({
    input$sorted_data
  })
}

shinyApp(ui, server)

image

As can be seen, both the filter for cyl == 8, the search for merc as well as the arrange by ascending wt is taken into account. Eg

mtcars[c(13, 14, 12), ]
#>                      mpg cyl   disp  hp  drat   wt qsec vs am gear carb
#> Merc 450SL   17.3   8 275.8 180 3.07 3.73 17.6  0    0       3      3
#> Merc 450SLC 15.2   8 275.8 180 3.07 3.78 18.0  0    0       3      3
#> Merc 450SE   16.4   8 275.8 180 3.07 4.07 17.4  0    0       3      3

First try at making this available in reactable::getReactableState()

I tried to update the Reactable.js (L1415-1421) file to add the row indices and also added it to R/shiny.R (L263) but I couldn't get it to properly run. Maybe someone with more experience with reactable is able to do it.

Let me know if that helps you or if you need more information.

DavZim avatar Sep 29 '22 13:09 DavZim