• This mail was updated with advanced tips (at the bottom) 2/12/2016

For scientific projects we often using knitr and R markdown in RStudio to share results and code either in Hypertext markup language or PDF format. These are great tools for reproducible research. We rule, however, that keeping all of our R code in an R markdown (.Rmd) file creates two problems. First, you need to completely re-run the report to assess new bits of R encipher and secondly the .Rmd file away can get clunky very apace. Fortuitously, there is an easy mode to make usage of external code.

1. Create your foreign R script

You would make over an R script as normal and, within the R hand, you create 'chunks' victimization the ## @knitr syntax. So, in this exemplar, we create a revolutionary R script entitled example.R with two chunks (variablesXY and plotXY) that looks like this:

                # This is our external R book called example.R # We're adding two chunks variablesXY and plotXY  ## @knitr variablesXY  x<-1:100 y<-x+rnorm(100) head(data.frame(x,y))  ## @knitr plotXY  plot(x,y)                              

2. Create your R markdown script and refer to the external R script

Now you can create your R markdown (.Rmd) file. In order to read your external file you use the function read_chunk then you can reference individual chunks using the <<chunkname>> syntax. So, continued with our example, we bottom create a new file called myreport.Rmd, read the external file and reference/run the code chunks:

                # record chunk (does not run code) ```{r echo=FALSE} read_chunk('example.R') ```  # run the variablesXY chunk and utilization the variables it creates ```{r first} <<variablesXY>> head(data.frame(x,y))  ```  # run the plotXY clump and create the plot ```{r second} <<plotXY>> ```              

3. Alternative syntax

Although we usually use the syntax above which allows us to easily rhenium-use chunks and flexibly name the R markdown chunks, on that point is an choice syntax you can use. You can move every of the R code to the chunks in the external file and concern to those chunks in the R markdown chunk headers. For example, to produce precisely the same outturn as that higher up you can use the favorable for the example.R:

                ## @knitr variablesXY  x<-1:100 y<-x+rnorm(100) steer(data.frame(x,y))  ## @knitr plotXY  plot(x,y)                              

And so the myreport.Rmd arse be simplified to look like:

                ```{r echo=FALSE} read_chunk('exemplar.R') ```  ```{r variablesXY} ```  ```{r plotXY} ```              

In either case, the outturn would look like this:

Capture

High tips:

Discharge and show external code but except the comments

This was a tip provided by Curtsy Rudis. My external computer code often has comments that I don't privation included in my concluding report but including code and excluding comments is difficult. To do this you add a few arguments to your code clump, one is tidy=Right which tells R to tidy raised the code (but leaves comments entire). The second is an argument to tidy.opts place setting comment=FALSE. To a reliable extent this is covered in Yihui's post happening chunk options but the various scene available in tidy.opts are not spelled out on that point.

                ```{r, echo=TRUE,tidy=TRUE, slicked up.opts=tilt(comment=Faithlessly)} <<variablesXY>> ```                              

Run or show all codification from an external script

I occasionally hold a situation where I want to echo the inscribe from a script but I don't wishing to run it. The simplest way to do this would be to simulate and paste the code into a code chunk (and set eval=Mendacious) but, of naturally, you would need to re-cut and paste if your code changes. An alternative to this, explained by Yihui at the bottom of this page, would be to purpose the code controversy and readLines. And then, as an example, to reverberate the cipher (but don't run IT) from an external script called myscript.R in your Rmarkdown you pot use up:

                ```{r, code=readLines("myscript.R")} ```