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, launch an interactive session and load the newest R module on Andromeda:

[johnchris@andromeda ~]$ interactive
Executing: srun --pty -N1 -n1 -c4 --mem=8g -pinteractive -t0-04:00:00 /bin/bash
Press any key to continue or ctrl+c to abort.
(press any key)
srun: job 585396 queued and waiting for resources
srun: job 585396 has been allocated resources
cpu-bind=MASK - c004, task 0 0 [946829]: mask 0x888800000000 set
[johnchris@c004 ~]$ module load R

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:

[johnchris@c004 ~]$ cd ~
[johnchris@c004 ~]$ mkdir Rlib
[johnchris@c004 ~]$ R
R version 4.5.0 (2025-04-11) -- "How About a Twenty-Six"
Copyright (C) 2025 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.

>

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

> install.packages("readr", lib="/home/your-user-name/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 ’72: USA (PA 1)’ in Pennsylvania is a good choice. The package will then be installed in /home/your-user-name/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. You will need to set this whenever you run an R script, either interactively or in a slurm batch file.

Interactively:

> q()
Save workspace image? [y/n/c]: y
[johnchris@c004 ~]$ export R_USER_LIBS=$R_USER_LIBS:/home/your-user-name/Rlib
# execute your script

In a batch file:

#!/bin/bash
#SBATCH --job-name=R_test
#SBATCH --partition=short
#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=1G
module load R
cd /home/your_username/your_R_project_directory
export R_USER_LIBS=$R_USER_LIBS:/home/your-user-name/Rlib
R CMD BATCH hello.R

And the corresponding R script file hello.R:

# hello.R
# ----------------------------
cat("Hello, world!\n")
cat("Counting from 1 to 5:\n")
for (i in 1:5) {
cat(i, "\n")
}
my_vector <- c(10, 20, 30, 40, 50)
cat("Here is a vector:\n")
print(my_vector)

Scroll to Top