Ted Byers
2008-Oct-15 19:48 UTC
[R] Can R scripts executed in batch mode take a commandline argument?
I have examined the documentation for batch mode use of R: R CMD BATCH [options] infile [outfile] The documentation for this seems rather spartan. Running "R CMD BATCH --help" gives me info on only two options: one for getting help and the other to get the version. I see, further on, that there are options for retoring and saving sessions (which I do not need to do in this case), but are there other options defined? If so, what are they and how are they to be used? However, it goes on to say: "Further arguments starting with a '-' are considered as options as long as '--' was not encountered, and are passed on to the R process, which by default is started with '--restore --save'." I see here it says further arguments starting with a '-' are passed to the R process, but usage is not clear. For example, if I write a script that should take as a commandline argument the name of a file that contains a series of numbers that I want to place into a vector which, in turn I want to pass to fitdistr(x,"exponential"), what do I do to get that file name from the commandline and pass it to, say, "read.csv"? BTW: How would I tell it that there is no need to restore and save? If I can't pass a commandline argument, do I have to write the arguments in afile, and have that file read each time I need to run the script? Thanks Ted -- View this message in context: http://www.nabble.com/Can-R-scripts-executed-in-batch-mode-take-a-commandline-argument--tp20000914p20000914.html Sent from the R help mailing list archive at Nabble.com.
cls59
2008-Oct-16 01:43 UTC
[R] Can R scripts executed in batch mode take a commandline argument?
On Unix/Linux platforms, you can use the included Rscript utility by adding the following shebang at the top of your program. Command line arguments can then be retrieved using the commandArgs function: #!/usr/bin/Rscript args <- commandArgs(trailingOnly = TRUE) args is now a character vector containing every space delimited text string that followed your program name when you ran it. trailingOnly is set to TRUE because the RScript interpreter prepends a few arguments of it's own that you will probably have no use for. As I stated earlier, this approach works great on Unix/Linux systems, I havn't tested it in a Windows environment. Good Luck! -Charlie ----- Charlie Sharpsteen Undergraduate Environmental Resources Engineering Humboldt State University -- View this message in context: http://www.nabble.com/Can-R-scripts-executed-in-batch-mode-take-a-commandline-argument--tp20000914p20005573.html Sent from the R help mailing list archive at Nabble.com.