Installing R Packages Locally

If possible, it is recommended  to install the R package in your home directory. This allows you to easily switch between versions of R libraries and applications for different projects. This also helps avoid potential conflicts. 

To start, load the newest R module on Andromeda with

module load R/4.4.0gnu9.2.0

You will need to create a directory to install your R packages. Here, we will create a directory called “Rlib” in your home directory and run the R executable:

cd ~
mkdir Rlib
R

You can install packages using packages.install. Here, we will install the “readr” package:

install.packages(“readr”, lib=”/data/johnchris/Rlib”)

You will need to select a CRAN mirror. Choose one which is relatively close to your geographic location. If you are in Boston, then 71 in Pennsylvania is a good choice. The package will then be installed in /data/johnchris/Rlib. 

[1001]$ module load R/4.4.0gnu9.2.0
[1001]$ mkdir Rlib
[1001]$ R

R version 4.4.0 (2024-04-24) -- "Puppy Cup"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>install.packages("readr", lib="/data/Rlib")

In order to use your package, you need to set R_USER_LIBS environment variable, which is a list of directories that R will check for requested packages:

export R_USER_LIBS=$R_USER_LIBS:/data/johnchris/Rlib

You will need to set this whenever you run an R script, either interactively or in a slurm batch file.

Interactively:

interactive
export R_USER_LIBS=$R_USER_LIBS:/data/johnchris/Rlib
# execute your script

In a batch file:

#!/bin/bash
#SBATCH --job-name=R_test
#SBATCH --partition=shared
#SBATCH --time=00:10:00
#SBATCH --output=slurm_%x_%j.output
#SBATCH --error=slurm_%x_%j.err
#SBATCH --ntasks=1
#SBATCH --nodes=1
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=500mb

module load R/4.4.0gnu9.2.0
export R_USER_LIBS=$R_USER_LIBS:/data/johnchris/Rlib
# execute your script here

Scroll to Top