WFU-TLC / flc_discussion_board

A repository for discussing questions and issues in the Data Analysis with R (FLC)
https://wfu-tlc.github.io/
0 stars 0 forks source link

Extracting code from an RMarkdown document #18

Open francojc opened 5 years ago

francojc commented 5 years ago

In our last session I mentioned that there is a way to extract the code, which is contained in the code chunks, from an RMarkdown document. The purl() function from the knitr package will do this.

Imagine your have an RMarkdown file analysis.Rmd. You will specify the input path to this file and then the path to the file you wish to create.

knitr::purl(input = "analysis.Rmd", output = "analysis-code.R")

The output in the analysis-code.R file, by default, will only include those comments which appear inside a code chunk -other markup and prose will not. The purl() function has other options that you can tweak. Take a look at the documentation if you'd like to explore this function with ?knitr::purl.