Reputation: 587
If you render the below in an rmarkdown document, colors appear in the color column just fine when you output to DOCX.
In a quarto document however the background colors do not display.
I cannot tell if this is a bug in my code, or if it is something with gt()
package.
library(tidyverse)
library(gt)
df_text <- tibble::tibble(index = c("C-1", "C-2"),
finding = c("A finding.", "Another finding."),
color = c("red", "blue"))
df_text %>%
gt::gt() %>%
gt::tab_style(
style = gt::cell_fill(color = "red"),
locations = gt::cells_body(
column = color,
rows = stringr::str_detect(color, "red")
)
) %>%
gt::tab_style(
style = gt::cell_fill(color = "blue"),
locations = gt::cells_body(
column = color,
rows = stringr::str_detect(color, "blue")
)
)
Upvotes: 0
Views: 245
Reputation: 3677
So, you can do it in this way.
Save the gt table in your favourite image format and after simply include:
An example:
---
format: docx
---
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(tidyverse)
library(gt)
df_text <- tibble::tibble(index = c("C-1", "C-2"),
finding = c("A finding.", "Another finding."),
color = c("red", "blue"))
df_text %>%
gt::gt() %>%
gt::tab_style(
style = gt::cell_fill(color = "red"),
locations = gt::cells_body(
column = color,
rows = stringr::str_detect(color, "red")
)
) %>%
gt::tab_style(
style = gt::cell_fill(color = "blue"),
locations = gt::cells_body(
column = color,
rows = stringr::str_detect(color, "blue")
)
) %>%
gtsave("tab_to_word.png")
```
![]("tab_to_word.png")
Output:
An addition:
If one method doesn't work - try another one. flextable
---
format: docx
---
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(flextable)
library(tidyverse)
df_text <- tibble::tibble(index = c("C-1", "C-2"),
finding = c("A finding.", "Another finding."),
color = c("red", "blue"))
ft_1 <- flextable(df_text) %>%
autofit()
ft_1 <- bg(ft_1, i = 1, j = 3, bg="red")
ft_1 <- bg(ft_1, i = 2, j = 3, bg="blue")
ft_1
```
Now this table can be edited.
Upvotes: 2