[R] [Rscript] difficulty passing named arguments from commandline

Tom Roche Tom_Roche at pobox.com
Sun Sep 9 19:21:24 CEST 2012


https://github.com/TomRoche/GEIA_to_netCDF/commit/62ad6325d339c61ac4e7de5e7d4d26fa21ed918c
>> # - Rscript ./netCDF.stats.to.stdout.r netcdf.fp="./GEIA_N2O_oceanic.nc" var.name="emi_n2o"
>> # fails

>> # + Rscript ./netCDF.stats.to.stdout.r 'netcdf.fp="./GEIA_N2O_oceanic.nc"' 'var.name="emi_n2o"'
>> # succeeds

https://stat.ethz.ch/pipermail/r-help/2012-September/323287.html
> The trailling arguments to Rscript, generally read by
> commandArgs(TRUE), come into R as a vector of character strings.
> Your script can interpret those character strings in many ways.
> The [script linked above] processed them all with

>    eval(parse(text=arg[i]))

> so all the arguments had to be valid R expressions: strings must be
> quoted, unquoted things are treated as names of R objects, slash
> means division, "=" and "<-" mean assignment, etc.

That explains the need for strict quoting--thanks.

> If that is a problem, don't use parse() to interpret the strings;
> use sub() or strsplit() to extract substrings and do what you want
> with them. (This is somewhat safer than using eval(parse(text=))
> because it can do less.)

Assigning arguments via strsplit() does seem to be more of a PITA, but it works now @

https://github.com/TomRoche/GEIA_to_netCDF/blob/master/netCDF.stats.to.stdout.r

your assistance is appreciated, Tom Roche <Tom_Roche at pobox.com>




More information about the R-help mailing list