1

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?

shafee
  • 15,566
  • 3
  • 19
  • 47
Douwe
  • 112
  • 10
  • While not really part of the question, it seems like `prof_table<-tab_options(data=prof_table,table.width = px(5))` also only works when manually running the code and not in the PDF – Douwe Oct 08 '22 at 19:38
  • 1
    keep an eye on this issue [`cell_fill` no longer works with `gtsave` pdf output in gt 0.7.0 #1041](https://github.com/rstudio/gt/issues/1041) – shafee Oct 09 '22 at 16:57

1 Answers1

2

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

mhovd
  • 3,724
  • 2
  • 21
  • 47
Gustavo P
  • 46
  • 2
  • I thought that this was not the case anymore (https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html). While it hasn't worked for me, I thought that gtsummary would help overcome this issue – Douwe Oct 10 '22 at 15:30
  • While gtsummary might be able to do what i wanted, I only now realize that it always will turn your table into a summary stats table. Which is probably obvious from the title. – Douwe Oct 23 '22 at 15:51