Packrat is now soft-deprecated
It is still maintained, but there will be no new development for it. An updated alternative would be
Do you want to learn about it? Visit this building block!
Suppose you’re trying to run someone else’s codes and you are bombarded with package installation errors. It can be frustrating to guess what R packages are needed to be installed, right? Or, have you ever updated a package to get code in one of your projects to run, only to find that the updated package makes code in another project stop working?
packrat you won’t be worrying anymore. In this guide you will:
- Understand the benefits of using
packratfor managing R package dependencies.
- Learn how to initialize, install, and manage packages using
- Grasp how to collaborate efficiently using
packratfor consistent R environments.
Get started with Packrat
Packrat is one of the most typical solutions to dependency issues. It is a dependency management system for R packages which is:
Packratgives each project its own private package library, so installing a new or updated package for one project won’t affect other projects you may be working on. As a result, next time you start an R session in a
packratproject directory, R will only look for packages in your private library and all changes to the packages are made in this private package.
- Portable: Easy to transfer your R projects from one machine to another, or even across platforms.
Packratsaves the exact package version your project depends on, so those exact versions are installed always whenever you run the project anywhere.
install.packages("packrat") # Alternatively, you can also install the development version of Packrat with: install.packages("devtools") devtools::install_github("rstudio/packrat")
You’ll also need to make sure your machine is able to build packages from source. See Package Development Prerequisites for the tools needed for your operating system.
Before starting to write the codes for your R project, initialize
packrat by running
packrat::init("...") and enter your project directory path inside the parantheses. If you already set the working directory to your desired project directory then simple run
Now, you’re in a
packrat project which has its own private package library. Any packages installed from inside a
packrat project are only available to that project and the ones installed outside of the project are not avilable to the project. This is basically the “isolation” feature of
Once you run this initialization code, you’ll see a
packrat folder created in your project directory which contains the following files:
packrat/packrat.lock: This contains a list of the package versions that satisfy the dependencies. Note that this file should never by edited manually by the user!
packrat/packrat.opts: This file contains project-specific packrat options and can be queried and set with
?"packrat-options"for more information.
packrat/lib/: This is the private package library for the current project.
packrat/src/: This folder contains the source packages of all the dependecies packrat has detected.
.Rprofile: Instructs R to use the private package library when starting the project.
Adding, removing, and updating packages
Simply install the packages as you normally would with
install.packages() and these packages now get installed in your project’s private package library. Then, take a snapshot to save these changes to
packrat by running:
Suppose you made some changes to your code and remove one of the libraries. When you re-run the snapshot command,
packrat will remove the dependent packages that are no longer required to run the script. Thus, keeping the private
packrat library up-to-date and tidy.
So let’s say you are collaborating with a co-author/teammate on GitHub and you made changes to your R project and took snapshot of the packages before comitting the changes. Now, your collaborator fetches these changes by pulling the commits made to their local machine. They can check “status” which shows the differences between the project’s
packrat dependencies, its private package library, and R scripts. Then they can “restore” the changes made.
packrat installs all the dependencies and your collaborator’s private library looks just like yours did when you wrote the codes and they will be able to run all the codes smoothly.
Cleaning up packages
As your project progresses, your package libraries may have outgrown and some of the packages installed may no longer be needed.
Packrat can analyze your .R script files and filter packages that are needed depending on your codes and keep the package library tidy.
# View changes made since last snapshot packrat::status() # Remove unwanted packages packrat::clean()
Use “Pacman” to load multiple packages in one go!
One usually loads packages requires using the
library() function but if you want to load multiple libraries, it gets a bit tedious. Pacman is a handy tool to load multiple packages with a single code snippet.
Get started with Pacman:
- Step 1: Install pacman
- Step 2: Load multiple packages with
pacman::p_load(dplyr,ggplot2,psych) # for example
Now you’re all set to “Packrat-away” your package dependency issues!
Packrat is a system for managing R package dependencies, ensuring consistent, isolated, and reproducible R environments. The tool ensures that each project has its own private package library, making R projects portable and specific package versions are consistently installed across environments.
- Package Management:
Users can initialize, install, snapshot, restore, and clean packages using packrat, ensuring consistent versions of packages across different setups. The private library keeps the specific versions of packages, which can be updated, and unused packages can be tidied up with specific commands.
- Deprecation Warning:
Packrat is soft-deprecated with no new developments planned, and users are advised to consider renv as an alternative for reproducible R environments.