Re: [R] Rscript example
On Thu, Mar 01, 2012 at 11:18:33PM -0800, statquant2 wrote: Hey Petr, ok I was thinking that R would handle the split by itself. I guess using eval we can even make arg1=val1 being executed by R. Hi. For executing the assignments, try myRscript.R containing args - commandArgs(TRUE); argmat - sapply(strsplit(args, =), identity) for (i in seq.int(length=ncol(argmat))) { assign(argmat[1, i], argmat[2, i]) } # available variables print(ls()) # print variables arg1, arg2, arg3 print(arg1) print(arg2) print(arg3) Then the command line ./myRscript.R arg1=aa arg2=22 arg3=cc yields [1] argmat args arg1 arg2 arg3 i [1] aa [1] 22 [1] cc Another option for setting some variables before executing an R script is to have two scripts, a shell script and an R script, containing shell script callR.sh #!/bin/bash PATH_TO_R/bin/R --vanilla EEE args - c($2,$3,$4,$5) source($1) EEE a trivial R script tst.R print(args) Then, ./callR.sh tst.R aa 22 cc leads to ... ... Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. args - c(aa,22,cc,) source(tst.R) [1] aa 22 cc Hope this helps. Petr Savicky. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Rscript example
Hi there, I am trying to find an example how to use Rscript Let's suppose I want to pass 3 arguments (I don't want [options] and -e [expressions] as described in help) *on the command line myRscript.R -arg1=value1 -arg2=value2 -arg3=value3 *In the script #! /path/to/Rscript args = commandArgs(TRUE); From what I see args is just a string, do I do things correctly ? -- View this message in context: http://r.789695.n4.nabble.com/Rscript-example-tp4436130p4436130.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Rscript example
On Thu, Mar 01, 2012 at 11:53:00AM -0800, statquant2 wrote: Hi there, I am trying to find an example how to use Rscript Let's suppose I want to pass 3 arguments (I don't want [options] and -e [expressions] as described in help) *on the command line myRscript.R -arg1=value1 -arg2=value2 -arg3=value3 *In the script #! /path/to/Rscript args = commandArgs(TRUE); From what I see args is just a string, do I do things correctly ? Hi. Extending your example with print(args), i obtained ./myRscript.R -arg1=value1 -arg2=value2 -arg3=value3 [1] -arg1=value1 -arg2=value2 -arg3=value3 If you can use a fixed order of the arguments, then you need not use the -arg[i]= parts. The arguments are strings on the command line, so they are also passed to R as strings, but you can convert them inside the script. For example, with the above script, i get ./myRscript.R 23 45 78 [1] 23 45 78 However, if the script contains args - as.numeric(args) , then i get ./myRscript.R 23 45 78 [1] 23 45 78 which is a numeric vector. If you want to keep the arg[i]= structure, you can have in the script x - strsplit(args, =) print(x) and a call produces ./myRscript.R -arg1=value1 -arg2=value2 -arg3=value3 [[1]] [1] -arg1 value1 [[2]] [1] -arg2 value2 [[3]] [1] -arg3 value3 The list x may then be further analyzed. Hope this helps. Petr Savicky. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Rscript example
Hey Petr, ok I was thinking that R would handle the split by itself. I guess using eval we can even make arg1=val1 being executed by R. Ok tx Colin -- View this message in context: http://r.789695.n4.nabble.com/Rscript-example-tp4436130p4437659.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.