Search code examples
rslurmrscript

Sys.getenv in R


cur_dir<-"C:/Users/child/Dropbox/Proteogenomics_analysis/Proteome and Phosphoproteome Data     Normalization/Experiment_data_20230222_Metabric_multistate_analysis"
setwd(cur_dir)
library(mstate)
library(brcarepred)
id <- as.numeric(Sys.getenv("SLURM_ARRAY_TASK_ID"))
print(id)
load("./Models/FourGroupsM.RData")
timepoints <- seq(from=0, to=20, by=0.25)
pt <- list()
newdata.DR <- newdata
newdata.DR$AGE.DR[which(newdata.DR$strata %in% seq(from=9,by=9, length=4))] <-newdata.DR$AGE.DR[which(newdata.DR$strata %in% seq(from=9,by=9, length=4))] +newdata.DR$TLastSurgery[which(newdata.DR$strata %in%seq(from=8, by=9, length=4))]pt[['DR']] <- getProbsDR(m, group=id, newdata.DR, timepoints=timepoints)`

I got error message.

pt[['DR']] <- getProbsDR(m, group=id, newdata.DR, timepoints=timepoints)
Error in group:(group + 8) : NA/NaN argument

I thought that this error may be resulted from

id <- as.numeric(Sys.getenv("SLURM_ARRAY_TASK_ID"))
print(id)
[1] NA

I do not understand "Sys.getenv("SLURM_ARRAY_TASK_ID") script". Before running this script, what file should I make? If possible, explain it with an example.


Solution

  • That code is meant to be run in a Slurm job array. Each job in the job array has a specific value defined for the $SLURM_ARRAY_TASK_ID environment variable.

    For instance, if the job was submitted with --array=1-10, there would be 10 independent jobs running, each with a distinct value for $SLURM_ARRAY_TASK_ID, from 1 to 10.

    The code retrieves that value to pass as the value of the group named argument to the getProbsDR function.

    If you would like to run that script outside the context of a Slurm job array, you can defined that variable by yourself, for instance with

    export SLURM_ARRAY_TASK_ID=1
    

    in Linux or MacOS before invoking the R interpreted, or

    SET SLURM_ARRAY_TASK_ID=1
    

    in Windows.

    If you are running it in RStudio, you can also try to run

    Sys.setenv(SLURM_ARRAY_TASK_ID = 1)
    

    before you source the code.

    Obviously, you can also simply hardcode the value of id directly in the script.