Join the community!
Visit our GitHub or LinkedIn page to join the Tilburg Science Hub community, or check out our contributors' Hall of Fame!
Want to change something or add new content? Click the Contribute button!
Environment management for R projects with renv
renv is a dependency management tool for R projects, facilitating the reproducibility of environments across computers or platforms.
As a package management tool,
renv helps to make projects:
- isolated : installing or updating a certain package in one project doesn’t affect other packages from other projects
- portable : projects can easily be transferred to different devices
- reproducible :
renvmakes it easy to reproduce projects by recording all package versions of each project
First thing we need to do is to install the
2. Initialize new project environment
Then we can move to initializing the local environment of the project. We can do that with the following command:
3. Install/remove packages
After initializing the project, we can install or remove packages from it:
#install packages install.packages() #or renv::install() #remove packages remove.packages() #or renv::remove()
4. Save library of project in
After configuring the project, we should save the configuration into a
lockfile, in which all packages versions are recorded.
We can also check the status of the library with:
If we continue to make changes to the packages after making the
lockfile, but then we would like revert to the previous state (as recorded in the
lockfile), we can use:
After running all the commands above, the directory in which the project was created should contain the following files:
||File that activates renv for the new R sessions|
||The created lockfile containing all packages and versions|
||Activation script run by
||Private project library|
From packrat to renv
Packrat is now deprecated. To switch from Packrat to renv, use
renv::migrate() to migrate projects.
Why renv is better than groundhog
groundhog is another R package that makes projects reproducible. However, it doesn’t work by keeping records of packages versions necessary for each project, like
renv does. Instead you have to manually load libraries with the option of giving a date on when the wanted version was available. Thus,
renv has more advantages because it can load all necessary packages in one go, without having to manually input the dates certain versions were available on.