I am trying to hide the weights used in this reactable weighted average aggregation.
The table rolls up correctly without the show = FALSE
parameter:
reactable1
However, it does not when show = FALSE
is present:
reactable2
It only appears to correctly aggregate at the lowest grouping 'group_sub_sub'.
Attempt at a reproducible example:
library(reactable)
x <- data.frame(
group = sample(c("a","b","c"), 200, replace = T),
group_sub = sample(1:3, 200, replace = T),
group_sub_sub = sample(c("x","y","z"), 200, replace = T),
weight_to_hide = runif(200, 1000, 20000),
return = runif(200, 0, 0.25)
)
weighted_mean <-
function(weight) {
JS(
paste0(
"function(values, rows) {
var numerator = 0
var denominator = 0
rows.forEach(function(row, index) {
numerator += row['", weight, "'] * values[index]
denominator += row['", weight, "']
})
return (numerator / denominator || 0)
}"
)
)
}
reactable(
x,
groupBy = c("group",
"group_sub",
"group_sub_sub"),
columns = list(
return = colDef(
aggregate = weighted_mean(weight = "weight_to_hide"),
format = colFormat(percent = TRUE, digits = 2)
),
weight_to_hide = colDef(
aggregate = "sum",
show = FALSE,
format = colFormat(currency = "USD", separators = T)
)
)
)
Edit: The above was an attempt in Rstudio on Windows 11 locally. I have found the above code to work as expected when ran on a server version of Rstudio (ubuntu). Does anyone know why this is the case?