1
votes

I am new to Rmarkdown and trying to generate a table that has some conditional text formatting. The table comes out as what I want except for the color. It displays \textcolor{red}{50} for example instead of 50 in red. Any idea what I'm doing wrong?

In my R script:

 table2 <- data %>% filter(student == 1) %>%
           dplyr::mutate(grade = cell_spec(grade, "latex", color = ifelse(grade > 75, "blue", "red"))  

In my Rmarkdown file:

 ```{r, results = "asis"}
 kable(table2, format = "latex")
 ```
1
I've figured out how to solve my problem. For some reason, if I add use ```{r kable, results = "asis"} kable(table2, format = "latex") my code works as I intended it to.imprela

1 Answers

3
votes

Your question isn't reproducible, but it appears the issue is that you didn't include escape = FALSE in the call to kable:

library(dplyr)
library(kableExtra)
data <- data.frame(student = rep(1:5, 2), grade = sample(100, 10))
table2 <- data %>% filter(student == 1) %>%
  dplyr::mutate(grade = cell_spec(grade, "latex", 
                                  color = ifelse(grade > 75, "blue", "red"))) 
kable(table2, format = "latex", escape = FALSE)