How to prevent command line args from being interpreted by R vs. only by my script?

♀尐吖头ヾ 提交于 2019-12-05 05:24:15

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 of Rscript. For example, using #!/usr/bin/Rscript in foo.R, I get the issue:

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

goodies = donuts

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

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

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:

On OS X, you may want to build it via configure --program-prefix="l" to renamed it to lr as that particular OS thinks R and r are the same

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

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

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

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

will work but give the spurious warning, and

Rscript -e "commandArgs()" --gui

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

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!