# New on Techguides: manage dependencies with renv

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 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.