Embed Rmarkdown with Rmarkdown, without knitr evaluation
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)`.")
```
Not sure about the pdf output, but surrounding your demo rmarkdown with:
<pre>
...
</pre>
seems to work for html.
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.