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 OP's script 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.
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.) E.g., % R --quiet --args ./file.txt logFile=./file.log var3=1/3 'stringVar <- paste("var3 is", var3)' > # note last argument had to be quoted because otherwise the shell would > # interpret the "<" as redirected input, spaces as breaks between arguments, > etc. > args <- commandArgs(TRUE) > args [1] "./file.txt" [2] "logFile=./file.log" [3] "var3=1/3" [4] "stringVar <- paste(\"var3 =\", var3)" > dataFile <- args[1] > assign(sub("=.*$", "", args[2]), sub("^[^=]*=", "", args[2])) > assign(sub("=.*$", "", args[3]), eval(parse(text=sub("^[^=]*=", "", > args[3])))) > eval(parse(text=args[4])) > objects() [1] "args" "dataFile" "logFile" "stringVar" "var3" > dataFile [1] "./file.txt" > logFile [1] "./file.log" > stringVar [1] "var3 is 0.333333333333333" > var3 [1] 0.3333333 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Ben Tupper > Sent: Thursday, September 06, 2012 7:39 PM > To: R Help > Cc: Tom Roche > Subject: Re: [R] [Rscript] difficulty passing named arguments from commandline > > Hi Tom, > > Using the script below I had no trouble. Here's an example > > Minke:R ben$ Rscript --no-init-file ./argCatcher.rs ./the=trouble > .with=./args is=the.y > argue > ./the=trouble > .with=./args > is=the.y > argue > > > # script starts here > # argumentCatcher.rs > args = commandArgs(trailing = TRUE) > cat(args, sep = "\n") > q(runLast = FALSE, status = 0, save = "no") > # script ends here > > What does your R --version report? > > Minke:R ben$ R --version > R version 2.15.0 (2012-03-30) > Copyright (C) 2012 The R Foundation for Statistical Computing > ISBN 3-900051-07-0 > Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) > > > Cheers, > Ben > > > > > > > On Sep 6, 2012, at 7:27 PM, Tom Roche wrote: > > > > > Wanting a commandline solution (for a problem detailed @ > > > > > http://mailman.unidata.ucar.edu/mailing_lists/archives/netcdfgroup/2012/msg00279.ht > ml > > > > ) I turned to Rscript, and whacked out the q'n'd > > > > https://github.com/TomRoche/GEIA_to_netCDF/blob/master/netCDF.stats.to.stdout.r > > > > However it wasn't as quick as hoped, because I spent quite a bit of time > > figuring out how to pass the arguments. This works (note the quoting): > > > > $ Rscript ./netCDF.stats.to.stdout.r 'netcdf.fp="./GEIA_N2O_oceanic.nc"' > 'var.name="emi_n2o"' > >> For ./GEIA_N2O_oceanic.nc var=emi_n2o > >> cells=64800 > >> obs=36143 > >> min=5.96e-08 > >> max=1.17e+03 > >> mean=99.5 > >> med=67.7 > > > > but this fails > > > > roche@amad1:/project/inf14w/roche/GEIA_to_netCDF $ Rscript > ./netCDF.stats.to.stdout.r 'netcdf.fp=./GEIA_N2O_oceanic.nc' > 'var.name=emi_n2o' > >> Error in eval(expr, envir, enclos) : object '.' not found > >> Calls: eval -> eval > >> Execution halted > > > > and this fails > > > > roche@amad1:/project/inf14w/roche/GEIA_to_netCDF $ Rscript > ./netCDF.stats.to.stdout.r netcdf.fp="GEIA_N2O_oceanic.nc" var.name="emi_n2o" > >> Error in eval(expr, envir, enclos) : > >> object 'GEIA_N2O_oceanic.nc' not found > >> Calls: eval -> eval > >> Execution halted > > > > and this fails > > > > roche@amad1:/project/inf14w/roche/GEIA_to_netCDF $ Rscript > ./netCDF.stats.to.stdout.r netcdf.fp=./GEIA_N2O_oceanic.nc var.name=emi_n2o > >> Error in eval(expr, envir, enclos) : object '.' not found > >> Calls: eval -> eval > >> Execution halted > > > > Must the quoting be so strict/brittle, or am I missing something? > > > > Also, It Would Be Nice if there was more in the `help(Rscript)` examples > > about argument passing. I for one found the current examples quite terse > > and unhelpful. > > > > TIA, Tom Roche <tom_ro...@pobox.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. > > Ben Tupper > Bigelow Laboratory for Ocean Sciences > 180 McKown Point Rd. P.O. Box 475 > West Boothbay Harbor, Maine 04575-0475 > http://www.bigelow.org > > ______________________________________________ > 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-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.