[R] commandArgs: feature request

Henrik Bengtsson hb at maths.lth.se
Thu Jun 27 14:08:36 CEST 2002


Here's an updated version of commandArgs() that does what you want (only
verified on WinMe w/ R 1.5.0):

commandArgs <- function(excludeReserved=FALSE, os=NULL) {
  # Process argument 'os'
  if (is.null(os) || toupper(os) == "ANY")
    os <- c("unix", "mac", "windows")
  else if (tolower(os) == "current")
    os <- .Platform$OS.type;
  os <- tolower(os);
  if (any(is.na(match(os, c("unix", "mac", "windows")))))
    stop("Argument 'os' contains unknown values.");

  # Reserved R command line options according to
  # "An Introduction to R" for R v1.5.0:
  reservedArgs <- c();
  # a) Unix
  if ("unix" %in% os) {
    reservedArgs <- c(reservedArgs, "--help", "-h", "--version", "--save",
"--no-save", "--no-environ", "--no-site-file", "--no-init-file",
"--restore", "--no-restore", "--no-restore-data", "--no-restore-history",
"--vanilla", "--no-readline", "--min-vsize=.*", "--min-nsize=.*",
"--max-nsize=.*", "--quiet", "--silent", "-q", "--slave", "--verbose",
"--debugger=.*", "--gui=.*");
  # b) Macintosh
  } else if ("mac" %in% os) {
    reservedArgs <- c(reservedArgs, "--version", "--save", "--no-save",
"--no-init-file", "--restore", "--no-restore", "--no-restore-data",
"--no-restore-history", "--vanilla", "--min-vsize=.*", "--max-vsize=.*",
"--min-nsize=.*", "--max-nsize=.*", "--quiet", "--silent", "-q", "--slave",
"--verbose");
  # b) Windows
  } else if ("windows" %in% os) {
    reservedArgs <- c(reservedArgs, "--version", "--mdi", "--sdi",
"--no-mdi", "--save", "--no-save", "--no-site-file", "--no-init-file",
"--no-environ", "--restore", "--no-restore", "--no-restore-data",
"--no-restore-history", "--vanilla", "--min-vsize=.*", "--max-vsize=.*",
"--min-nsize=.*", "--max-nsize=.*", "--max-mem-size=.*", "--quiet",
"--silent", "-q", "--slave", "--verbose", "--ess");
  }

  # Flag reserved arguments
  args <- .Internal(commandArgs());
  reservedArgs <- paste("^", unique(reservedArgs), "$", sep="");
  isReserved <- logical(length(args));
  for (rarg in reservedArgs)
    isReserved <- isReserved | (regexpr(rarg, args) != -1);
  attr(args, "isReserved") <- isReserved;

  if (excludeReserved == TRUE)
    args <- args[!attr(args, "isReserved")];

  args;
} # commandArgs()

Feel free to use it anywhere!

Henrik Bengtsson

Dept. of Mathematical Statistics @ Centre for Mathematical Sciences
Lund Institute of Technology/Lund University, Sweden (+2h UTC)
Office: P316, +46 46 222 9611 (phone), +46 46 222 4623 (fax)
h b @ m a t h s . l t h . s e, http://www.maths.lth.se/bioinformatics/


> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch
> [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of Vadim Ogranovich
> Sent: Tuesday, June 25, 2002 2:37 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] commandArgs: feature request
>
>
> Dear R-core Team,
>
> As Thomas Lumley pointed out in one of his e-mails one can use
> commandArgs()
> to get a copy of the command line arguments supplied when R session was
> invoked and then use grep to extract parameters of interest.
> His solution works very well if the custom options are passed by
> names, e.g.
> --my-option=value, but what if one wants to pass parameters by their
> positions. Then it's not easy to tell standard options from user-supplied
> ones.
> One possible solution would be to augment the return value of
> commandArgs()
> with BOOL vector attribute that says whether it is a standard (reserved) R
> option or not. Here is a usage example that I have in mind:
>
> R BATCH --no-save 2001-01-01 3001-01-01 Foo/*.in batch.R
>
> batch.R:
>
> myArgs <- commandArgs()
> myArgs <- myArgs[!attr(myArgs, "is.standard")]
>
> start <- myArgs[1]
> end <- myArgs[2]
> files <- myArgs[-seq(2)]
>
> # code
> ...
>
> I am sure there are other (better) solutions, but having anything
> like this
> would make it easier to integrate R  with shell scripts that invoke R as a
> sub-task.
>
> Thanks, Vadim
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-
> r-help mailing list -- Read
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list