renv

Overview

{renv} is a package management tool for R that helps you create isolated and reproducible R environments. It allows you to specify the exact versions of R packages that your project depends on, ensuring that your code runs consistently across different environments.

With {renv}, you can create a project-specific library of packages, which are separate from your global R library. This allows you to have different versions of packages for different projects and ensures that your project dependencies are self-contained. {renv} also provides tools for managing and resolving package dependencies, making it easier to collaborate with others and share your code.

To use {renv} in your projects, set up the project environment by running renv::init(). This command creates an renv.lock file, which specifies the exact versions of packages needed for the project. Other people can then replicate the environment by running renv::restore() using the renv.lock file.

Slides

Open the slides in a new tab here.

Exercise

  1. Download the renv.lock file here.
  • If it downloads a file, navigate to your R project in your computer file system and replace the renv.lock file that’s already there with this new one.
  • If it opens in the web browser, copy all the text and paste over the renv.lock file that’s already in your R project.
  1. In your R project, run renv::restore().

  2. Install a new R package of your choice. (Not sure what to choose? Try one of these fun packages. For example, I did install.packages("hadley/emo").)

  3. Create an R script and save it in your R project. Include some code that requires the package. For example:

emo::ji("banana")
🍌 
  1. Run renv::status() to make sure your project picked up the package as a dependency.

  2. Run renv::snapshot() to record that package in your lockfile.

  3. Open your lockfile and look for your new package. For example, mine now has:

"emo": {
      "Package": "emo",
      "Version": "0.0.0.9000",
      "Source": "git",
      "RemoteType": "git",
      "RemoteUrl": "https://github.com/hadley/emo",
      "RemoteHost": "api.github.com",
      "RemoteUsername": "hadley",
      "RemoteRepo": "emo",
      "RemoteRef": "master",
      "RemoteSha": "3f03b11491ce3d6fc5601e210927eff73bf8e350",
      "Requirements": [
        "R",
        "assertthat",
        "crayon",
        "glue",
        "lubridate",
        "magrittr",
        "purrr",
        "rlang",
        "stringr",
        "utils"
      ],
      "Hash": "eb32b8f0bb50621ad498f5d836b546a4"
    },

Resources