Skip to content

How to Call an Outside Program with in a R script

Neranjan Perera edited this page Oct 29, 2019 · 1 revision

Calling Programs with in a R Script (when using R container)

In Xanadu cluster some R versions are installed as a Singularity Containers.

When you want to call programs which are installed as modules, this becomes little bit tricky. In such cases you need to add the environment variables of the specific program to the path.

e.g: You may want to use hmmscan which is a part of the hmmer/3.2.1 module file

In your R script lets say hmmscan.R:

#!/isg/shared/apps/R/3.6.0/R

old_path <- Sys.getenv("PATH")
print(old_path)
system("hmmscan -h")
Sys.setenv(PATH = paste(old_path, "/isg/shared/apps/hmmer/3.2.1/bin", sep = ":"))
print(Sys.getenv("PATH"))
system("hmmscan -h") 

Then if you are using a BASH script, then the bash script you need can call the hmmscan.R program using:

#!/bin/bash
#SBATCH --job-name=R_program
#SBATCH -n 1
#SBATCH -N 1
#SBATCH -c 1
#SBATCH --mem=1G
#SBATCH --qos=general
#SBATCH --partition=general
#SBATCH -o %x_%j.out
#SBATCH -e %x_%j.err

hostname
date

module load R/3.6.0
R CMD BATCH hmmscan.R 

Using a NON-Container R version

If you are using a non-container version of R (eg: R/3.5.1), then you can load the module files during your run and the R will be able to see them.

So your R script (hmmscan.R) will be like:

#!/isg/shared/apps/R/3.5.1/bin/R

old_path <- Sys.getenv("PATH")

print(old_path)

system("hmmscan -h")

In the batch script you can load the module files as:

#!/bin/bash
#SBATCH --job-name=my_R_program
#SBATCH -n 1
#SBATCH -N 1
#SBATCH -c 1
#SBATCH --mem=1G
#SBATCH --qos=general
#SBATCH --partition=general
#SBATCH -o %x_%j.out
#SBATCH -e %x_%j.err

hostname
date

module load R/3.5.1
module load hmmer/3.2.1 

R CMD BATCH hmmscan.R

date