How to prevent command line arguments from interpreting R compared to my script?

I use docopt for R. My script has a command line option where the short form is -g . When I run my script, it seems that this argument is first interpreted by R, and then by my script. Therefore, I get a slap in the wrist to not indicate a value for the GUI. Can I prevent R from trying to work with these command line arguments?

Sample 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 is 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 parameter from -g to -j , WARNING will go away ... but I have a good reason for using the letter g !

+7
r docopt
source share
2 answers

As pointed out by @krlmlr, this problem is related to Rscript (in your hashing). One way would be to use littler functions instead of Rscript . For example, using #!/usr/bin/Rscript in foo.R , I get a problem:

 [ nathan@nrussell R]$ ./foo.R -g donuts WARNING: unknown gui 'donuts', using X11 goodies = donuts 

Replacing this with #!/usr/local/bin/r in the new script foo2.R , I get clean output:

 [ nathan@nrussell R]$ ./foo2.R -g donuts goodies = donuts 

It looks like you are on an OS X machine, so if you decide to install littler , just pay attention to the authors warning:

In OS X, you can create it via configure --program-prefix = "l" rename it to lr, since this particular OS thinks that R and r are the same

+3
source share

The R and Rscript know --args . Compare the output:

 R -e "TRUE" --args --silent R -e "TRUE" --silent 

This works because of an early exit if --args detected. However, the --gui warning runs in a separate loop before that .

It means that

 Rscript -e "commandArgs()" --args --gui 

will work, but give a false warning, and

 Rscript -e "commandArgs()" --gui 

gives an error right away. It only looks like --gui and -g .

Like a quick and dirty hack, you can insert something like

 if(!strcmp(*avv, "--args")) { break; } 

at the beginning of the GUI validation loop . Until this changes in R, I suspect there is no choice but to avoid the -g switch or live with a warning (otherwise harmless).

+3
source share

All Articles