3.2 Initialize an renv project

To initialize a package with renv, run:

  # use the DESCRIPTION file to capture dependencies
  settings = list(snapshot.type = "explicit"),
  # do not install dependencies (done in a custom way)
  bare = TRUE

This ensures that the dependencies of the package, as listed in the DESCRIPTION file are used for initializing the virtual environment.

Note: renv tracks and installs only the packages explicitly mentioned in the DESCRIPTION file. This means that only the dependencies used by the package are installed. Packages needed for development, such as devtools, should be be installed for the specific project via install.packages() or renv::install().

The renv initialization causes:

  • the creation of a new renv.lock file where the R version and the list of used packages with their version are tracked;
  • the creation of a new renv folder containing:
    • a setting file settings.dcf,
    • a script activate.R to activate the project-specific virtual environment,
    • the project-specific library of installed packages;
  • the execution of the activation script on project launch via .Rprofile;
  • an update of the .Rbuildignore because the renv-specific files are not part of the R package infrastructure;
  • an update of the .gitignore to exclude the renv library from version control, as it can be simply restored via renv::restore().

Having all the renv infrastructure committed under version control ensures that the same renv setup is available to anyone working on the same project (via renv::restore()).

3.2.1 Control packages version

As a note, if the DESCRIPTION file does not request a specific package version, the renv initialization will pick the version currently available in the user library when the snapshot of the packages was created. However, it is advisable to have a stricter control over the version of the packages used in a project. To do so, one can set the option to install dependencies from a specific MRAN repo, therefore fixing the version for all available packages.

# Install all dependencies from a specific MRAN date repo
options(repos = "https://mran.microsoft.com/snapshot/2020-11-15")

One can then install the dependencies of a package via:

(deps <- remotes::dev_package_deps(dependencies = TRUE))
renv::install(with(deps, sprintf("%s@%s", package[diff!=0], available[diff!=0])))

To create a snapshot of the package dependencies and update the renv.lock file:

# Create a snapshot to track dependencies in the lockfile