Search code examples
rgt

change font color conditionally in multiple columns using gt()


This question is related to this one: How can I color the same value in the same color in the entire gt table in R?

Basically the OP asks to change the font color in an gt object conditionally:

if value == 4 -> font blue if value == 0 -> font red

It turned out that it is not as easy as I thought. I managed to change the colors in specific columns like:

library(gt)
library(dplyr)

mtcars %>% 
  gt() %>% 
  tab_style(
    style = cell_text(color = "red", weight = "bold"),
    locations = cells_body(
      columns = am,
      rows = am == 0
    )
  ) %>% 
  tab_style(
    style = cell_text(color = "blue", weight = "bold"),
    locations = cells_body(
      columns = cyl,
      rows = cyl == 4
    )
  )

which gives: enter image description here

My question: How can I modify my code to apply these condition to all columns!

e.g all 0 are red and all 4 are blue!


Solution

  • If we want only to do this on particular columns, create a vector of names ('nm1') and loop over only those columns, within the loop, get the index that meets the condition in rows

    library(dplyr)
    library(gt)
     tbl1 <- mtcars %>% 
         gt()
     nm1 <- c("cyl", "vs", "am", "gear", "carb")
     for(i in seq_along(nm1)) {
          
          tbl1 <- tbl1 %>%
            tab_style(
              style = list(
                cell_text(color = "red", weight = "bold")
                
              ),
              locations = cells_body(
                columns = nm1[i],
                
                rows = tbl1$`_data`[[nm1[i]]] == 0 
              )
            ) %>%
            
            tab_style(
              style = list(
                cell_text(color = "blue", weight = "bold")
                
              ),
              locations = cells_body(
                columns = nm1[i],
                
                rows = tbl1$`_data`[[nm1[i]]] == 4 
              )
            ) 
            
        }
    

    -output

    enter image description here


    Another option would be to create the gt object in each column using across, store as_raw_html and then call the gt on top of the output with fmt_markdown

    out <-  mtcars %>%
        summarise(across(everything(),  ~ 
                              setNames(tibble(.x), cur_column()) %>%
                                      gt() %>%
                                     tab_style(
                                       style = cell_text(color = "red", weight = "bold"),
                                       locations = cells_body(
                                         columns = cur_column(),
                                         rows = .x == 0
                                       )
                                     ) %>% 
                                     tab_style(
                                       style = cell_text(color = "blue", weight = "bold"),
                                       locations = cells_body(
                                         columns = cur_column(),
                                         rows = .x == 4
                                       )
                                     ) %>%
                           as_raw_html()
                                              
        ))
     
    out1 <- out %>%
         gt() %>%
       fmt_markdown(columns = everything())
    out1