I am trying to find out if there is a way to color specific text within a cell with the Gt package in R. I don't have a reproducible example since I haven't found any tutorials or examples of how to do this.
library(tidyverse)
library(gt)
type <- c("A", "B")
track <- c("G-P-L-H-H", "G-H-P-L-G")
table <- tibble(type, track) %>% gt()
For the table above, on the track column I'd like the font color of G = red, H = blue, L = green and P = yellow. is there a way to achieve this? This is a small representation of the real table I am working with, which will have many more rows with random combination of letters.
Does this achieve what you need? I replace, for example, "G" with G inside an html
tag with style corresponding to the needed color.
table <- tibble(type, track) %>%
mutate(
track = track %>%
str_replace_all("G", '<a style="color:red">G</a>') %>%
str_replace_all("H", '<a style="color:blue">H</a>') %>%
str_replace_all("L", '<a style="color:green">L</a>') %>%
str_replace_all("P", '<a style="color:yellow">P</a>')
) %>%
gt() %>%
fmt_markdown(columns = "track")