Conditional formatting with column_spec within a dplyr chain

Thanks @cderv, for inviting me!

Hi, @joseepoirier, the . symbol in pipes always refers to the last object before the current pipe symbol. For example, in

mtcars %>% summarise(mean = mean(mpg)) %>% mutate(new = .$mean)

. means the new data frame after summarise instead of mtcars. That says, in this case, we won't be able to use . to refer to either mtcars[1:8, 1:8] or the arranged version because column_spec is not directly connected to either of those.

The solution is quite simple: save the final data before you create your table. Well, I understand the joy of piping from top to bottom. However, in cases like this one, it's just easier and cleaner to break them into two pieces.

mydt <- mtcars[1:8, 1:8] %>%

kbl(mydt, booktabs = T, linesep = "", escape=FALSE) %>% 
    kable_paper(full_width = F) %>%
    column_spec(6, color = ifelse(mydt$drat > 3, "red", "green"))