Running R script through qsub

user2763361 picture user2763361 · Oct 17, 2013 · Viewed 7.9k times · Source

I am trying to run an R script called test.r through qsub. My R script is as follows:

#!/usr/bin/Rscript
x <- 1
write.csv(x,"test.csv")

If in Ubuntu terminal I type R CMD BATCH test.r, then the script behaves as planned; test.csv gets exported in the same directory.

However if I create a bash script called testbash.sh and run it through the command qsub testbash.sh; it will run without errors but the output won't be there.

#!/usr/bin/bash
R CMD BATCH test.r

How to fix this?

Answer

Ricardo Oliveros-Ramos picture Ricardo Oliveros-Ramos · Mar 7, 2014

Try modifying your R script to:

#!/usr/bin/Rscript
x <- 1
print(getwd())
write.csv(x,"test.csv")

When you run a script via qsub, the script is normally running in another server, and by default as in your home directory. You need to change to the original directory in your script, there is a variable PBS_O_WORKDIR for that:

#!/usr/bin/bash
#PBS -N qsub_R_test
echo We are in the $PWD directory
cd $PBS_O_WORKDIR
echo We are now in $PBS_O_WORKDIR, running an R script.
R --vanilla < test.r > test.log 2> test.log

I normally cannot use R CMD BATCH, but redirection to R -vanilla works. You can also specify options for the PBS in the script, starting with #PBS, like the job name in this case (qsub_R_test).

You can get a more detailed list of qsub parameters here: http://www.csc.fi/english/pages/louhi_guide/batch_jobs/commands/qsub

And an example of a PBS script here: http://bose.utmb.edu/Compu_Center/Cluster_users/PBS%20HOWTO/PBS_HOW_TO.html