glin / reactable

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

tidyjs JS formatter #193

Open zac-garland opened 3 years ago

zac-garland commented 3 years ago

Hi @glin, have you seen tidyjs?

I recently stumbled on it, and think it could be cool to integrate portions of it (or all) for easier accessibility in using a custom JS style format'er.

It's likely ill take a look and create a PR if I can find a good way to integrate; was curious to gauge initial thoughts, ideas, and any practical considerations to keep in mind.

glin commented 3 years ago

I have seen tidy.js before, but never thought about using it with reactable. Interesting, I do see how it could be useful for custom formatters or aggregate functions in reactable, especially the summarizers. Probably not all of it would be applicable though - tidy.js generally operates on data frames, whereas column formatters/aggregators operate more on columns. For example, grouping and filtering is already handled by reactable, so you only need to provide a summarizer for column values. But if you did want to summarize data across multiple columns, I could definitely see tidy.js being useful there.

For integration with reactable, I think it would be best to have it as a separate package that could be included optionally, but still be easy to call. For example, something like this (probably non-working code):

library(reactable)
library(tidyjs)

reactable(
  data,
  columns = list(
    col = colDef(
      # Calculate the variance of the column. reactable doesn't have a variance
      # aggregate function, so this saves you the pain of implementing it yourself
      aggregate = JS("values => Tidy.variance(values)")
    ),
    col2 = colDef(
      # Or summarize over the entire data frame
      aggregate = JS("(values, rows) => {
        return Tidy.tidy(
          rows,
          Tidy.groupBy('other-col', [
            Tidy.summarize(...)
          ])
        )
      ")
    )
  )
)

A separate package would make it easy to use in other contexts as well, like in Shiny apps or other HTML widgets that let you run custom JS. This may already exist, from a quick search: https://github.com/gadenbuie/tidyjs-r. If this works with reactable, examples would be great to have for documentation.

On a related note, I think R has been missing a package to make math and data manipulation easier to do in JavaScript for a while. tidy.js might help with that. Other libraries like Math.js (which I think someone brought up before) also look interesting.

zac-garland commented 3 years ago

@glin I think thats a great idea. I'll test out this workflow over the coming months and will post any interesting use cases if I find some