Search code examples
rknitrquarto

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


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?


Solution

  • 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.