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

Aggregating dates? #130

Closed
algo-se opened this issue Mar 4, 2021 · 3 comments
Closed

Aggregating dates? #130

algo-se opened this issue Mar 4, 2021 · 3 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@algo-se
Copy link

algo-se commented Mar 4, 2021

I have not found info regarding this, excuse me if it´s duplicate.

Is it posible to aggregate dates? The built-in aggregate functions do not work, but maybe it can be done with JS?

REPREX:

data <- data.frame(
  group = c("A", "A", "B", "B", "B"),
  percent = c(0.95, 0.5, 0.11, 0.12, 0.23),
  date = as.Date(c("2019-01-02", "2019-03-15", "2019-09-22", "2019-09-22", "2020-09-22"))
)

library(reactable)
reactable(data,
          groupBy = "group",
          columns = list(
            percent = colDef(aggregate = "max"),
            date = colDef(aggregate = "max") # this does not work
          ))

image

@glin
Copy link
Owner

glin commented Mar 7, 2021

It is possible to aggregate dates, but the built-in min and max aggregate functions only work on numbers. So for now, you'll have to write a custom aggregate function for dates. Here's an example of this you can adapt:

library(reactable)

data <- data.frame(
  group = c("A", "A", "B", "B", "B"),
  percent = c(0.95, 0.5, 0.11, 0.12, 0.23),
  date = as.Date(c("2019-01-02", "2019-03-15", "2019-09-22", "2019-09-22", "2020-09-22"))
)

# Aggregate function that gets the max of an array of date strings
maxDate <- JS("function maxDate(values) {
  const dates = values.map(function (v) {
    // Format YYYY-MM-DD dates in local time, not UTC.
    // Ignore ISO 8601 dates otherwise, i.e., YYYY-MM-DDTHH:MM:SS[Z]
    // http://blog.dygraphs.com/2012/03/javascript-and-dates-what-mess.html
    if (v.includes('-') && !v.includes('T') && !v.includes('Z')) {
      v = v.replace(/-/g, '/')
    }
    return new Date(v)
  })
  const date = new Date(Math.max.apply(null, dates))
  // Format as YYYY-MM-DD
  return [
    date.getFullYear(),
    (date.getMonth() + 1).toString().padStart(2, '0'),
    date.getDate().toString().padStart(2, '0')
  ].join('-')
}")

reactable(
  data,
  groupBy = "group",
  columns = list(
    percent = colDef(aggregate = "max"),
    date = colDef(aggregate = maxDate)
  )
)

reactable output

I'll probably add some variant of this example to reactable itself. I think it'd be a nice addition, especially since it's really painful to work with dates in JavaScript. The only reason why min and max don't support dates is just that I hadn't thought of it before.

@glin glin added the enhancement New feature or request label Mar 7, 2021
@sctyner
Copy link

sctyner commented Mar 9, 2021

I'm in a situation where I want to aggregate start/end times in a process. I would love a min/max datetime aggregate function. Thank you for this example.

@glin
Copy link
Owner

glin commented May 15, 2021

The min and max aggregate functions now work on dates and date-times in the development version (3d5f143). It turned out to be much simpler than the ugly example I came up with earlier 😅, thanks for opening this.

Here's what the original example code should produce now:

library(reactable)

data <- data.frame(
  group = c("A", "A", "B", "B", "B"),
  percent = c(0.95, 0.5, 0.11, 0.12, 0.23),
  date = as.Date(c("2019-01-02", "2019-03-15", "2019-09-22", "2019-09-22", "2020-09-22"))
)

reactable(
  data,
  groupBy = "group",
  columns = list(
    percent = colDef(aggregate = "max"),
    date = colDef(aggregate = "max")
  )
)

reactable output

@glin glin closed this as completed May 15, 2021
@glin glin added the bug Something isn't working label May 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants