Skip to content

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

Open

Description

@C92Anderson

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!

Activity

  1. glin commented on Aug 5, 2024

    @glin
    Owner

    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.

  2. C92Anderson commented on Aug 6, 2024

    @C92Anderson
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions