The Reproducible R Toolkit provides tools to ensure the results of R code are repeatable over time, by anyone. Most R scripts rely on packages, but new versions of packages are released daily. To ensure that results from R are reproducible, it’s important to run R scripts using exactly the same package version in use when the script was written.
The Reproducible R Toolkit provides an R function checkpoint, which ensures that all of the necessary R packages are installed with the correct version. This makes it easy to reproduce your results at a later date or on another system, and makes it easier to share your code with the confidence that others will get the same results you did.
The Reproducible R Toolkit originally worked with Microsoft’s checkpoint server. Microsoft retired MRAN in 2023.
RRT is a collection of R packages and the checkpoint server that together make your work with R packages more reproducible over time by anyone.
The default snapshot source is Posit Public Package
Manager. Its CRAN snapshots start on 2017-10-10. If the
requested date has no snapshot, Posit serves the most recent preceding
snapshot. Posit and MRAN snapshots may differ even for the same date. To
use an MRAN-compatible mirror, set
options(checkpoint.mranUrl = "https://your-mirror.example")
or pass mran_url to checkpoint().
The goal of the checkpoint package is to solve the
problem of package reproducibility in R. Since packages get updated on
CRAN all the time, it can be difficult to recreate an environment where
all your packages are consistent with some earlier state. To solve this
issue, checkpoint allows you to install packages locally as
they existed on a specific date from the corresponding snapshot (stored
on the checkpoint server) and it configures your R session to use only
these packages. Together, the checkpoint package and the
checkpoint server act as a CRAN time machine so that anyone using
checkpoint() can ensure the reproducibility of their
scripts or projects at any time.
The checkpoint package has 3 main functions.
The create_checkpoint function
~/.checkpoint by default, but you can
change its location.library() and
require() calls, as well as the namespacing operators
:: and :::.The use_checkpoint function
options(repos)create_checkpoint, i.e. modifies
.libPaths()This means the remainder of your script will run with the packages from your specified date.
Finally, the checkpoint function serves as a unified
interface to create_checkpoint and
use_checkpoint. It looks for a pre-existing checkpoint
directory, and if not found, creates it with
create_checkpoint. It then calls
use_checkpoint to put the checkpoint into use.
To reset your session to the way it was before checkpointing, call
uncheckpoint(). Alternatively, you can simply restart
R.
To update an existing checkpoint, for example if you need new
packages installed, call create_checkpoint() again. Any
existing packages will remain untouched.
The functions delete_checkpoint() and
delete_all_checkpoints() allow you to remove checkpoint
directories that are no longer required. They check that the
checkpoint(s) in question are not actually in use before deleting.
Each time create_checkpoint() is run, it saves a series
of json files in the main checkpoint directory. These are outputs from
the pkgdepends package, which checkpoint uses
to perform the actual package installation, and can help you debug any
problems that may occur.
<date>_<time>_refs.json: Packages to be
installed into the checkpoint<date>_<time>_config.json: Configuration
parameters for the checkpoint<date>_<time>_resolution.json: Dependency
resolution result<date>_<time>_solution.json: Solution to
package dependencies<date>_<time>_downloads.json: Download
result<date>_<time>_install_plan.json: Package
install plan<date>_<time>_installs.json: Final
installation resultFor more information, see the help for
pkgdepends::pkg_installation_proposal.
First, create a new folder and change your working directory to this folder. If you use an IDE like RStudio, this is identical to creating a new RStudio project. Otherwise, or alternatively, you can do it in code:
Next, add a script to the project folder, adding the snippet
mentioned above to the top. Here’s a simple example, using the
prettyunits package. Save this file in the
~/temp_project folder as script1.R.
When you run this script, checkpoint will create the
checkpoint by scanning your project for packages, and then downloading
them from the CRAN snapshot for 2020-01-01. As this
happens, you should see a number of messages appear in your R window
that tell how the installation is proceeding. It then sets the library
search path and CRAN mirror for your session, to point to the local
checkpoint directory and dated CRAN snapshot.
After running the above script, you can verify that the checkpointing has succeeded:
## CRAN
## "https://packagemanager.posit.co/snapshot/2020-01-01"
## [1] "C:/Users/hongo/Documents/.checkpoint/2020-01-01/lib/x86_64-w64-mingw32/3.6.2"
## [2] "C:/Program Files/R/R-3.6.2/library"
## [1] "prettyunits"