Skip to content

Instantly share code, notes, and snippets.

@al2na
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save al2na/2c37953e8f77ca51f67a to your computer and use it in GitHub Desktop.
Save al2na/2c37953e8f77ca51f67a to your computer and use it in GitHub Desktop.

This document shows how to use packrat to create reproducible R projects. packrat allows reproducuiblty of R code in an OS independent manner. First we need to install the packrat package.

install.packages("packrat")
setwd("~/mdc_desktop_projects/packrat_trial3")

now we can initialize the packrat project. This will create local directories for packages and .Rprofile file. Whenever we run R in this directory, we will be using locally installed R packages and even if we copy this directory to some other computer (even with different OS) we will still have the same setup.

packrat::init(getwd())
Initializing packrat project in directory:
- "/Users/aakalin/mdc_desktop_projects/packrat_trial3"

Adding these packages to packrat:
            _      
    packrat   0.4.3

Fetching sources for packrat (0.4.3) ... OK (CRAN current)
Snapshot written to '/Users/aakalin/mdc_desktop_projects/packrat_trial3/packrat/packrat.lock'
Installing packrat (0.4.3) ... OK (downloaded binary)
Initialization complete!

Restarting R session...

Now assume we want to use edgeR, we should just install it, all of its dependencies will be downloaded and wil be available to anyone who copies this working directory and runs R and scripts within this directory.

source("http://bioconductor.org/biocLite.R")
biocLite(c("edgeR"))

we take the snapshot of the current state to save. Now every time you start R in this directory your session will be restored, or whenever you copy this folder to a new computer, the the packages in the snapshot will be automatically installed.

packrat::snapshot()
> dir()
[1] "packrat"          "packrat_demo.Rmd"
> dir("packrat")
[1] "init.R"       "lib"          "lib-ext"      "lib-R"        "packrat.lock" "packrat.opts" "src"  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment