When building, deploying, or testing with continuous integration (CI) systems (e.g. Travis, AppVeyor, and others), one often needs to download and install a set of R packages before the service can be run. Normally, one will have to download and re-install these packages on each build, which can often be slow – especially in environments where binary packages are not available from your R package repositories.

renv can often be helpful in these situations. The general idea is:

  1. Call renv::snapshot() on your local machine, to generate renv.lock;

  2. Call renv::restore() on your CI service, to restore the project library from renv.lock;

  3. Ensure that the project library, as well as the global renv cache, are cached by the CI service.

Normally, renv will use the R package repositories as encoded in renv.lock during restore, and this will override any repositories set in other locations (e.g. in .Rprofile or .Rprofile.site). We’ll discuss some strategies for providing an alternate R package repository to use during restore below.

Travis CI

On Travis CI, one can add the following entries to .travis.yml to accomplish the above:

cache:
  directories:
  - $HOME/.local/share/renv
  - $TRAVIS_BUILD_DIR/renv/library

install:
  - Rscript -e "renv::restore()"
  
script:
  - Rscript -e '<your-build-action>'

Note that we provide both install and script steps, as we want to override the default behaviors provided by Travis for R (which might attempt to install different version of R packages than what is currently encoded in renv.lock). See https://docs.travis-ci.com/user/languages/r/#customizing-the-travis-build-steps for more details.

It’s also possible to override the package repository used during restore by setting the RENV_CONFIG_REPOS_OVERRIDE environment variable. For example:

env:
  global:
    - RENV_CONFIG_REPOS_OVERRIDE=<cran>

replacing <cran> with your desired R package repository. This can also be accomplished in a similar way by setting:

options(renv.config.repos.override = <...>)

but it is generally more ergonomic to set the associated environment variable. (See ?config for more details.) This can be useful if you’d like to, for example, enforce the usage of a MRAN checkpoint during restore, or another similarly-equipped repository.

See https://docs.travis-ci.com/user/caching for more details on how Travis manages caching.