Search code examples
rgt

conditional style of gt summary row in R


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")
                    )
          )

Solution

  • this answer is a bit hacky and manual but it works. first you save your table as an object. then you can directly access the table styles and manually modify them.

    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"),
        rows = "my_sum" )
      ) -> t1
    
    
    x1 <- t1$`_styles`$styles[4]
    
    x2 <- t1$`_styles`$styles[3]
    x2[[1]]$cell_fill$color <- "#FFFFFF"
    
    t1$`_styles`$styles[3] <- x1
    t1$`_styles`$styles[4] <- x2
    
    t1$`_styles`$styles[6] <- x1
    t1$`_styles`$styles[5] <- x2