[R] How identify args into R, sent from a command line.
Steve Lianoglou
mailinglist.honeypot at gmail.com
Wed Mar 23 23:47:52 CET 2011
On Wed, Mar 23, 2011 at 4:15 PM, Ortiz, John <OrtizJ at si.edu> wrote:
> Hi,
>
> For example, I have several variables in php, like
>
> var1 = 1, 2, 3, 5, 6
> var2 = 3, 1, 8
> var3 = 8, 10, 4, 0, 9, 1
>
> I sent the arguments to R, with this line:
>
> '/usr/bin/R --vanilla --slave --args '.$var1.' '.$var2.' '.$var3.' < script.r'
>
> In my "script.r" I can read the arguments whit this line:
>
> args=(commandArgs(TRUE))
> args=as.numeric(args)
>
> args = 1, 2, 3, 5, 6, 3, 1, 8, 8, 10, 4, 0, 9, 1
>
> I'm looking the way to catch into de R, just the arguments var2
>
> something like
>
> args = 3, 1, 8
>
> I know that is possible index with
>
> args[6:8]
>
> But, the problem is that the length of (var1, var2, var3) might be variable,
> not always var2 is args[6:8]
Given this exact scenario (variable length var1/2/3), how would you
get any language (not just R) to parse this command line call
correctly?
$ script.r 1 2 3 5 6 3 1 8 8 10 4 0 9 1
Into what you want?
While you noodle on that for a bit, you might try another approach,
and use command line switches. For instance, what if you invoked your
script form the command line like this, instead?
$ script.r --arg1=1,2,3,5,6 --arg2=3,1,8 --arg3=8,10,4,0,9,1
Should be easy enough to do from within your php script too, no?
If that seems acceptable to you, then take a look at R's optparse library:
http://cran.r-project.org/web/packages/optparse/index.html
Another approach would be to skip optparse and just call your script
slightly differently. Maybe like so:
$ script.r "1,2,3,5,6" "3,1,8" "8,10,4,0,9,1"
Now after you do your commandArgs(TRUE) maneuver, args[0] (or is it
1?) should have "1,2,3,5,6", which you can split and convert to
integers if necessary.
HTH,
-steve
--
Steve Lianoglou
Graduate Student: Computational Systems Biology
| Memorial Sloan-Kettering Cancer Center
| Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
More information about the R-help
mailing list