Search code examples
rr-markdownkablekableextra

How can I color specific cells in R Markdown table depending on their values?


I want to color cells of a table in R Markdown depending on their value: Every cell with a value > 0 should be light blue and the cell with the largest value in the row should be a darker blue. At the moment I'm using the following code for the table:

kable(mydata) %>%
    kable_styling(bootstrap_options=c("hover", "condensed")) %>%
    column_spec(1, bold = T, border_right = T)

Does anyone know how I can color particular cells?


Solution

  • Have a look at the conditional Logic section in Cell/Text Specification unit on this page

    You will be able to modify cell something like this :

    cell_spec(df, color = ifelse(df> 20, "red", "blue"))