Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed Rmarkdown with Rmarkdown, without knitr evaluation

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

like image 637
Stephen Turner Avatar asked Feb 17 '16 14:02

Stephen Turner


People also ask

How do I embed code into R Markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).

How do I get R Markdown to show no results?

You use results="hide" to hide the results/output (but here the code would still be displayed). You use include=FALSE to have the chunk evaluated, but neither the code nor its output displayed.

Does R Markdown use Pandoc?

A recent version of Pandoc (>= 1.12. 3) is required to use the rmarkdown package. RStudio also automatically includes this so you do not need to download Pandoc if you plan to use rmarkdown from the RStudio IDE. If not using the RStudio IDE, you'll need to install Pandoc for your platform.

What is the purpose of knitr?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.


2 Answers

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.

like image 172
Yihui Xie Avatar answered Oct 07 '22 05:10

Yihui Xie


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)`.")
```
like image 38
John S Avatar answered Oct 07 '22 07:10

John S