[R] passing command line arguments to 'R CMD BATCH myScript.R'

WeiWei Shi helprhelp at gmail.com
Fri Feb 25 19:00:45 CET 2005


Hi,
I recently solved the problem: I ran a program from Linux. The basic
idea is using enviroment variables and ?Sys.getenv

This is a general approach to calling R from a script file.

Here is part of my codes and explanation:

# assign some values to the arguments
dvar=5
categorical=3
....

# export them as env. var.
export dvar mod_min mod_max tree_no cp categorical

#run your r program in a batch mode
R CMD BATCH < $HOME/r/batch/mk_trees.r

The following is part of my mk_trees.r:
#load arguments
cargs<-Sys.getenv(c('dvar','mod_min','mod_max','tree_no','cp','categorical'))
dvar<-as.numeric(cargs[1])
mod_min<-as.numeric(cargs[2])
mod_max<-as.numeric(cargs[3])
tree_no<-as.numeric(cargs[4])
cp<-as.numeric(cargs[5])
cc<-strsplit(cargs[6], "\n")
categorical<-as.numeric(cc[[1]])


HTH,

Ed

On Fri, 25 Feb 2005 14:57:57 +0000, Oleg Sklyar <osklyar at ebi.ac.uk> wrote:
> Hi Community,
> 
> I have a question about how to pass command line parameters to R script
> running in the batch mode. The problem is: there is a banch of data
> files which are to be processed by R script called from a web-server,
> i.e. in the batch mode. The web server generates data files and passes
> their names calling 'R CMD BATCH' one by one for every file. Now the
> question is how to call 'R CMD BATCH myScript.R' with parameters, like
> file name to process? I know how to read the parameters passed to an R
> script, but I don't know how to pass them.
> 
> There is an option --args that should cut the rest of the line off the
> command line. The problem is however that BATCH syntax is: 'R CMD BATCH
> [options] inFile [outFile]', i.e. if I write
> 'R CMD BATCH --args myDataFile myScript.R' or 'R CMD BATCH --args
> myDataFile < myScript.R' (similar was posted on some R help under
> Windows) it is not going to work because then BATCH doesn't know about
> myScript.R - it is considered as a parameter, so only 'R CMD BATCH' is
> executed. If however I use 'R CMD BATCH myScript --args myDataFile' then
> R understands --args as an output file and generates a file with that name.
> 
> Does anyone has a solution for the problem?
> 
> Best regards
> Oleg
> 
> --
> Dr Oleg Sklyar
> European Bioinformatics Institute
> Wellcome Trust Genome Campus
> Hinxton, Cambridge, CB10 1SD
> England
> phone/fax  +44(0)1223 49 4478/4468
> e-mail osklyar at ebi.ac.uk
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list