We can customize the style of code chunks and their text output using the chunk options class.source and class.output, respectively. These options take character vectors of class names (see Section 11.13 for more information). For example, when class.source = "important", the HTML element containing the code chunk in the output will have a class important. Then you can define CSS rules for this class.11 This can be useful when you want to highlight a certain code chunk or its text output.

By default, the HTML output of R Markdown includes the Bootstrap framework, which makes it easy for you to change the appearance of your code and output, because Bootstrap has predefined some CSS classes for backgrounds: "bg-primary", "bg-success", "bg-info", "bg-warning", and "bg-danger".

Below is an example using the chunk options class.source = "bg-danger" and class.output = "bg-warning", and you can see its output in Figure 7.1.

  1. ---
  2. title: Change the chunk style
  3. output: html_document
  4. ---
  5. When you subset a data frame, it does not necessarily return
  6. a data frame. For example, if you subset two columns, you get
  7. a data frame, but when you try to subset one column, you get
  8. a vector:
  9. ```{r class.source="bg-danger", class.output="bg-warning"}
  10. mtcars[1:5, "mpg"]
  11. ```
  12. To make sure that we always get a data frame, we have to use
  13. the argument `drop = FALSE`. Now we use the chunk option
  14. `class.source = "bg-success"`.
  15. ```{r df-drop-ok, class.source="bg-success"}
  16. mtcars[1:5, "mpg", drop = FALSE]
  17. ```

Image 1: A code chunk and its text output with background colors defined by Bootstrap.

FIGURE 7.1: A code chunk and its text output with background colors defined by Bootstrap.

You can also use arbitrary class names and define CSS rules accordingly. In this case, you will have to include your custom CSS rules using the methods mentioned in Section 7.1. Below is an example:

  1. ---
  2. title: Assign custom classes to chunks
  3. output: html_document
  4. ---
  5. First we define some CSS rules for a class `watch-out`.
  6. ```{css, echo=FALSE}
  7. .watch-out {
  8. background-color: lightpink;
  9. border: 3px solid red;
  10. font-weight: bold;
  11. }
  12. ```
  13. Then we assign a class `watch-out` to the code chunk via the
  14. chunk option `class.source`.
  15. ```{r class.source="watch-out"}
  16. mtcars[1:5, "mpg"]
  17. ```

The output style is shown in Figure 7.2.

Image 2: A code chunk with a light pink background color and a thick red border.

FIGURE 7.2: A code chunk with a light pink background color and a thick red border.

If you want all code blocks in the document to use the custom style, you can set class.source in the global knitr options, e.g.,

  1. knitr::opts_chunk$set(class.source = "watch-out")

You can apply multiple classes to the code blocks. For example, with class.source = c("important", "warning"), the code block will have two classes, “important” and “warning.”

If you want to decorate individual elements in code blocks instead of the whole blocks, you may consider using the flair package (Bodwin and Glanz 2024). With this package, you can highlight different parts of your code (such as fixed strings, function names, and arguments) with custom styles (e.g., color, font size, and/or font weight).

References

Bodwin, Kelly, and Hunter Glanz. 2024. Flair: Highlight, Annotate, and Format Your r Source Code. https://github.com/r-for-educators/flair.

11. In this case, the rule would begin .important because in CSS, classes are prefixed with a period (.).