Search code examples
rcluster-computinghpc

Force load R packages while running the job in cluster


When I run a job in HPC cluster in interactive mode, I can load the packages and if it fails (not sure why some packages fail to load at first instance) to load, I can load it by running the library (failed package) multiple times, but when I do qsub my_rscript_job.pbs, the packages fail to load.

my my_rscript_job.pbs script is:

#!/bin/bash 
#PBS -l walltime=100:00:00
#PBS -l ncpus=1,mem=100g

source ~/.bashrc

Rscript /dmf/mypath/map.r -t 100

The packages I need to load in the map.r script are

library(biomaRt)
library(dplyr)
library(stringi)
library(GenomicFeatures)
library(Rsamtools)
library(foreach)
library(doMC)
library(doMC)

which I can load if I submit the job in interactive mode and submit the rscript directly to the terminal, but when I do qsub I get the following error:

Loading required package: methods
Warning messages:
1: package ‘biomaRt’ was built under R version 3.2.2 
2: In eval(quote({ : bytecode version mismatch; using eval
3: In .recacheSubclasses(def@className, def, doSubclasses, env) :
  undefined subclass "externalRefMethod" of class "expressionORfunction"; definition not updated
4: In .recacheSubclasses(def@className, def, doSubclasses, env) :
  undefined subclass "externalRefMethod" of class "functionORNULL"; definition not updated
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/dmf/bin/R/x86_64-redhat-linux-gnu-library/3.2/dplyr/libs/dplyr.so':
  /dmf/bin/R/x86_64-redhat-linux-gnu-library/3.2/dplyr/libs/dplyr.so: undefined symbol: Rf_installChar
In addition: Warning message:
package ‘dplyr’ was built under R version 3.2.2 
Error: package or namespace load failed for ‘dplyr’
Execution halted

Is there a way to force load the packages while running r as qsub?


Solution

  • Setting timer to reload each package until each package in the list is successfully loaded. There is a timer of 5 second to force load the package when running the qsub option.

     myPackages <- c("biomaRt", "dplyr", "stringi","GenomicFeatures","Rsamtools","foreach","doMC")
        tryCount <- 0    
    
        while( !all(myPackages %in% (.packages())) ){
    
          try(require(biomaRt))
          try(require(dplyr))
          try(require(stringi))
          try(require(GenomicFeatures))
          try(require(Rsamtools))
          try(require(foreach))
          try(require(doMC))
    
          tryCount <- tryCount + 1
    
          if( !all(myPackages %in% (.packages()))  ){
            cat(paste0("Failure: ", tryCount, "\n"))
            cat("Failed to load: ")
            cat(myPackages[ !myPackages %in% (.packages()) ])
            cat("\n")
          } else {
            print(paste0("Success!"))
          }
    
          Sys.sleep(5)
    
        }