Skip to content

R

R is an open-source programming language and environment for statistical computing and data visualization. Due to its rich ecosystem of packages, it is frequently used in research environments, bioinformatics, economics, social sciences, and other scientific disciplines.

For preparing and editing R scripts on HPC Vega, the most practical approach is to use the Visual Studio Code (VS Code) editor. You can connect to HPC Vega directly from VS Code via the Remote SSH extension. This allows you to edit files directly on the cluster. This means you can iteratively modify code, review execution results, and submit SLURM jobs from the same workspace, without needing to transfer files between your local computer and the cluster. Instructions for setting up Remote SSH in VS Code can be found here.

For working with R in Visual Studio Code, it is recommended to install the R extension and the R Debugger extension.

Running R Scripts

1. Check available R modules

module avail R/

Lists the installed versions of R on the cluster.

2. Load the module

module load R/<version>

Loads the selected version of R, replacing <version> with the desired version.

3. Verify that the module is working

R --version

4. Set the package location

R packages should not be installed in the system library by default, so we create our own directory for packages:

mkdir -p ~/R/library

5. Install packages

Start the R command line:

R

Within R, set the path to your library and install the package:

.libPaths(c("~/R/library", .libPaths()))
install.packages("<package_name>", repos = "https://cloud.r-project.org")

Exit the R command line by pressing Ctrl + D.

You must add the path to your library at the beginning of every R script you run:

.libPaths(c("~/R/library", .libPaths()))

6. Run the R script

Run the script using the SLURM job scheduler. Below is an example .sh script for submitting the job:

#!/bin/bash
#SBATCH --job-name=r_job
#SBATCH --output=r_job_%j.out
#SBATCH --error=r_job_%j.err
#SBATCH --partition=cpu
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=00:15:00

module load R/<version>

Rscript my_script.R

Submitting the job:

sbatch my_job.sh

Check the job status with the command:

squeue --me

Other useful commands

installed.packages()                  # list of installed packages
remove.packages("<package_name>")     # remove a package
update.packages()                     # update all packages

Usage Example

In your working directory, prepare a script named analysis.R that generates a dataset (1 million random values) and visualizes their distribution:

.libPaths(c("~/R/library", .libPaths()))
library(ggplot2)
library(hexbin)            

set.seed(42)

df <- data.frame(
  x = rnorm(1e6),
  y = rnorm(1e6)
)

cat("Number of observations:", nrow(df), "\n\n")
print(summary(df))

p <- ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  labs(
    title = "Density of 1 Million Data Points",
    x = "X",
    y = "Y"
  ) +
  theme_minimal()

ggsave("density_plot.png", p, width = 8, height = 6, dpi = 300)

cat("Analysis completed successfully.\n")

Prepare an SBATCH script named r_analysis.sh to submit the job via SLURM:

#!/bin/bash
#SBATCH --job-name=r_analysis
#SBATCH --output=r_analysis_%j.out
#SBATCH --error=r_analysis_%j.err
#SBATCH --partition=cpu
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=00:15:00

module load R

Rscript analysis.R

Submit the job with the command:

sbatch r_analysis.sh

You can check the job status with the command:

squeue --me

Once the job completes, the density_plot.png graph and the .out and .err log files will be created in your working directory.