# Using packrat

RStudio have released packrat, an R package to manage dependencies between packages to ensure that what gets shipped with a project works.

Following the walkthrough example should give a pretty good idea of what you need to do.

## Before you start

Ensure that you have the latest versions of devtools and packrat from github, using devtools from CRAN gave me an error.

  library(devtools)
install_github("devtools")
devtools::install_github("rstudio/packrat")


With that done, you can procede as below…

## Example

Code I ran to get the Distance 6.2 packrat “project”.

Create a package directory and run (from the level above the package directory):

  library(packrat)
packrat::bootstrap("packrat-distance-6.2/")


inside, this creates the requisite files and folders. You should get some messages asking you to restart R.

Now starting R again (and ensuring the working directory is our project folder), we effectively have a “fresh” R install, so we have to install all the packages again, but now they will be frozen at their “correct” versions.

We want the versions of dsm and mrds currently on CRAN, so install them as usual:

  install.packages(c("dsm","mrds"))


packrat needs to find a .R file with a library or require call somewhere in the directory (I guess to prove you’re serious? I don’t know). Anyway just adding

  library(mrds)
library(dsm)


to a file (I called mine packrat-load.R) will keep it happy.

Now to take the snapshot of the package dependencies:

  packrat::snapshot()


This downloaded the sources of the dependencies and install them. This means you have to have C and Fortran compilers installed!

Now when you launch R in the project directory we will get the right versions of the packages (.Renviron and .Rprofile ensure this).

## Version control

You don’t need to include the library/ folder but you do need the other files added to your git repository.

When you clone the repository, you need to call initPackrat() to install the source packages. In terms of shipping, building on the given platform should suffice.