Using Rmarkdown without re-running long commandsEmily DolsonOctober 3, 2015One potential obstacle to using Rmarkdown with computationally-intensive projects is that waiting for the whole thing to run again every time you make a small change to your document is a pain. So here are a few workarounds. Option 1: Put commands that only need to be run once in an eval=FALSE chunk It’s usually important to include commands to download and unpack data with your script, so that it’s clear which data you were working with. However, you probably never need to run those commands more than once. In this case, the best options is probably to put them inside an r chunk, with the ```{r, eval=FALSE} #download huge dataset ``` Note that if someone else downloads and runs your file, they will need to set the Option 2: Use caching To deal with this very problem, chunks in R markdown have a ```{r, cache=TRUE, cache.comments=TRUE} #code that takes forever to run #I can add this comment later without rerunning the code that takes forever! ``` Option 3: Load previous results conditionally Caching is all well and good, but what if you were playing around in the console (rather than your Rmarkdown file), and created some object that took forever to create. Or what if the script that you’re documenting can’t even run on your computer (for instance, maybe you had to run it on the HPCC instead)? In this case, your best bet is to save the object from wherever you created it, and then have your Rmarkdown code look for the file you stored it in before trying to run the code again. This idea was originally suggested in this stackoverflow post. Here’s an example of how it might work: First you store the object into a file:
Then make sure this file is in the current working directory and add something like this to the part of your Rmarkdown script where you need to use ```{r}
``` By following these approaches, you can make sure that your code is reproducible without needing to wait for it to reproduce itself every time you knit the file!
10 Comments
Using Rmarkdown without re-running long commandsEmily DolsonOctober 3, 2015One potential obstacle to using Rmarkdown with computationally-intensive projects is that waiting for the whole thing to run again every time you make a small change to your document is a pain. So here are a few workarounds. Option 1: Put commands that only need to be run once in an eval=FALSE chunk It’s usually important to include commands to download and unpack data with your script, so that it’s clear which data you were working with. However, you probably never need to run those commands more than once. In this case, the best options is probably to put them inside an r chunk, with the ```{r, eval=FALSE} #download huge dataset ``` Note that if someone else downloads and runs your file, they will need to set the Option 2: Use caching To deal with this very problem, chunks in R markdown have a ```{r, cache=TRUE, cache.comments=TRUE} #code that takes forever to run #I can add this comment later without rerunning the code that takes forever! ``` Option 3: Load previous results conditionally Caching is all well and good, but what if you were playing around in the console (rather than your Rmarkdown file), and created some object that took forever to create. Or what if the script that you’re documenting can’t even run on your computer (for instance, maybe you had to run it on the HPCC instead)? In this case, your best bet is to save the object from wherever you created it, and then have your Rmarkdown code look for the file you stored it in before trying to run the code again. This idea was originally suggested in this stackoverflow post. Here’s an example of how it might work: First you store the object into a file:
Then make sure this file is in the current working directory and add something like this to the part of your Rmarkdown script where you need to use ```{r}
``` By following these approaches, you can make sure that your code is reproducible without needing to wait for it to reproduce itself every time you knit the file! |
Spatial Ecology @ MSUClick on "Category" below to search for R code compiled by the Zarnetske Spatial & Community Ecology Lab and students in MSU's Spatial Ecology graduate course (FOR870/FW870) Category
All
Archive
October 2016
|