glin / reactable

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

Can't include footer on a grouped/aggregate table #305

Closed daranzolin closed 1 year ago

daranzolin commented 1 year ago

I was hoping to include summary rows on a grouped/aggregate table, a la:

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

reactable(
  data,
  groupBy = "Type",
  columns = list(
    Price = colDef(aggregate = "max"),
    MPG.city = colDef(aggregate = "mean", format = colFormat(digits = 1)),
    DriveTrain = colDef(aggregate = "unique"),
    Man.trans.avail = colDef(aggregate = "frequency"),
    Type = colDef(
      html = TRUE,
      footer = JS("<b>Total</b>")
    )
  )
)

But this example fails with the error Uncaught SyntaxError: Unexpected token '<' in my browser console.

Is it possible to have summary rows here?

glin commented 1 year ago

Yes, the problem is that in the footer value, <b>Total</b> isn't JavaScript but really HTML, so the browser fails to parse it as JavaScript. It should work if you remove JS() (which marks it as a JavaScript expression) and use a plain character string:

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

reactable(
  data,
  groupBy = "Type",
  columns = list(
    Price = colDef(aggregate = "max"),
    MPG.city = colDef(aggregate = "mean", format = colFormat(digits = 1)),
    DriveTrain = colDef(aggregate = "unique"),
    Man.trans.avail = colDef(aggregate = "frequency"),
    Type = colDef(
      html = TRUE,
      footer = "<b>Total</b>"
    )
  )
)
daranzolin commented 1 year ago

Ah, makes sense, thank you!