10

In this feature request back in 2016 Yihui shows, at the bottom, how to have different code chunks have different backgrounds in R Markdown. I've attached a screenshot of this here. Is there a way to do this with xaringan?

rmd-solution

I found this answer, which works for changing the background color for all code chunks, but I can't figure out how to modify the css so I can change the background for just a few specific chunks.

The goal here is to have most chunks show up normally, but a few show up with a red background or similar to signify "This is bad". Any help would be appreciated.

Daniel Anderson
  • 2,394
  • 13
  • 26

1 Answers1

13

This was actually something I was trying to work out a few days ago.

Here's one workaround.
First in your css file or either place the following in xaringan Rmarkdown file:

```{css, echo=F}
.code-bg-red .remark-code, .code-bg-red .remark-code * {
 background-color:red!important;
}
```

and then wrap the code chunk like

.code-bg-red[
```{r}
lm(speed ~ dist, cars)
```
]

then your output will be: red chunk output

You can, of course, change the red to other colors to suit your needs.

Emi
  • 3,514
  • 8
  • 11