Typically we write code chunks and inline expressions that we want to be parsed and evaluated by knitr. However, if you are trying to write a tutorial on using knitr, you may need to generate a verbatim code chunk or inline expression that is not parsed by knitr, and we want to display the content of the chunk header.
The verbatim
engine can take any R Markdown content and output it as-is in the resulting document. By using more backticks on the outer fences than the backticks in the inner content, a whole code chunk including the chunk header can be included in a verbatim
chunk. For example,
````{verbatim}
```{r, eval=TRUE}
1 + 1
```
````
will be rendered as:
```{r, eval=TRUE}
1 + 1
```
in the output. You have to use at least N+1
backticks to wrap up N
backticks. We are using four backticks here for the verbatim
chunk because the inner chunk header contains three backticks.
The content of the verbatim
chunk will be placed in a fenced code block with the class default
, meaning no syntax highlighting will be applied. You can set the lang
chunk option to use a different syntax highlighting language name. For example,
````{verbatim, lang = "markdown"}
We can output arbitrary content **verbatim**.
```{r}
1 + 1
```
The content can contain inline code like
`r pi * 5^2`, too.
````
will result in the Markdown source file as
````markdown
We can output arbitrary content **verbatim**.
```{r}
1 + 1
```
The content can contain inline code like
`r pi * 5^2`, too.
````
The verbatim content will be syntax highlighted using the markdown
language in the output.
If the content you want to include verbatim is in a file, you can use the embed
engine instead. Here is an example of the CSS code from Section 9.6.2:
.blackbox {
padding: 1em;
background: black;
color: white;
border: 2px solid orange;
border-radius: 10px;
}
.center {
text-align: center;
}
The syntax for the above inclusion in our Rmd source file is
```{embed, file = "css/box.css"}
```
Another syntax for it is to pass the file as chunk body:
```{embed}
"css/box.css"
```
The embed
engine is based on the verbatim
engine. By default, the lang
option is set based on the extension of the file you include. It can be modified using lang
directly as chunk option.
Show a verbatim inline expression
There are multiple ways to show a verbatim inline expression. The first way is to break the inline expression after `r
, e.g.,
This will show a verbatim inline R expression `` `r
1+1` `` in the output.
In the output document, you should see:
This will show a verbatim inline R expression
`r 1+1`
in the output.
The trick works for two reasons: (1) a single line break is often the same as a space to Markdown parsers (by comparison, two consecutive line breaks means starting a new paragraph); (2) knitr requires a space after `r
to parse it; if the space is missing, it will not be treated as an inline expression.
Another way to show a verbatim inline R expression is to wrap the R code in knitr::inline_expr()
, e.g.,
This will show a verbatim inline R expression
`` `r knitr::inline_expr("1+1")` `` in the output.
I’d recommend the second way, because the first way is more or less a hack taking advantage of the Markdown syntax and knitr’s parser.