[R] Help using system() command to execute Perl script through MSDOS

Marc Schwartz marc_schwartz at me.com
Thu Mar 7 13:20:13 CET 2013


On Mar 6, 2013, at 8:52 PM, pdbarry <pdbarry at alaska.edu> wrote:

> I am working on creating a program for some simulations I need to do and I
> want to execute a Perl script that I wrote  using the system() command in R.
> I have spent a couple days trying to figure this out and it appears that my
> problem occurs when sending the perl script file path through R to MSDOS. I
> have tried using double backslashes, quotations, etc. Moving my files to the
> root directory with no folders with spaces seems to make the problem go
> away. 
> 
> So here is the code:
> system(paste("perl",FP.chmod,FP.baseline.in,FP.baseline.out))
> 
> where perl is the command to use the perl script
> FP.chmod is the extenstion to the perl file
>       "C:/Documents and Settings/Pato/Desktop/Mikes R package/chmod700.pl"
>        created using FP.chmod<- file.path(wd,'chmod700.pl') #wd is the
> working directory
> FP.baseline.in is the input file extension
>        "C:/Documents and Settings/Pato/Desktop/Mikes R
> package/Baseline2c.bse"
> FP.baseline.out is the output to make from the perl script.
> 
> If I look at it pasted together it looks like it would work: 
> "perl C:/Documents and Settings/Pato/Desktop/Mikes R package/chmod700.pl
> C:/Documents and Settings/Pato/Desktop/Mikes R package/Baseline2c.bse
> C:/Documents and Settings/Pato/Desktop/Mikes R package/Baseline2d.bse
> 
> If I change the location of the files so that they are directly under the
> root directory it runs without a hitch. I greatly appreciate any help that
> can be offered. 
> Pat



You might want to take a look to see how I do it in WriteXLS():

  https://github.com/marcschwartz/WriteXLS/blob/master/R/WriteXLS.R

Look at around line 167 in the R source.

The key thing to understand (as you hint at above) is that spaces in the arguments on the CLI are delimiters, separating what you have as a single argument into multiples.

Use ?shQuote to put single quotes around each argument that contains spaces, so that it is treated as a single argument:

> shQuote("This is a single argument")
[1] "'This is a single argument'"


Regards,

Marc Schwartz



More information about the R-help mailing list