Package Management
Last updated: January 18, 2021
Table of contents:
Using renv
for package management
renv
is a package to help make reproducible R environments, distributed by the RStudio folks.
I have had lots of issues with packrat
failing to properly bootstrap an environment on a new system, so I’ve abandoned it in favor of renv
.
Installing
install.packages("renv")
Using in a new project
Read this introduction first. Then:
setwd("/path/to/project)
renv::init()
- Restart R
- Install packages as you normally would with
install.packages()
(or userenv::install()
if you want) - Create a
.R
file and uselibrary(packagename)
to import your packages. - Use
renv::snapshot()
to save the list of dependencies torenv.lock
.- Note that this will look at the
.R
files in your project to figure out what packages actually need to be included in therenv.lock
file. If you want to include everything, you can userenv::snapshot(type = "simple")
instead.
- Note that this will look at the
Note that you’ll get the following when you set up renv
for the first time:
Welcome to renv! It looks like this is your first time using renv.
This is a one-time message, briefly describing some of renv's functionality.
This package maintains a local cache of data on the filesystem at:
- '~/Library/Application Support/renv'
This path can be customized -- see the documentation in `?paths`.
renv will also write to files within the active project folder, including:
- A folder 'renv' in the project directory, and
- A lockfile called 'renv.lock' in the project directory.
In particular, projects using renv will normally use a private, per-project
R library, in which new packages will be installed. This project library is
isolated from other R libraries on your system.
In addition, renv will attempt to update files within your project, including:
- .gitignore
- .Rbuildignore
- .Rprofile
Please read the introduction vignette with `vignette("renv")` for more information.
You can also browse the package documentation online at http://rstudio.github.io/renv.
By providing consent, you will allow renv to write and update these files.
Bootstrapping an existing project
“Bootstrapping” refers to taking an existing project from another person or system, and getting all the dependencies installed on your system for the first time.
You can install all the dependencies specified in renv.lock
by running renv::restore()
.
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 knowledge base for R, the popular statistical programming language. I attempt 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 or use the survey link below..