[R] passing a variable at the command line

Marc Schwartz marc_schwartz at comcast.net
Wed Jul 2 02:27:30 CEST 2008


on 07/01/2008 06:49 PM Erin Hodgess wrote:
> Dear R People:
> 
> I have used
> 
> ./R --no-save -q -f e.in >stuff.out
> 
> with great success on SUSE10.1 with R-2.7.1.
> 
> My question is, please:  is there a way to pass in a variable to the
> e.in file?  I'm fairly sure that the answer is no, but thought I'd
> double check.
> 
> Thanks in advance,
> Sincerely,
> Erin

Erin,

If I am correctly interpreting what you are looking to do, see:

   ?commandArgs

which will let you access the command line as passed to R when invoked 
and then be able to parse out parameters. This, used in combination with 
the '--args' argument to R, can be useful.

For example, let's say that your 'e.in' contains:

   ARGS <- commandArgs(trailingOnly = TRUE)

   as.numeric(ARGS[1]) ^ as.numeric(ARGS[2])


and you then use the following invocation of R from the command line:

   $ R --no-save --slave < e.in > stuff.out --args 3 5

The result generated is:

$ cat stuff.out
[1] 243

which of course is 3 ^ 5.

HTH,

Marc Schwartz



More information about the R-help mailing list