Package Management

Last updated: January 18, 2021

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:

  1. setwd("/path/to/project)
  2. renv::init()
  3. Restart R
  4. Install packages as you normally would with install.packages() (or use renv::install() if you want)
  5. Create a .R file and use library(packagename) to import your packages.
  6. Use renv::snapshot() to save the list of dependencies to renv.lock.
    • Note that this will look at the .R files in your project to figure out what packages actually need to be included in the renv.lock file. If you want to include everything, you can use renv::snapshot(type = "simple") instead.

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..