Package Management
Last updated: September 29, 2019
Package management
Packrat is the standard package management tool for R. It’s not perfect, but it’s better than nothing.
Possible alternatives include pak
(a R Infrastructure package that is an alternative to install.packages()
and devtools::install_github()
) and renv
(from RStudio).
Standard workflow
- Install the
packrat
package (you only need to do this once for your system):install.packages("packrat")
- Initialization to use
packrat
with a new project:packrat::init("/path/to/rstudio/project")
- Or you can turn it on via the RStudio interface.
- Install package:
install.packages("packagename")
and then callpackrat::snapshot()
to save the changes to Packrat. - Install everything specified in the latest snapshot:
packrat::restore()
. This is what you would do if you just cloned a git repository that uses Packrat to install all the necessary dependencies.- You can do this with RStudio by going to the “Packrat” dropdown in the “Packages” tab, and then choosing “Check Library Status…”
- Using Packrat with git:
packrat/lib*
is gitignored by default. You can also gitingorepackrat/src
if you don’t want to commit the source for all dependencies to your repo (other people with have to re-download everything themselves, which is probably fine).- I typically do ignore
packrat/src
to avoid bloat in the repo.
- I typically do ignore
Storing installed packages in a custom library location
.libPaths(c("/path/to/folder", .libPaths()))
After you run that, install.packages()
will install in that folder, and library()
will try to load from that folder.
Installing a specific version of a package
Using the remotes
package
remotes::install_github("user/repo@v1.1.1")
Using the devtools
package
devtools::install_version()
ℹ️ This page is part of my R knowledge base, which attempts to use idiomatic practices with the tidyverse
collection of packages as much as possible. If you have suggestions for ways to improve this code, please contact me.