Check out the getopt package.
On Thu, Apr 30, 2009 at 10:10 PM, JiHO <jo.lists at gmail.com>
wrote:> Hello everyone,
>
> I am writing an R script which will be provided with command line arguments
> by a shell script. I read them with commandArgs() but I have an issue to
> make that fool proof.
>
> For example, with test.R containing:
>
> ? ? ? ?args=commandArgs(trailingOnly=TRUE)
> ? ? ? ?args
>
> I get:
>
> ? ? ? ?$ Rscript test_commandLineArgs.R 3 4 5
> ? ? ? ?[1] "3" "4" "5"
>
> Now, to make the input more flexible and robust, I want to pass *named*
> arguments, so that the R script does not depend on the order of arguments
> passed and can check which are present/absent etc. I.e.
>
> ? ? ? ?$ Rscript test_commandLineArgs.R foo=3 bar=4 5
> ? ? ? ?[1] "foo=3" "bar=4"
>
> But I am stuck on how to actually execute the code within those strings so
> as to get two variables foo and bar equal to 3 and 4 respectively. I looked
> at eval, deparse, substitute and all that but did not find anything. Is
that
> possible?
>
> Otherwise I will resort to parsing the arguments with strsplit but I would
> much prefer and more elegant solution.
>
> Thank you very much in advance. Sincerely,
>
> JiHO
> ---
> http://jo.irisson.free.fr/
>
> ______________________________________________
> R-help at 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.
>