[R] getopt does not work as expected!

Trevor Davis trevor.l.davis at gmail.com
Sat Mar 31 02:39:55 CEST 2012


> Why is commandArgs()[1] = "/usr/lib64/R/bin/exec/R" and not
> "getopt_test.R" as described by the help (?getopt)?

That used to work but R changed the behavior of the commandArgs()
function a couple of years ago, I guess the orginal author of getopt
didn't update his example then.

This will do the trick:

args <- commandArgs()
file_index <- grepl("--file=", args)
self <- gsub("--file=", "", args[file_index])

> This behaviour I don't like at all. Why getopt does not distinguish
> beetween options starting with alt least one hyphen and normal arguments
> starting without hyphen? How it is possible to handle such a situation?
> Have I myself to split the arguments into two lists, one with hyphen and
> one without hyphen, and then pass only this list with hyphen to getopt?
> Or is there a better solution?

The author of getopt started but did not finish adding positional
arguments to ``getopt``.  Howevor, positional arguments have been
supported in the ``optparse`` package for over a year if you specify
``positional_args=TRUE`` to the ``parse_args`` function.

- Trevor Davis



More information about the R-help mailing list