change color of output for a single R code chunk from a Quarto document

I have the following code chunk in a qmd file which is a chapter in a Quarto book.

```{r}
#| echo: false

message("Error in library(tidyverse) : there is no package called ‘tidyverse’", )
```

When the chapter is rendered, this message appears in black. I would like it to appear in red, since that would be the color a student sees when she actually encounters this error.

How can I change the color outputed from a single R code chunk?

See 5.1 Font color | R Markdown Cookbook

\textcolor{red}{message("Error in library(tidyverse) : there is no package called ‘tidyverse’", )}

seems to work for PDF output but not in a code chunk.

Thanks for the reply! That works for pdf output, as you note. But it does not work for html output, which is my use case. (Apologies for not specifying that before.) But the link you kindly provide gives me the answer:

<span style="color: red;">Error in library(tidyverse) : there is no package called 'tidyverse'</span>

Thanks!

Glad to help. We Kanes need to stick together.
JKane.

Just to add that I believe solutions working with knitr in R with rmarkdown should work with Quarto.

See about fansi which has specific hooks for messages, warnings and errors look at vignette: ANSI CSI SGR Sequences in Rmarkdown

and that allow to use ANSI coloring in HTML document.

Anyhow, it is an alternative. Above documentation shared is the right one.

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.