Reprinting R Chunks

November 3, 2017   

I only discovered this the other day and I’m writing it down so I can find it easily again.

In an R markdown document you can include a chunk or R code like

```{r chunk-name}
my_fn <- function() {
  print("Hello World.")
}
```

What I hadn’t realised is that you can reprint that chunk of code later by using the name as a reference.

```{r, ref.label = "chunk-name", echo = TRUE, results = "markup"}
```

The reason I was looking for this was to enable R functions to be printed in an appendix without having to copy and paste the code.

This also means that you could put a chunk earlier in the document with eval = FALSE and then actually calculate the results later in the document by using ref.label = "chunk-name", eval = TRUE. I doubt I’ll use this very much but it might be useful at some point.

It is of course in the knitr documentation and the functionality is exactly what I’ve come to expect from a package by Yihui Xie. Yihui thinks of pretty much everything when he writes a package.