How to let javascript calculate both grouped rows totals and subgroup totals?
This issue is related to this one.
I know I should try to find out myself, but what If I need to calculate the totals of both grouped rows and their subgroups. And additional to that the column totals in the footer. Maybe you can point me in the right direction?
This example will illustrate what I mean. Instead of the built in aggreation functions I want to use their equivalents in javascript (and with an addtional column total):
data <- data.frame(
State = state.name,
Region = state.region,
Division = state.division,
Area = state.area
)
reactable(
data,
groupBy = c("Region", "Division"),
columns = list(
Division = colDef(aggregate = "unique"),
Area = colDef(aggregate = "sum", format = colFormat(separators = TRUE))
),
bordered = TRUE
)
Many thanks in advance!
Do you mean the total of all individual values in the column? Here's a way to do that, based on the example I just added to https://github.com/glin/reactable/issues/50#issuecomment-650924550. The totals of all values in a row's subgroup will be available in the row data:
data <- data.frame(
State = state.name,
Region = state.region,
Division = state.division,
Area = state.area
)
reactable(
data,
groupBy = c("Region", "Division"),
columns = list(
Division = colDef(aggregate = "unique"),
Area = colDef(
aggregate = "sum",
format = colFormat(separators = TRUE),
footer = JS("function(colInfo) {
var total = 0
colInfo.data.forEach(function(row) {
total += row[colInfo.column.id]
})
// Use toLocaleString to add thousands separators
return total.toLocaleString()
}")
)
),
bordered = TRUE
)

If this isn't what you meant, could you provide an example of the expected output perhaps?
Just a side question: Would it be possible to remove the row counts (e.g in example above have South without the number 3.)
@amanthapar That's not possible today, but it likely will be in the future.