Background at a Glance

An overview of the ideas behind reproducible R environments, before we meet {renv}

One Big Question

How can we make sure that our R code runs on other machines, by other people, and in the future?

The four chapters in this section build up to that question:

  1. Reproducibility — why code should be portable and future-proof
  2. Software Dependencies — how software builds on software
  3. Technical Definitions — packages, libraries, and repositories
  4. R Package Managers — from {packrat} to {renv}

1 · Reproducibility

What Does “Reproducibility” Mean?

Reproducibility is the ability of independent investigators to draw the same conclusions from an experiment by following the documentation shared by the original investigators. –Gunderson

  • A written paper rarely captures every data cleaning, exploration, and analysis choice
  • Sharing code and data for complete documentation
  • Our focus: code that others can run on their machines, now and later

Why Bother with Reproducible Code?

  • For science: lets others verify findings, build on existing work, and keeps the process transparent and trustworthy
  • For yourself: makes it easier to revisit your own work, debug, and collaborate
  • In practice:
    • reproducibility saves time in the long run
    • Journals and funding agencies increasingly require it 😟

The “Works on My Machine” Problem

Your machine works because you configured it. Someone else’s machine probably differs:

  • Operating systems — and versions of the same OS
  • Language versions — R’s random number generation changed at 3.6.0; density() changed again at 4.4.0
  • Package versions — the most likely pain point, with over 20,000 packages on CRAN alone

2 · Software Dependencies

What Is a Dependency?

Software depends on other software to run:

  • Package dependencies are R packages a project needs — and they have dependencies of their own
    • e.g. {dplyr} relies on {tibble}, {rlang}, and {vctrs}, which rely on others…
  • System dependencies are other programs, e.g. Python or a C++ compiler

This cascading problem is known as “dependency hell” — and it is not unique to R.

How Should We Manage Dependencies?

  • Manually: write down the software and versions you used
  • Automatically: use a package manager
    • Accurate, shareable, and the recommended approach

Common R package managers you will meet: {renv} (our recommendation), {groundhog}, and the deprecated {packrat}.

3 · Technical Definitions

Packages, Libraries, Repositories

Three terms come up constantly in R dependency management:

  • Package: a bundle of functions, data, and documentation that extends base R
  • Library: a folder on your computer where installed packages are stored
  • Repository: an online collection from which packages can be downloaded and installed

Packages & Libraries

  • Packages come pre-installed (the base and recommended packages, like {stats}) or are user-installed from a repository
  • Libraries hold packages; .libPaths() shows which ones your R session uses

Repositories

Where to get packages from:

  • CRAN — the default and most popular; curated and reviewed, often the “gold standard”
  • Bioconductor — thousands of packages for bioinformatics and genomics
  • GitHub / GitLab — where developers host and distribute packages

4 · R Package Managers

What Do R Package Managers Do?

Package managers ensure the packages your project needs are:

  • downloaded from a defined repository
  • installed into the libraries you want to use
  • recorded with their versions, so the environment can be recreated

Three Managers Worth Knowing

Manager Status Idea
{packrat} Deprecated The original; deprecated
{groundhog} Alternative Date-based: still developing?
{renv} Recommended Posit; project-based and widely used

Meet {renv}

renv::init()      # set up a project library and lockfile
renv::snapshot()  # record the package versions in renv.lock
renv::restore()   # reinstall those versions later, or on a collaborator's machine
  • Stable (v1+), maintained by Posit, and a transferable skill between academia and industry
  • Project-based workflow: pairs naturally with R Projects and CI/CD pipelines

What Package Managers Cannot Do

  • They manage packages only — not R itself, other software, or the operating system
  • Matching R versions is usually the main hurdle: tools like rig or container solutions (Docker, Binder) fill that gap
  • System dependencies are not managed either, but are documented in a package’s DESCRIPTION file and on Posit Package Manager

Wrapping Up

  • Reproducibility means others can rerun your code and draw the same conclusions
  • Dependencies grow recursively, so manage them automatically
  • Remember the vocabulary: packages live in libraries, and are installed from repositories
  • For R, the recommended package manager is {renv} — read through the content, and try the exercises!