Skip to content

Running Shell Script with R

3 messages · jp134711, Steve Lianoglou

#
I'm having some trouble getting my shell script to work. I've checked out the
Intro to R Manual and a host of other websites, but I still can't get the
script to work when I submit the job to the cluster.

Here is my main R code:

##Load Libraries
##...

## Load Time Data
Args <- commandArgs(trailingOnly = TRUE);
print(Args);
timeDat <- read.flowSet(files=NULL, path=Args[1]);
save(timeDat,file = Args[2]);

Here is my shell script file


#!/bin/bash
#$ -cwd
#$ -q all.q

R --slave --vanilla --file="/home/jpura/Desktop/FDA_Trial_Data/testscript.R"
--args "/home/jpura/Desktop/FDA_Trial_Data/Experiment_1/"
"/home/jpura/Desktop/FDA_Trial_Data/testscript.RData"

It runs just fine (i.e. I get the right output file, which is
testscript.RData) when I type the lines in the shell script file directly
into the command prompt. However, when I try to submit the file using:

qsub "path/to/testscript.sh", I don't get the desired output file.

Any insights to this is be greatly appreciated.

Thanks,
John


--
View this message in context: http://r.789695.n4.nabble.com/Running-Shell-Script-with-R-tp4115734p4115734.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi John,

Hard to say (for me) what's going on ... is there any errors in any
relevant logs anywhere?

Also, note that you don't have to write a bash script to run your R
script .. you can write a script with a shebang like so:

=====================
#!/usr/bin/env Rscript

## R code here.
=====================

And the R code will run as a command line script .. maybe that can
prove helpful in your situation. When doing so, I find it helpful to:

(1) To set R to exit when some error happens in your code you didn't
account for. In "shell/Rscript" mode (or whatever we call it), I've
found that when an error happens, the script just goes through to the
next command. To get R to do so, I set the appropriate options() at
the top of my script like so:

options(error=function(err) {
  cat("An error happened you didn't account for\n")
  cat("\n\n")
  quit(save='no', status=1)
})

(2) I find it handy to use the optparse package from CRAN to help
parsing command line arguments and options/flags:

http://cran.r-project.org/web/packages/optparse/index.html

HTH,
-steve
On Mon, Nov 28, 2011 at 11:10 AM, jp134711 <johnandrewpura at gmail.com> wrote:

  
    
#
One more thing I probably overlooked:

On Mon, Nov 28, 2011 at 1:24 PM, Steve Lianoglou
<mailinglist.honeypot at gmail.com> wrote:
Perhaps your bash wrapper script is necessary for your cluster
submission? (I vaguely remember having to write this some time ago).

Anyway, perhaps the options(error=) thing may help, but you might also
have better luck posting this on the highperformance computing mailing
list here:

https://stat.ethz.ch/mailman/listinfo/r-sig-hpc

-steve