Skip to content

commandArgs()

2 messages · R A F, Brian Ripley

#
Apologies for asking about this, but I don't quite understand how
this works after looking through the FAQ and the Help archives.

Let's say I want to pass "1000" as an argument to R.  I did the
following:
When I do print( commandArgs() ), I see

[1] ".../R.bin"         "--restore"
[3] "--save"            "--no-readline"
[5] "gui=none"          "--1000"

So commandArgs()[6] is the argument I want.  Does one then go on and
remove the "--" manually and cast this as numeric?  If so, what's the
usual way to do this?  Or am I not understanding this mechanism
properly?

Thanks very much.
#
At present there is no official way to add arguments to R: commandArgs() 
is intended to let you retrieve the standard arguments.

This has been discussed for a long time, but there are problems with doing 
it consistently across platforms: for example Windows allows an extra 
argument, the name of a .RData file, to allow file-association to work.

What you can do reliably across platforms is to pass information in 
environment variables.  Something like

NRUNS=1000 R --vanilla < test.R > test.Rout   (Unix, sh)
Rterm NRUNS=100 --vanilla < test.R > test.Rout (Windows)

and have

nruns <- as.numeric(Sys.getenv("NRUNS"))

in the script test.R
On Mon, 5 May 2003, R A F wrote:

            
There is no such mechanism at present.