New on Techguides: manage dependencies with renv

[This article was first published on Mirai Solutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Concepts like reproducibility, integrity and collaborative development are a “must” for using R in production.

Among other common best practices (modularize your code, structure it as a package, define unit testing and automate with CI / CD pipelines), managing code dependencies is a fundamental step for ensuring quality and robustness of your project. In fact, by not specifying the versions of your package dependencies, your project is exposed to uncontrolled dependency updates, causing potential breaks or — even worst — introducing non-breaking unexpected behaviors.

While on the one hand you may want to benefit from the latest developments in the community, on the other hand you must ensure integrity and reproducibility of your product. Therefore, controlling — and potentially aligning — development and productive environments is essential.

However, for an R project this may not be trivial. For this reason we at Mirai Solutions have added a new chapter to our techguides, describing how to manage dependencies using renv.

renv

renv is a package that helps you creating reproducible environments for your R development. It can be installed from CRAN:

install.packages("renv")

In an renv-project, the dependencies are automatically detected and, if out of sync, the user can chose to renv::restore() them. This approach ensures that dependencies can be collaboratively shared and maintained at project level.

To initialize a package with renv:

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

Dependencies can then be installed from a specific mran snapshoot, i.e. with a fixed version.

# Install all dependencies from a specific MRAN date repo
options(repos = "https://mran.microsoft.com/snapshot/2020-11-15")
renv::install("remotes")
(deps <- remotes::dev_package_deps(dependencies = TRUE))
renv::install(with(deps, sprintf("%s@%s", package[diff!=0], available[diff!=0])))

Finally, to enable tracking of the dependencies:

renv::snapshot()

This will create a snapshot of the dependencies in the form of a lockfile renv.lock , which should be shared under version control to ensure that anyone accessing the project would have the same R environment.

As a note, renv tracks and installs only the packages explicitly mentioned in the DESCRIPTION file. This means that packages needed for development, e.g. devtools, should be be installed for the specific project separately.

As a reference on how to use renv, have a look at the dedicated chapter in the techguides.

To know more about using renv for safely bringing a shiny app to production, don’t miss the chance to attend our workshop “Bring a Shiny App to Production”, taking place online on March 31st, 14-17 CET. In this hands-on workshop, additionally to renv, we will discuss other best practices such as: version control, automated CI/CD pipelines with GitHub Actions, and how to use GitFlow in a collaborative setup.

prodshiny

Share on:

To leave a comment for the author, please follow the link and comment on their blog: Mirai Solutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)