{renv}
{renv} helps us create more reproducible and shareable projects by allowing us to run code on different machines, across time, by different people. It essentially takes the packages used in your project and records their versions in a file that you can update as you develop the project. When you come back to your project, or share it with someone else, they are using the same versions of packages and R that you were.
The three main functions used when working with {renv} are:
init()
for setting up project to use {renv} for the first timesnapshot()
for recording versions of packages currently installed in the project in the filerenv.lock
restore()
for restoring a project’s package versions to match the versions recorded by {renv} from the filerenv.lock
Note: {renv} will not install a different version of R, or any other system dependencies that are required. However, {renv} will warn if the renv.lock
lists a version of R that differs from your current version of R. For system dependencies that are required but not installed, R will fail.
Setup
The first time you set up a project with {renv}, use the function init()
. init()
writes a file to the root of your project called renv.lock
that tracks the versions and dependencies of R packages installed and the version of R itself. This is a simple text file, so feel free to open it up (but don’t edit) in a text editor.
Exercise: setting up {renv}
Instruction: in the console of your project, initialize {renv} with init()
.
Usage
As you work on your project and add or update packages, record your packages used and their version with snapshot()
. This will edit the renv.lock
file with new versions and packages that describe the package’s environment.
You can always check the status of your {renv} project with status()
to see which packages are installed but not recorded, or which packages are recorded but no longer used.
Exercise: using {renv}
Instruction: install a new package with install.packages()
then run status()
. Follow {renv}’s recommendations to either save (snapshot()
) or discard (restore()
) these potential changes to your project’s environment.