I'm using the docopt implementation for R. My script has a command line option where the short form is -g
. When I run my script, it seems this argument is first interpreted by R and then by my script. Therefore I get a wrist slap about not specifying a value for the GUI. Can I prevent R from trying to work with these command line args?
Example of a script:
#!/usr/bin/Rscript
suppressPackageStartupMessages(library(docopt))
"docopt practice script
Usage: foo.R [-g <goodies>]
Options:
-g <goodies>, --goodies=<goodies> Goodies
" -> doc
opts <- docopt(doc)
cat(sprintf("goodies = %s\n", opts$goodies))
Here's what happens when I run it:
Jennifers-MacBook-Pro-3:scripts jenny$ ./foo.R -g donuts
WARNING: --gui or -g without value ignored
goodies = donuts
If you change the short form of the option from -g
to -j
, the WARNING
goes away … but I have a good reason for using the letter g
!
As pointed out by @krlmlr, this issue has to do with
Rscript
(in your hash bang). One workaround would be to use the functionality provided by the excellent littler in place ofRscript
. For example, using#!/usr/bin/Rscript
infoo.R
, I get the issue:Replacing this with
#!/usr/local/bin/r
in a new scriptfoo2.R
, I get a clean output:It looks like you're on an OS X machine, so if you do choose to install
littler
, just be sure to note the authors' warning:The
R
andRscript
commands know--args
. Compare the output of the following:This works due to an early exit if
--args
is detected. However, the--gui
warning is triggered in a separate loop before this.This means that
will work but give the spurious warning, and
gives an error right away. It looks like only
--gui
and-g
are affected.As a quick-and-dirty hack, one could insert something like
at the beginning of the GUI-check loop. Until this is changed in R, I suspect there's no choice but to avoid the
-g
switch or live with the (otherwise harmless) warning.