Skip to content

Rscript argument processing minor bug with -g

2 messages · Dan Davison, Dirk Eddelbuettel

#
I think there's a minor bug in the argument-processing carried out by Rscript.
The effect is that if one passes "-g" as a flag to the script, it is erroneously 
exposed to the main executable's argument processing and therefore generates a 
message about not being able to comply with the request for a particular GUI. 
Uppercase G is fine as are the other 25 letters in upper or lower case.

I noticed this with R-2.5.1 and carried out the tests below with R-devel-2.7.0.

Dan

platform       i686-pc-linux-gnu                                               
arch           i686                                                            
os             linux-gnu                                                       
system         i686, linux-gnu                                                 
status         Under development (unstable)                                    
major          2                                                               
minor          7.0                                                             
year           2008                                                            
month          01                                                              
day            04                                                              
svn rev        43862                                                           
language       R                                                               
version.string R version 2.7.0 Under development (unstable) (2008-01-04 r43862)

[fedora core 5]

## No problem with -a -b -f -h

~/src/scripts/R> /usr/src/R/R-devel/bin/Rscript -e "commandArgs()" -a -b -f -h
 [1] "/usr/src/R/R-devel/bin/exec/R" "--slave"                      
 [3] "--no-restore"                  "-e"                           
 [5] "commandArgs()"                 "--args"                       
 [7] "-a"                            "-b"                           
 [9] "-f"                            "-h"   

## But if -g is in there, there's a warning message

~/src/scripts/R> /usr/src/R/R-devel/bin/Rscript -e "commandArgs()" -a -b -f -g -h
WARNING: unknown gui '-h', using X11

 [1] "/usr/src/R/R-devel/bin/exec/R" "--slave"                      
 [3] "--no-restore"                  "-e"                           
 [5] "commandArgs()"                 "--args"                       
 [7] "-a"                            "-b"                           
 [9] "-f"                            "-g"                           
[11] "-h"                           

~/src/scripts/R> /usr/src/R/R-devel/bin/Rscript -e "commandArgs()" -a -b -f -g not_a_gui -h
WARNING: unknown gui 'not_a_gui', using X11

 [1] "/usr/src/R/R-devel/bin/exec/R" "--slave"                      
 [3] "--no-restore"                  "-e"                           
 [5] "commandArgs()"                 "--args"                       
 [7] "-a"                            "-b"                           
 [9] "-f"                            "-g"                           
[11] "not_a_gui"                     "-h"      

## The problem remains if the R code is in a file rather than in-line,
## and also with a #! style script:

~/src/scripts/R> cat test.R 
#!/usr/src/R/R-devel/bin/Rscript
commandArgs()

~/src/scripts/R> ./test.R -a -g invalid_gui
WARNING: unknown gui 'invalid_gui', using X11

[1] "/usr/src/R/R-devel/bin/exec/R" "--slave"                      
[3] "--no-restore"                  "--file=./test.R"              
[5] "--args"                        "-a"                           
[7] "-g"                            "invalid_gui"        

~/src/scripts/R> cat test2.R 
commandArgs()
~/src/scripts/R> /usr/src/R/R-devel/bin/Rscript test2.R -a -g
WARNING: --gui or -g without value ignored
[1] "/usr/src/R/R-devel/bin/exec/R" "--slave"                      
[3] "--no-restore"                  "--file=test2.R"               
[5] "--args"                        "-a"                           
[7] "-g"
9 days later
#
On 5 January 2008 at 19:34, Dan Davison wrote:
| I think there's a minor bug in the argument-processing carried out by Rscript.
| The effect is that if one passes "-g" as a flag to the script, it is erroneously 
| exposed to the main executable's argument processing and therefore generates a 
| message about not being able to comply with the request for a particular GUI. 
| Uppercase G is fine as are the other 25 letters in upper or lower case.
| 
| I noticed this with R-2.5.1 and carried out the tests below with R-devel-2.7.0.
[...]
| ~/src/scripts/R> /usr/src/R/R-devel/bin/Rscript -e "commandArgs()" -a -b -f -g -h
| WARNING: unknown gui '-h', using X11
| 
|  [1] "/usr/src/R/R-devel/bin/exec/R" "--slave"                      
|  [3] "--no-restore"                  "-e"                           
|  [5] "commandArgs()"                 "--args"                       
|  [7] "-a"                            "-b"                           
|  [9] "-f"                            "-g"                           
| [11] "-h"                           

For what it's worth, littler does not have that problem with   Following GNU
traditions, we stick all arguments that are not referring to existing
switches into the argv vector which you can access normally:

	edd at ron:~> r -e 'print(argv)' -- -a -b -f -g -h -i -j
	[1] "-a" "-b" "-f" "-g" "-h" "-i" "-j"

It is then up to you to parse these remaining arguments. If you hit an
existing option like -h _before_ the --, its code gets invoked.  This also
works when code is piped into littler (when '-' is used to turn on stdin
parsing):

	edd at ron:~> echo 'print(argv)' | r - -- -a -b -f -g -h -i -j
	[1] "-a" "-b" "-f" "-g" "-h" "-i" "-j"

Prior versions had an off-by-one error in the way argv was built, but version
0.1.0 which I just put onto

	http://dirk.eddelbuettel.com/code/littler.html

should be fine.

Hope this helps,  Dirk