Skip to content

Instantly share code, notes, and snippets.

@grimbough
Created June 3, 2019 14:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grimbough/efafda8cad546c1a0443f463324a7824 to your computer and use it in GitHub Desktop.
Save grimbough/efafda8cad546c1a0443f463324a7824 to your computer and use it in GitHub Desktop.
Reusing code chunks from an existing Rmarkdown document
This code chunk will extract all code blocks from `example.Rmd` and write them to a temporary file, preservining chunk options. It will then read them and make those chunks avaliable in the current R environment when the presentation is knitted.
```{r include, eval=TRUE, echo=FALSE, include = FALSE}
tmp_script <- tempfile()
knitr::purl("example.Rmd", output=tmp_script, quiet = TRUE)
knitr::read_chunk(tmp_script)
```
We can then include a chunk from `example.Rmd` like so:
```{r chunk-in-report, eval=TRUE, echo=FALSE}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment