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

Add first/last as one of the built-in aggregate functions for colDef #387

Open
C92Anderson opened this issue Aug 1, 2024 · 2 comments
Open
Labels
enhancement New feature or request

Comments

@C92Anderson
Copy link

Hi @glin,

Wondering if it would be possible to add last() and first() as a built-in aggregate function for colDef()? I'm not sure if the custom JS (which I'm not great with) can work with defaultSorted to make this happen so I'm making a request - thanks!

@glin glin added the enhancement New feature or request label Aug 5, 2024
@glin
Copy link
Owner

glin commented Aug 5, 2024

Sure, that seems reasonable. In JS, it would also be relatively straightforward to implement, so a custom aggregate function solution could look like this:

data <- MASS::Cars93[14:38, c("Type", "Price", "MPG.city", "DriveTrain", "Man.trans.avail")]

agg_first <- JS("values => values[0]")
agg_last <- JS("values => values[values.length - 1]")

reactable(
  data,
  groupBy = "Type",
  columns = list(
    Price = colDef(aggregate = agg_first),
    DriveTrain = colDef(aggregate = agg_last)
  )
)

But also just to clarify, these would return the first and last elements of the group before sorting, so sorting won't change the results. If you did need the first and last elements after sorting, that'd be more difficult as grouping/aggregation is currently done before sorting. I'm not sure how feasible it would be to enable a sorting-aware aggregation.

@C92Anderson
Copy link
Author

C92Anderson commented Aug 6, 2024 via email

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