R-Related Codes and Scripts
R code.
File linear-fit.R:
# Purpose: demonstrate simple plotting of x-y data and linear fit.
# Modules:
# module load R-bundle-CRAN/2025.11-foss-2025b
# Capture the trailing command line arguments
args <- commandArgs(trailingOnly = TRUE)
# Check if the user provided the required number of arguments
if (length(args) != 2) {
stop("Error: Please provide one argument (input_filename output_plot_filename).", call. = FALSE)
}
# Extract and parse your parameters (they always load as character strings)
# Command line arguments (CLAs).
input_filename <- args[1]
output_filename <- args[2]
# Load the data into a df.
df <- read.table(input_filename, sep = " ", header = TRUE)
# Print the data, to confirm.
print(" Contents of dataframe (df):")
print(df)
# Fit the model using the data argument
model_df <- lm(response ~ predictor, data = df)
# Print out only the calculated coefficients (Intercept and Slope)
print(" ")
print(" Fit parameters")
coefficients(model_df)
print(" ")
print(" Summary of model")
summary(model_df)
print(" ")
# Load the ggplot2 library (install first via install.packages("ggplot2") if needed)
library(ggplot2)
# Generate a scatter plot and overlay the linear fit line automatically
ggplot(df, aes(x = predictor, y = response)) +
geom_point(color = "blue", size = 3) +
geom_smooth(method = "lm", formula = y ~ x, color = "red", se = TRUE) +
theme_minimal()
# Save the plot to file.
ggsave(output_filename, width = 5, height = 5)
Sbatch script for batch job on Owl.
File sbatch.r.owl.genoa.slurm:
#!/bin/bash
## R on Owl.
## -----------------------
## ACCOUNT.
## The account to charge to.
## You will have your own accounts.
#SBATCH --account arcadm
## -----------------------
# SLURM JOB SCRIPT OPTIONS:
#SBATCH --job R-lin-fit
## -----------------------
## EXECUTION DURATION.
# Set the time, which is the maximum time your job can run in HH:MM:SS.
#SBATCH --time=0:10:00
## -----------------------
## NUM NODES AND CORES.
# A serial code needs 1 node, 1 task, and 1 cpu.
## Number of tasks.
#SBATCH --ntasks=1
## Number of tasks per node (can compute number of nodes).
#SBATCH --ntasks-per-node=1
## Number of cores (total) per task.
#SBATCH --cpus-per-task=1
## -----------------------
## JOB QUEUE/PARTITION AND CONSTRAINTS.
## Set the partition to submit to (a partition is equivalent to a queue)
#SBATCH --partition=normal_q
#SBATCH --constraint=avx512
## -----------------------
## MEMORY.
## If needed, request whole nodes by uncommenting the "#SBATCH --exclusive" line
## below.
## #SBATCH --exclusive
##SBATCH --mem=122G
## -----------------------
## SLURM OUTPUT AND ERROR FILES.
#SBATCH --output slurm.owl.genoa.r.linear.fit.%j.out
#SBATCH --error slurm.owl.genoa.r.linear.fit.%j.err
## -----------------------
## RESERVATION.
## #SBATCH --reservation=HPCMaintenance
## -----------------------
## MODULES.
module reset
module load R-bundle-CRAN/2025.11-foss-2025b
## -----------------------
## WORKING DIRECTORY.
cd $SLURM_SUBMIT_DIR
## -----------------------
## Record slurm conditions.
echo " "
echo "slurm scontrol:"
echo " "
scontrol show job --details $SLURM_JOB_ID
echo " "
echo " << end slurm scontrol >>"
echo " "
## -----------------------
## EXPORTS
## Exports and variable assignments.
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK
export MV2_ENABLE_AFFINITY=0
echo " SLURM_CPUS_PER_TASK: " $SLURM_CPUS_PER_TASK
echo " OMP_NUM_THREADS: " $OMP_NUM_THREADS
echo " MV2_ENABLE_AFFINITY: " $MV2_ENABLE_AFFINITY
echo " SLURM_NTASKS: " $SLURM_NTASKS
echo " SLURM_JOB_NUM_NODES: " $SLURM_JOB_NUM_NODES
## -----------------------
## JOB.
sh run.me.r
Bash script to launch R code from slurm script.
File run.me.r:
code=linear-fit.R
input_filename=in_data.inp
# output_filename=r.plot.out.pdf
output_filename=r.plot.out.png
# Rscript linear-fit.R in_data.inp plot.out.pdf
Rscript ${code} ${input_filename} ${output_filename}