2

Possible Duplicate:
How can I read command line parameters from an R script?

I've got a R script for which I'd like to be able to supply several command-line parameters (rather than hardcode parameter values in the code itself). The script runs on linux.

I can't find out how how to read the R.script on the command line Bash.

sh file

cd `dirname $0`
/usr/lib64/R/bin/R --vanilla --slave "--args input='$1' input2='$2' output='$3'"  file=/home/lvijfhuizen/galaxy_dist/tools/lisanne/partone.R  $3.txt

R file

args <- commandArgs()
file <- read.csv(args[8],head=TRUE,sep="\t")   
annfile <- read.csv(args[9],head=TRUE,sep="\t")
Community
  • 1
  • 1
lisanne
  • 75
  • 1
  • 2
  • 5
  • 2
    See also http://stackoverflow.com/search?q=[r]+script+command+line+parameters It's not the best search function at hand, but it does work sometimes... – Joris Meys Mar 09 '11 at 15:23

1 Answers1

5

To source a R script from the command line, you can pipe it into R with <.

For example, if I create the following test.sh bash script :

#!/bin/bash

rfile=$1
shift
R --vanilla --slave --args $* < $rfile
exit 0

where test.R is the following R script in the same directory :

print(commandArgs(trailingOnly=TRUE))

Then running the script with test.R as first argument and possibly others will give something like this :

$ ./test.sh test.R foo bar 1 2 3
[1] "foo" "bar" "1"   "2"   "3"  

EDIT : another way, maybe cleaner, is to use the dedicated Rscript command. Then you can put directly in your bash script something like :

rfile=$1
shift
Rscript $rfile $*

which should give the same results.

juba
  • 47,631
  • 14
  • 113
  • 118