[R] Running Shell Script with R

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Nov 28 19:24:31 CET 2011

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")
  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:



On Mon, Nov 28, 2011 at 11:10 AM, jp134711 <johnandrewpura at gmail.com> wrote:
> 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.
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

More information about the R-help mailing list