Quantcast
Channel: Active questions tagged kable - Stack Overflow
Viewing all articles
Browse latest Browse all 105

Formatting kable cells with individual colors in RMarkdown and Quarto

$
0
0

I have a table with numeric data and want to assign individual colors, depending on a certain condition, e.g. negative cells in red and positive in blue.

This works well with kable(escape=FALSE):

library("dplyr")library("knitr")library("kableExtra")set.seed(123)highlight <- function(x) {  x <- ifelse(is.na(x), "", x)  ifelse(x < 0, cell_spec(x, color="red"), cell_spec(x, color="blue"))}matrix(sample(-4:4), nrow=3) |>  data.frame() |>  mutate(across(1:3, function(x) highlight(x))) |>  kable(escape=FALSE)

table shows colors

Additional styling

But after adding additional styling with kable_styling:

matrix(sample(-4:4), nrow=3) |>  data.frame() |>  mutate(across(1:3, function(x) highlight(x))) |>  kable(escape=FALSE) |>  kable_styling(font_size = 12)

... the printed table shows only raw HTML code instead of colors:

enter image description here

What has changed between versions?

With older versions of R, kable_styling works as expected. I tested it again right now with R version 3.6.3 using RMarkdown and dplyr pipes. However, with recent versions of R (4.3.2 or 4.3.3) and the recent set of packages, it prints only code, regardless if I use RMarkdown or Quarto, native or dplyr pipes, html or latex output.

So I wonder, what has changed between versions and if there is any workaround?


Viewing all articles
Browse latest Browse all 105

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>