Create appendix with R-code in rmarkdown/knitr

knitr::purl() can extract all R code from a markdown file into an R script. You can add that as an appendix.

## appendix

```{r code=readLines(knitr::purl('~/path/to/file.Rmd', documentation = 0)), eval = FALSE}

```

You could use a reference to your initial chunks, but then change the options:

main text

```{r blah, echo = FALSE}
summary(cars)
```

appendix

```{r blah2, ref.label='blah', eval = FALSE}
```

Which will give:

enter image description here


Another possibility:

### Appendix 
```{r, ref.label=knitr::all_labels(),echo=TRUE,eval=FALSE}
```

as suggested by Yihui's nice example