Running an R file with arguments from a bash script
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.
lisanne
Updated on June 14, 2022Comments
-
lisanne almost 2 years
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")