Using Packrat with Bioconductor in RStudio

As an R programmer, you may not be familiar with the development processes involved in programming Java. For those of you who have written some production Java code, you may have found that the barrier to entry can seem quite high. With so many tools you need to grok in order to have a basic level of proficiency, particularly if you are thrown in the deep end on a mature code base. Furthermore, soon even more complexity will be added to the Java developer’s toolbox with the additions presented in Java JDK 9 such as the module system that has burst forth from Project Jigsaw.

With this said, one thing tool that has matured in Java software development is the use of dependency management. While in years past, Java libraries (packages) would be committed to repositories along with a project’s source code, it is now common practice to define a pom.xml file which contains a listing of all libraries and their versions. When a developer clones a copy of the source code, she will run ‘mvn build’ to build the project and simultaneously download all library dependencies to her machine if they are not already present. This means that all developers who build their project using maven will be using the same versions of libraries while testing their code.

Packrat provides this very same level of convenience to R programmers. In particular, packrat works by creating a subfolder in your R project which stores a file that specifies all the packages and their versions you used in your project as well as a repository of packages that is used privately by your project.

As this blog addresses using R with Bioconductor, I will discuss what I do in order to set up a project with packrat using RStudio. You will note that I mix using the Graphical User Interface and a handful of packrat commands in the console which I find to be most useful.

The easiest way to set up RStudio to use Packrat is, when creating a new project, is to ensure you choose the ‘Use packrat with this project’ option.

Ensure you choose 'Use packrat with this project'

Now we have an R project with its own package library inside the packrat subfolder. If using a version control system like Git, it’s tempting to commit the whole packrat directory along with the project to your git repository. The consequence of this is that you are potentially installing binary files to the repository making it a much larger repository for others to download if they want to run your code.

To avoid committing R packages to git, packrat provides a function that modifies your .gitignore file. Run this inside your RStudio console:

You can now commit everything to your git repository as an initial commit.

You should now be able to install all packages using install.packages to retrieve packages from CRAN. After installing a package, it will be saved in your private packrat repository. However, in order to update the packrat list of packages (which is described in the packrat/packrat.lock file), you should perform packrat::snapshot() after each package you install in order to avoid any surprises later on.

Finally, one issue I had with using packrat was how to install packages from Bioconductor. In my experience, the easiest way to do this is through setting the available repositories interactively by typing

into the console. This presents you with a text-based prompt:

Select all BioC repositories and then you can simply install all required Bioconductor packages using install.packages. Packrat will keep track of the version of Bioconductor currently being used.

Having done all this, when someone wants to use your code elsewhere, they need only clone your project and load it in to RStudio. RStudio will automatically restore all the packages that are missing into the project by downloading them from the relevant repositories.

Packrat is by no means perfect, for instance, packrat will endeavour to download binary packages on Windows as it lacks a toolchain for compiling any C/C++ code. Some packages in Bioconductor are only available as source and, as such, packrat is unable to find these packages.

I really appreciate the work done to make packrat work with R and it will, I’m sure become increasingly important in the future to make sure that R code that is written is more stable and predictable by keeping R packages consistent across all computers using a particular R project.

This entry was posted in packrat, r, rstudio, statistics. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *