[R] R 2.11, shell, spaces

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Wed Sep 8 01:59:52 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Oliver Soong
> Sent: Tuesday, September 07, 2010 2:01 PM
> To: R-lists
> Subject: [R] R 2.11, shell, spaces
> 
> Can somebody confirm some unexpected behavior?  This is under Windows,
> with R 2.11.0 and 2.11.1.
> 
> 1. Create a trivial test file (I called it test.R) containing:
> cat("Success.\n")
> 2. Load R (Gui or Term) and run:
> shell("\"C:\\path\\to\\Rscript.exe\" \"C:\\path\\to\\test.R\"")
> 
> In my case, I get various error messages.  If the
> path\\to\\Rscript.exe has spaces or not, it complains that the first
> token (e.g., 'C:\Program') is not a valid command.  If the
> path\\to\\Rscript.exe has no spaces, it complains that the specified
> path is invalid.  If C:\\path\\to\\test.R has no spaces, and I remove
> the surrounding quotes, things work as expected.
> 
> I think this might be related to some earlier e-mail traffic that I
> didn't see a satisfactory resolution to.
> 
> Cheers,
> Oliver
> 

The shell command is a "user friendly" wrapper for the system() command, that can sometimes introduce its own difficulties (for example your quoting problems).  In addition, where you may need to include double quote marks for Windows purposes, I would use single quotes for the outer quotes in the R command.  Try something like the following:

system('"C:\\path\\to\\Rscript.exe" "C:\\path\\to\\test.R"') 

This works for me on R-2.11.1 running under Windows 7 (should work for other recent versions of R and Windows XP/Vista).  I also prefer to use the forward slash for paths in R since it works in windows, reduces the typing (no need to escape them), and is compatible with Unix/Linux conventions.  So, the following should work as well

system('"C:/path/to/Rscript.exe" "C:/path/to/test.R"')


Hope this is helpful, 

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204




More information about the R-help mailing list