[R] R BATCH script & reading command line options ? la argc() & argv()

Rik Bradt cropdesign at xs4all.be
Sun Feb 24 23:47:20 CET 2002


At 01:40 PM 2/24/2002 -0600, Dirk Eddelbuettel wrote:
>On Sun, Feb 24, 2002 at 06:53:28PM +0100, Rik Bradt wrote:
> > I'm writing a R BATCH script on linux that connects to a MS-sqlserver to
> > fetch its data (=cross-platform).
> > After the processing in R it does updates in the MS-sqlserver.
>
>Why don't you install R on Windows and then use RODBC to access it?

Because we like linux. Database connection is not really an issue here

> > I want to be able to pass parameters to the R BATCH script to control 
> which
> > data it has to process.
> > Is it possible to read the command line parameters passed to R in a BATCH
> > script?
>
>Sort of. This gets discusses on the list every once in a while, please do
>search the archive.
The archive does indeed contain some hints

http://www.r-project.org/nocvs/mail/r-help/2001/1045.html
http://www.r-project.org/nocvs/mail/r-help/2000/2213.html
http://www.r-project.org/nocvs/mail/r-help/1999/1447.html

The problem is that commandArgs() is not really behaving the way I want.

Interactively, everything is ok:

------------------------- snip -------------------------
rikbradt at pc184:/usr/local/httpd/htdocs> /opt/R-1.4.0/bin/R bla1 bla2 bla3
ARGUMENT 'bla1' __ignored__
ARGUMENT 'bla2' __ignored__
ARGUMENT 'bla3' __ignored__

R : Copyright 2001, The R Development Core Team
Version 1.4.0  (2001-12-19)

...
Type `q()' to quit R.

[Previously saved workspace restored]

 > commandArgs()
[1] "/opt/R-1.4.0//lib/R/bin/R.bin" "bla1"
[3] "bla2"                          "bla3"
 >
------------------------- /snip -------------------------

But in a Batch (this is the essential part of the question!) those command 
line arguments get stripped!
This renders the function commandArgs() useless.

I did some research and it seems that the executable I normally use is 
nothing more than a bunch of shell-scripts.
These scripts seem to remove the command line options that are essential 
for me.
It seems cynical but a hint for a solution is in the above log.

Instead of using /opt/R-1.4.0/bin/R I could use "/opt/R-1.4.0//lib/R/bin/R.bin"

         /opt/R-1.4.0/lib/R/bin/R.bin --vanilla < test.R

worked for me, with the environment variable R_HOME=/opt/R-1.4.0/lib/R set. 
It wasn't necessary to set LD_LIBRARY_PATH.

------------------------- snip -------------------------
argc <- function() {
         args <-  commandArgs()
         return(length(args))
}

argv <- function(x) {
         args <-  commandArgs()
         return(args[x])
}
------------------------- /snip -------------------------

So, it looks as if the problem is solved.

Thanks for your time anyhow.

Rik

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list