In Quarto how can I show inline R code that is part of a YAML header

88 Views Asked by At

I am writing a tutorial in Quarto and I want to point out that you can use inline R code in the YAML header. I want my students to see this:

You can automatically insert today's date with date: "`r Sys.Date()`"

Because the YAML is not using {r}, I can't find a way to tell knitr to not evaluate the Sys.Date() function. Is there a way to display that code in the sentence without having to imbed an image?

2

There are 2 best solutions below

1
shafee On BEST ANSWER

One trick could be using the unicode representation \u0060 of backticks.

---
title: Showing Inline R code
engine: knitr
---

Insert today's date with ```date: "`r "\u0060r Sys.Date()\u0060" `" ```

Escaped inline R code

Explanation: Knitr will evaluate the expression "\u0060r Sys.Date()\u0060" in the inline R code and generate a markdown file containing,

---
title: Showing Inline R code
engine: knitr
---

Insert today's date with ```date: "`r Sys.Date()`" ```

which will be passed to pandoc then and pandoc will produce the expected output then.

1
Till On

You can enclose the YAML with the inline R expression in double tick marks ``. You can also enclose it in three tickmarks to get in a separate line.

The code for the Quarto docs are on github and you can check out there how they do it.

Note: Inline code expressions in quarto are now using curly brackets around the language name. For R it still works without, for any other language curly brackets are needed.

---
format: html
editor: source
---

You can automatically insert today's date with by including
the following in your YAML header: ``date: `{r} Sys.Date()` ``

OR

You can automatically insert today's date with by including
the following in your YAML header:

```
---
date: `{r} Sys.Date()`
---
```