R Environment Setup and Package Management¶
This guide provides general instructions for creating an isolated R environment, which is separate from the system-wide R installation. Instructions for installing and upgrading R system-wide are not covered in this guide.
Prepare R environment¶
This approach creates an isolated R environment.
Load Mamba / Conda¶
ScienceCluster users¶
module load mamba
ScienceCloud users¶
Refer to the micromamba installation documentation in the generic guide on Conda.
Create an R environment¶
mamba create -n renv -c conda-forge r-base -y
where
-n renv: names the environmentrenv.-c conda-forge: uses theconda-forgechannel for up-to-date R packages.r-base: installs the base R interpreter.-y: automatically confirms prompts.
To install a specific version of R:
mamba create -n renv -c conda-forge "r-base==4.2.2" -y
Activate the environment¶
source activate renv
renv. Deactivate the environment¶
mamba deactivate
Package management¶
Install packages using the R interactive session¶
Once the R environment is activated, start an interactive R session by running:
R
Once the interactive R session has started, verify that all required packages are installed and available in your environment.
To view the installed packages, run the following command in R:
installed.packages()
If additional packages are needed, install them from the interactive R session using install.packages().
For example, to install the ggplot2 package:
install.packages("ggplot2")
Note: these packages are installed inside renv, separate from system R (if system R is available).
After installing all required packages, exit the interactive R session. To quit R, run:
q()
When prompted to save the workspace image, you can safely choose n.
By default, packages are installed to the default user library. Using the lib argument of the install.packages() function, you can instead specify an alternative location for your R packages installation.
For example, you can install ggplot2 package to a custom_directory:
install.packages('ggplot2', lib=paste("custom_directory",sep=""))
A specific example of installing R packages to a /data directory on ScienceCluster is provided in the ScienceCluster section to help users manage large package libraries outside their limited home directory space.
Install packages using Mamba¶
Alternative to R interactive session, you can also use mamba to install R packages as precompiled binaries:
mamba install r-ggplot2 r-dplyr
Backing up and restoring R packages¶
When upgrading R or moving between environments, you can save your installed package list and reinstall it. This works with system-wide R and Mamba environments renv.
Save installed packages (before upgrade)¶
From inside R:
packagesToReInstall <- installed.packages()
save(packagesToReInstall, file = "packagesToReInstall.RData")
This code creates an object containing all installed package metadata and saves it to packagesToReInstall.RData in your home directory.
Upgrade R¶
source activate renv
mamba update r-base
Restore R packages (after upgrade)¶
To default user library¶
From inside R:
load("packagesToReInstall.RData")
install.packages(rownames(packagesToReInstall), dep = TRUE)
This code loads your saved package metadata and reinstalls all packages (dependencies included) into your user library by default.
To a shared system library¶
If you want system-wide installation, reinstall into /usr/lib/R/library:
sudo R
install.packages(rownames(packagesToReInstall), dep = TRUE, lib = "/usr/lib/R/library")
Note: /usr/lib/R/library is protected, is used as the default location of the base R packages; you must run R as root using sudo R.
Info
ScienceCluster users: System-wide library installation is not available.
ScienceCloud users: Users who opt for a system-wide R installation can install libraries into protected /usr/lib/R/library using sudo.