How to add totals rows to reactable table after every seven days in R?

64 Views Asked by At

I'd like to add some rows to my reactable table which show the column totals or averages over a 7-day period. In my real data, I have both a date (see df$date below) and a day (df$day) column, and I'd like to calculate the total or average (depending on the variable) for a Monday-Sunday period. Issue is, I can't just add these "totals" rows after every 7 rows in my data as the number of entries per Monday-Sunday will vary. For example, there's three entries for one 7-day period below and four for another. I'd like to get the total for some columns, such as df$var1 and the average for others df$var2 (my real data has more columns compared to the example below). Essentially, I'd like my reactable table to look something like this...

enter image description here

It would also be preferable to have these "totals" rows in bold text with the cell backgrounds filled with a different colour to stand out, such as light red. Note that my example image below takes the total for df$var1 and the average for df$var2. My example code is below, so any assistance tackling this will be appreciated.

library(reactable)

set.seed(10)
df <- data.frame(
  date = c("09/09/2023", "07/09/2023", "05/09/2023", "02/09/2023",
            "31/08/2023", "29/08/2023", "28/03/2023"),
  day = c("Saturday", "Thursday", "Tuesday", "Saturday", "Thursday",
          "Tuesday", "Monday"),
  var1 = round(rnorm(7, 5000, 1000)),
  var2 = round(rnorm(7, 1000, 500))
  )

reactable(df,
          striped = TRUE,
          defaultColDef = colDef(
            align = "center"),
          columns = list(
            day = colDef(show = FALSE)
            )
          )
0

There are 0 best solutions below