Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to have a certain code chunk highlighted in a different color (e.g. red) to indicate that it is bad practice. If I was using .Rnw, I could add the chunk option background = 'red' and get what I want, but this does not seem to work in .Rmd. My guess is that I need to make a custom css stylesheet (though what the selector would be, I don't know), and maybe also create a custom hook. I'd like it to be on a per-chunk basis, not an overall change for the entire document.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
744 views
Welcome To Ask or Share your Answers For Others

1 Answer

We can use the class.source option in the code chunk header to provide custom CSS to R Markdown. This is explained in the following post:

Add a CSS class to single code chunks in RMarkdown

Putting together an example, I might set a class called "badCode" then have a bit of CSS to change the background as you might like. Here's my .Rmd:

---
output: html_document
---

```{css}
.badCode {
background-color: red;
}
```

```{r mtcars}
summary(mtcars)
```

```{r cars, class.source="badCode"}
summary(cars)
```

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...