rknitrgthtml-pdf

Losing colors when knitting a gt table to PDF


When I run the following code manually, in my Rmd file, I end up with a (correct) colored table:

color_table <- c("darkgoldenrod2","darkslategray3", "darkseagreen3",  "lightpink2")
job <- c("job1", "job2", "job3","job4")

prof_table <- data.frame(job) %>%
  gt()

for (i in 1:length(color_table)) {
  prof_table <- tab_style(
       data=prof_table,
       style = list(
          cell_fill(color = color_table[i])),
          locations = cells_body(columns = 1, rows = i)
       )
}

prof_table

However when i knit it, the colors are gone. I am guessing that this might have to do with the knit going to PDF and the table being HTML. But I am not sure, and not aware of a fix. Does anyone have a solution for me?


Solution

  • This is a known limitation, as the documentation states that tab_style() will only work for HTML output. Here is the relevant excerpt:

    At present this function is focused on the application of styles for HTML output only (as such, other output formats will ignore all tab_style() calls).