Embed Rmarkdown with Rmarkdown, without knitr evaluation
Asked Answered
D

5

17

I want to demonstrate how to write RMarkdown, where said RMarkdown demonstration is embedded within an RMarkdown document used to create the course material. Within this fenced code block, I don't want knitr to execute the chunk.

I want to put something like this into my "top level" Rmarkdown document, and have everything that's between the outer fences be printed verbatim in fixed width in the output HTML document, rather than having knitr evaluate the inner embedded R code chunk and inline code.

```
---
title: "RMarkdown teaching demo"
author: "whoever"
---

# Major heading

Here's some text in your RMarkdown document. Here's a code chunk:

```{r, eval=FALSE}
head(mtcars)
```

Now we're back into regular markdown in our embedded document.

Here's inline code that I don't want executed either; 
e.g. mean of mpg is `r mean(mtcars$mpg)`.

```

I've tried the zero-width space trick in knitr example 65, but this fails when trying to compile to PDF (I need both HTML and PDF).

Densmore answered 17/2, 2016 at 14:17 Comment(1)
In 2022, knitr introduced the verbatim engine, see hereMidpoint
H
18

Here is one way to achieve it. You may add `r ''` before the chunk header so that the code chunk is not recognized, and use knitr::inline_expr() to generate `r `.

````
---
title: "RMarkdown teaching demo"
author: "whoever"
---

# Major heading

Here's some text in your RMarkdown document. Here's a code chunk:

`r ''````{r, eval=FALSE}
head(mtcars)
```

Now we're back into regular markdown in our embedded document.

Here's inline code that I don't want executed either; 
e.g. mean of mpg is `r knitr::inline_expr('mean(mtcars$mpg)')`.

````

It will be easier if you just save the R Markdown example document in a separate file, and include it in the top-level document via readLines(), e.g.

````
`r paste(readLines('example.Rmd'), collapse = '\n')`
````

To include three backticks in a fenced code block, you need more than three backticks. That is why I'm using four here.

Humism answered 18/2, 2016 at 4:46 Comment(3)
That works, only thing I would change is closing the bottom fence the same way, with `r ''````. Otherwise, the syntax highlighting in RStudio gets out of whack.Densmore
Right. That would help RStudio with syntax highlighting, although it is not necessary for knitr to work.Humism
For me (rmarkdown_1.0) the inline piece required additional escaping for backticks to appear.Hippel
H
5

I do this using the cat function, which works for both HTML and PDF output.

---
title: "RMarkdown teaching demo"
author: "whoever"
---

# Major heading

Here's some text in your R Markdown document. Here's a code chunk:

```{r, echo=FALSE, comment=""}
cat(c("```{r, eval=FALSE}",
      "head(mtcars)",
      "```"), 
    sep='\n')
```

Now we're back into regular Markdown in our embedded document.

Here's inline code that I don't want executed either: 

```{r, echo=FALSE, comment=""}
cat("The mean of mpg is `r mean(mtcars$mpg)`.")
```
Herl answered 20/2, 2016 at 16:40 Comment(0)
O
3

Not sure about the pdf output, but surrounding your demo rmarkdown with:

<pre>
...
</pre>

seems to work for html.

Oina answered 17/2, 2016 at 14:22 Comment(0)
A
1

A simple solution that works with R Markdown 2.1 involves using spaces and escape ticks:

````    ```{r, eval=FALSE}    ````  
```head(mtcars)```  
````    ```    ````

Note that each line is escaped separately. There are four spaces between the four tick marks and three tick marks. And there are two spaces after the last tick marks on the first and second lines to insert a line break.

Appaloosa answered 1/2, 2020 at 20:45 Comment(0)
M
0

As of 2022-06-16, knitr v1.37, the recommended approach is to use the verbatim engine, ie.

````{verbatim}
```{r}
head(mtcars)
```
````
Midpoint answered 11/4, 2024 at 6:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.