Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Markdown – a concise way to print all code snippets used in the document

Tags:

markdown

r

pandoc

I'm writing a report in R Markdown in which I don't want to print any of my R code in the main body of the report – I just want to show plots, calculate variables that I substitute into the text inline, and sometimes show a small amount of raw R output. Therefore, I write something like this:

In the following plot, we see that blah blah blah:
```{r snippetName, echo=F}
plot(df$x, df$y)
```

Now...

That's all well and good. But I would also like to provide the R code at the end of the document for anybody curious to see how it was produced. Right now I have to manually write something like this:

Here is snippet 1, and a description of what section of the report
this belongs to and how it's used:
```{r snippetName, eval=F}
```
Here is snippet 2:
```{r snippetTwoName, eval=F}
```
<!-- and so on for 20+ snippets -->

This gets rather tedious and error-prone once there are more than a few code snippets. Is there any way I could loop over the snippets and print them out automatically? I'm hoping I could do something like:

```{r snippetName, echo=F, comment="This is snippet 1:"}
# the code for this snippet
```

and somehow substitute the following result into the document at a specified point when it's knitted:

This is snippet 1:
```{r snippetName, eval=F}
```

I suppose I could write some post-processing code to scan through the .Rmd file, find all the snippets, and pull out the code with a regex or something (I seem to remember there's some kind of options file you can use to inject commands into the pandoc process?), but I'm hoping there might be something simpler.

Edit: This is definitely not a duplicate – if you read my question thoroughly, the last code block shows me doing exactly what the answer to the linked question suggests (with a slight difference in syntax, which could have been the source of the confusion?). I'm looking for a way to not have to write out that last code block manually for all 20+ snippets in the document.

like image 390
Soren Bjornstad Avatar asked Feb 07 '23 17:02

Soren Bjornstad


1 Answers

This is do-able within knitr, no need to use pandoc. Based on an example posted by Yihui at https://github.com/yihui/knitr-examples/blob/master/073-code-appendix.Rnw

Set echo=FALSE throughout your document: opts_chunk$set(echo = FALSE)

Then put this chunk at the end to print all code:

 ```{r show-code, ref.label=all_labels(), echo = TRUE, eval=FALSE}

 ```

This will print code for all chunks. Currently they all show up in a single block; I'd love to figure out how to put in the chunk label or some other header... For now I start my chunks with comments (probably not a bad idea in any case).

Updated: to show only the chunks that were evaluated, use: ref.label = all_labels(!exists('engine')) - see question 40919201

like image 71
DonJ Avatar answered Feb 16 '23 02:02

DonJ