Packrat Package – managing package versions

This week CEREO’s Stephanie Labou introduced us to the packrat package. Packrat is a relatively new package that assists collaboration and functionality of code by maintain and standardizing package versions used in a project. Depending on the level of experience, R users may not have ran into this issue before but it is a persistent problem with the R system. Due to the dynamic and open nature of the software, changes and improvements to packages can tweek the way that certain functions interact, making old code buggy or obsolete. Packrat is an attempt to control for this.

Packrat, in essence, creates a large zip file with all of the libraries and settings used for a project. Users then send this entire file to their collaborators and collaborators load packages and libraries from that zip file. This ensures that the versions of packages used are the same across all collaborators. Within packrat, each folder is essentially its own project, with its own packages – packrat folders are created within the working directory when the creation command is called.

The first step in using packrat is to create, or “bundle” your libraries. This is shown in the script below. In addition, the script below uses the “::” syntax to call commands. The double colon symbol is a way to specify exactly which packages commands are being used. This is because some packages have commands with the same name – whichever package is loaded last will overwrite the identically named commands from the other one. This is why people’s script may sometimes have notes about the loading order of packages.

PackratBundling

Once the packrat package has bundled the libraries for a project, you can then send the entire file to a collaborator. To re-create this on your own computer open a brand new R session and then follow the script below, which will unbundle the packrat file created in the above script:

PackratUnbundling

Once you are working within a packrat session there are some useful commands to know. One is sessionInfo() which shows what versions of things you have loaded. There is also a way to install older versions of packages – this is useful if you want to create a new packrat project but you realize your current packages are too new. Information on how to do that can be found here.

Additionally, the scripts provided by Stephanie do an excellent job of annotating, or commenting, on the code. This is especially important when working with collaborators, but is also important when working solo as it makes it easier to troubleshoot issues. Good annotations can help users determine if issues are code issues, are package related (and can therefore be addressed with packrat), or are (rarely) issues with versions of R. R version errors are harder to fix, and are not addressed by the packrat package. But! As Dr. Katz said during this session: “there is a long conversation to be had about strategies in programming for another time.”

Enjoy packrat!