This blogpost is primarily written for my future self. I am notoriously forgettting how things are done. This is partly linked to me playing around with too many things at the same time or simply not having used a specific feature for a while. This page hopefully helps filling in as an aide memoire.
Displaying Code Chunks Without Executing Them
Code Chunk Blocks
Yihui provides examples for displaying verbatim code chunks. The trick is to destroy the header of the code chunk by inserting an what they call an empty string as inline r code. I rather say, insert two tick marks as inline code after the chunk header as shown here `r ''`
. These will create the empty string.
{r eval=TRUE} n = 10 rnorm(x)
In order to have the code chunks displayed as a block, intend the whole block by inserting 4 spaces.
```{r eval = TRUE}
n = 10
rnorm(x)
```
Inline R Code
If you want to display inline r code, c.f. above, Yihui’s FAQ #7 offers 2 ways of doing this:
- Single backticks can be escaped by double backticks. In order to trick R into not evaluating the inline code, insert a line break immediately after the first backtick and the folloing r
`r
. Note: if you check the Rmd of this post, you can see this is how I displayed the two tick marks as inline code above :). As an example, this might help`r 1 + 1 `
. The inline r code is shown verbatim without executing it. - Alternatively, knitr() offers an inline expression function, i.e.
knitr::inline_expr()
. With this you can write`r 1 + 1`
.