[R] Passing strings with spaces to Python using system2 splits string at whitespace

Marc Schwartz marc_schwartz at me.com
Thu Jun 4 16:10:17 CEST 2015


On Jun 4, 2015, at 8:47 AM, Wall, Wade A ERDC-RDE-CERL-IL <Wade.A.Wall at usace.army.mil> wrote:
> 
> Hi all,
> 
> I am trying to pass arguments to a python script using R, but am running into a problem with the string being split on the white spaces. Investigation on the python end suggests that it is happening upstream from python, because other shells such as bash have generated similar errors.
> 
> Here is example code.
> 
> R script:
> 
> test = "./Example.py"
> string1 = "ThisWorks"
> string2 = "This doesn't"
> 
> system2('python',args = c(as.character(test),as.character(string1))) ## This works
> system2('python',args = c(as.character(test),as.character(string2))) ## This doesn't
> 
> Python script:
> 
> from sys import argv
> script, string = argv
> print script
> print string
> 
> What happens is that string 2 is splits into "This" and "doesn't". Does anyone know how to resolve this issue? Of course I can remove the white spaces, but that may be somewhat inconvenient.
> 
> Thanks for any help.
> 
> 
> Wade


See ?shQuote

> shQuote(string2)
[1] “\"This doesn’t\""


Regards,

Marc Schwartz



More information about the R-help mailing list