Changing chunk background color in RMarkdown

后端 未结 4 1555
一整个雨季
一整个雨季 2020-12-04 21:45

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

4条回答
  •  广开言路
    2020-12-04 22:48

    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)
    ```
    

提交回复
热议问题