I have the following table.
I would like to put yellow highlight only in the cells in the "my_sum" row when the value is greater than 0. How can I do this for grouped data? I assume some type of function to recognize the grouping in the data put into the tab_style?
Here is my reprex
library(gt)
library(tidyverse)
tibble(cars = c("honda", "honda",
"ford", "ford"),
mpg = c(24, 22,
NA, NA),
wt = c(NA, NA,
3432, 4234)
) %>%
group_by(cars) %>%
gt() %>%
fmt_missing(columns = everything(),
missing_text = "") %>%
summary_rows(
groups = TRUE,
columns = c(mpg:wt),
fns = list("my_sum" = ~sum(., na.rm = TRUE)),
missing_text = "",
formatter = fmt_number,
decimals = 0
) %>%
grand_summary_rows(
columns = c(mpg:wt),
fns = list("my_big_sum" = ~sum(., na.rm = TRUE)),
missing_text = "",
formatter = fmt_number,
decimals = 0
) %>%
tab_options(
row_group.font.weight = "bold"
) %>%
tab_style(
style = list(
cell_fill(color = "#d4ebf2")
),
locations = cells_grand_summary(
columns = c("mpg", "wt")
)
) %>%
tab_style(
style = list(
cell_fill(color = "#FFFFE0")
),
locations = cells_summary(
columns = c("mpg", "wt")
)
)