Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display YAML and chunks without executing them in blogdown

In an article made with blogdown, I would like to show the YAML and some chunks I used in a previous work. Therefore, I would like to display them without executing them, just as-is. I tried to embed the YAML and the chunks with 4 backticks but the execution of chunks still depends on their options. Here's an example:

---
title: ""
author: ''
date: ""
output:
  blogdown::html_page
---
  
Display a fake YAML and a fake chunk:
````
---
title: ""
author: ''
date: ""
output:
  pdf_document
---
```{r}
1 + 1
```
````

As you can see, the chunk containing 1+1 is executed. This is what should be displayed:

---
title: ""
author: ''
date: ""
output:
  pdf_document
---
```{r}
1 + 1
```

How can I do that? If it matters, the extension of my file is .Rmarkdown, and not .Rmd.

like image 784
bretauv Avatar asked Jun 28 '20 11:06

bretauv


People also ask

How do you not run a chunk in R markdown?

If you don't want any code chunks to run you can add eval = FALSE in your setup chunk with knitr::opts_chunk$set() . If you want only some chunks to run you can add eval = FALSE to only the chunk headers of those you don't want to run.

How do I hide results in R markdown?

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.

How do I show output in R markdown?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .

How do I not include code in R markdown?

include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.


1 Answers

I had a look in the source of the RMarkdown book. Besides the four backticks the trick is to put

`r ''`

in front of the code chunk. Try this:

---
title: "Untitled"
output: html_document
---

````markdown
---
title: ""
author: ''
date: ""
output:
  pdf_document
---
`r ''````{r}
1 + 1
```
````
like image 168
stefan Avatar answered Sep 23 '22 15:09

stefan