Skip to content

Instantly share code, notes, and snippets.

@jennybc
Created September 19, 2014 05:57
Show Gist options
  • Save jennybc/8add4496cbef43324c59 to your computer and use it in GitHub Desktop.
Save jennybc/8add4496cbef43324c59 to your computer and use it in GitHub Desktop.
How to get verbatim R chunks in R markdown. Again. Writing it down now.
---
title: "Get verbatim R chunks in R Markdown"
author: "Jenny Bryan"
date: "18 September, 2014"
output:
html_document:
keep_md: TRUE
---
My periodic revisitation of "how can I include a verbatim R chunk in `.rmd`"? This time I am writing it down! Various proposed solutions:
From the [`knitr` FAQ](https://github.com/yihui/knitr/blob/master/FAQ.md):
`r ''` ```{r label, what=ever}
From [issue 443](https://github.com/yihui/knitr/issues/443)
`r ''````{r foo}
print('hello world!')
```
From [`knitr` example 065](https://github.com/yihui/knitr-examples/blob/master/065-rmd-chunk.Rmd), involving inline R code:
```{r eval=TRUE}`r ''`
n = 10
rnorm(x)
```
Also from [`knitr` example 065](https://github.com/yihui/knitr-examples/blob/master/065-rmd-chunk.Rmd), involving a zero-width space:
`​``{r eval=TRUE}
n = 10
rnorm(n)
```
From [Karl Broman](https://gist.github.com/kbroman/ca560b967b0b7a08a3b2), involving `<pre>` and `<code>` tags:
<pre><code>```{r whatever}
data(cars)
summary(cars)
```</code></pre>

Get verbatim R chunks in R Markdown

Jenny Bryan
18 September, 2014

My periodic revisitation of "how can I include a verbatim R chunk in .rmd"? This time I am writing it down! Various proposed solutions:

From the knitr FAQ:

 ```{r label, what=ever}

From issue 443

```{r foo}
print('hello world!')
```

From knitr example 065, involving inline R code:

```{r eval=TRUE}
n = 10
rnorm(x)
```

Also from knitr example 065, involving a zero-width space:

`�``{r eval=TRUE}
n = 10
rnorm(n)
```

From Karl Broman, involving <pre> and <code> tags:

```{r whatever}
data(cars)
summary(cars)
```
@jennybc
Copy link
Author

jennybc commented Oct 15, 2014

@ramnathv
Copy link

ramnathv commented Nov 3, 2014

I wrote a blog post about this a while back. Let me know if you find it useful

https://ramnathv.github.io/posts/verbatim-chunks-knitr/

@jennybc
Copy link
Author

jennybc commented Nov 21, 2014

Thanks @ramnathv! I just noticed your comment and yes I am always happy to get more resources on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment