Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jmfernandez/96287b6b6c3609e9cb1d299e3eb63e37 to your computer and use it in GitHub Desktop.
Save jmfernandez/96287b6b6c3609e9cb1d299e3eb63e37 to your computer and use it in GitHub Desktop.
Creating reproducible R environments with renv

Another way to be able to record all the exact versions of the R packages being used (even BioConductor ones), and install them later, is using renv.

This gist shows an example where CRAN and Bioconductor packages are installed and recorded.

Later, the minimum information is put in a different directory (and machine), and all the installed packages and their dependencies are installed, using the same, exact version used when it was previously installed.

#!/usr/bin/Rscript
# This option is needed to finish environment isolation from the cache
# when renv::isolate() is run
options(renv.consent = TRUE)
# First, install renv (if it is needed!)
if (!requireNamespace("renv", quietly = TRUE)) {
install.packages("renv")
}
# We are declaring the directory where to install
# all of these packages.
# If we are already in the project directory, the path is not needed.
# To start from a clean environment, use, bare=TRUE. Otherwise, renv::init
# will scan all the already installed packages, in order to add them to
# the newly created environment
renv::init("~/RATAMAN2",bare=TRUE,restart=TRUE)
# if the renv project already exists, instead of the initialization
# you activate it with 'activate'
# Remember: if we are already in the project directory,
# the path is not needed
renv::activate("~/RATAMAN2")
# A CRAN package is installed
# It can also be installed using traditional method, but it is better
# in this way
renv::install("reshape2")
# Bioconductor itself must be recorded
if (!requireNamespace("BiocManager", quietly = TRUE)) {
install.packages("BiocManager")
}
# This line adding the Bioconductor repositories is needed,
# in order to have the URLs to their repos in the
# reproducible snapshot
options(repos=BiocManager::repositories())
# You have to uncomment next line if you want packages to be tied to an
# specific Bioconductor release
# BiocManager::install(version = "3.11")
# Now, installing several packages from Bioconductor
# using renv::install is as easy as giving a prefix
renv::install(c("bioc::GenomicFeatures", "bioc::AnnotationDbi"))
# At last, the snapshot is generated
renv::snapshot(prompt=FALSE)
#!/usr/bin/Rscript
# This option is needed to finish environment isolation from the cache
# once it is restored
options(renv.consent = TRUE)
if (!requireNamespace("renv", quietly = TRUE)) {
install.packages("renv")
}
# If we are already in the project directory, the path is not needed
# renv::activate("~/newRATA")
# For projects using only standard repos, it would be something like
renv::restore(prompt=FALSE)
# When Bioconductor packages are involved, a two step procedure is needed
# First: restore the Biocmanager, in order to use the repositories
renv::restore(packages=c('BiocManager'),prompt=FALSE)
# Second: take into account the Bioconductor repositories
renv::restore(repos=BiocManager::repositories(), prompt=FALSE)
# At last, isolate
renv::isolate()
# From this point, the environment has all the dependencies,
# and it is ready to be used
#!/bin/bash
# At this point, you should have an environment at `~/RATAMAN2`.
# You should copy from inside that directory only a few files.
# .Rprofile , renv.lock , renv/activate.R , and renv/settings.dcf
# Let's create the new directory for the reproduced environment.
# For instance, `~/newRATA`
mkdir -p ~/newRATA ~/newRATA/renv
# Now, let's copy the minimum information needed to regenerate
# the environment
cp -p ~/RATAMAN2/{.Rprofile,renv.lock} ~/newRATA
cp -p ~/RATAMAN2/renv/{activate.R,settings.dcf} ~/newRATA/renv
# Last, you have to run next code from an R console
# cd ~/newRATA
# R -f populate-renv-environment.R
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment