glin / reactable

Interactive data tables for R
https://glin.github.io/reactable
Other
632 stars 80 forks source link

Accept a vector of length of groups in `aggregate` #391

Open olivroy opened 2 months ago

olivroy commented 2 months ago

I am trying to add support for summary rows in gt and its wrapper for reactable (#1359). However, the aggregate argument is not flexible enough.

Since gt precomputes its summary values, I wonder if there is a way to just plug those in the aggregate argument.

library(gt)
library(reactable)
# # rstudio/gt#1359
gt <- exibble |>
  gt(groupname_col = "group") |>
  summary_rows(
    columns = num,
    fns = ~sum( .x ^ 2)
  )
sum <- extract_summary(gt)

exibble |> 
  reactable::reactable(
    groupBy = "group",
    columns = list(
      # create summary
      num = colDef(aggregate = "sum")
    )
  )

I wonder if there would be a way to pass values of the length of groups.

So that I could just plug the summary values that are computed in gt in the reactable call.

num_summary_vals <- extract_summary(gt)[[1]] |> purrr::map_dbl(~ .x$num)
exibble |> 
  reactable::reactable(
    groupBy = "group",
    columns = list(
      # create summary
      num = colDef(aggregate = num_summary_vals )
    )
  )

I'd be happy to contribute this to reactable with some guidance

glin commented 2 months ago

It's not impossible, but I think it'd be very difficult just because all the grouping is done on the JavaScript side right now. You'd need a way to associate an aggregate value with a group, or make assumptions about how the grouping is done in JS, e.g. what order the groups are in.

I think the best way to do this would be to let you define both the row groups and the aggregate values at the same time in R, so you'd have full control over both the grouping and aggregation. That would be similar to other common requests for a "tree table" feature, so linking https://github.com/glin/reactable/issues/391, https://github.com/glin/reactable/issues/379, https://github.com/glin/reactable/issues/112, https://github.com/glin/reactable/issues/147