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.

1 Answer 1

13
+500

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.

1
  • 2
    Beautiful! Thanks Emi! I actually almost just messaged you on Twitter because I figured you’d know! You’re amazing! Oct 5, 2018 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.