1

I have an .R file saved locally at the following path:

Rfilepath = "C:\\python\\buyback_parse_guide.r"

The command for RScript.exe is:

RScriptCmd = "C:\\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe --vanilla"

I tried running:

subprocess.call([RScriptCmd,Rfilepath],shell=True)

But it returns 1 -- and the .R script did not run successfully. What am I doing wrong? I'm new to Python so this is probably a simple syntax error... I also tried these, but they all return 1:

subprocess.call('"C:\Program Files\R\R-2.15.2\bin\Rscript.exe"',shell=True)

subprocess.call('"C:\\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe"',shell=True)

subprocess.call('C:\Program Files\R\R-2.15.2\bin\Rscript.exe',shell=True)

subprocess.call('C:\\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe',shell=True)

Thanks!

Ray
  • 3,137
  • 8
  • 32
  • 59

2 Answers2

0

The RScriptCmd needs to be just the executable, no command line arguments. So:

RScriptCmd = "\"C:\\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe\""

Then the Rfilepath can actually be all of the arguments - and renamed:

 RArguments = "--vanilla \"C:\\python\\buyback_parse_guide.r\""
cfeduke
  • 23,100
  • 10
  • 61
  • 65
  • It's still not working. I tried the above and did: subprocess.call([RScriptCmd,RArguments],shell=True) – Ray Nov 29 '12 at 02:56
  • what happens when you run that same command from the command prompt? Some programs incorrectly return a non-zero value on success (doubt that R would do this though) – cfeduke Nov 29 '12 at 02:59
  • C:\Program Files>C:\Program Files\R\R-2.15.2\bin\Rscript.exe --vanilla C:\python\ buyback_parse_guide.r 'C:\Program' is not recognized as an internal or external command, operable program or batch file. – Ray Nov 29 '12 at 03:21
  • I tried `RScriptCmd = "C:\\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe"` and `RArgs = "--vanilla C:\\python\\buyback_parse_guide.r"`. Didn't work either. Definitely a syntax error I'm not seeing – Ray Nov 29 '12 at 05:42
  • This is what works in cmd.exe: `"C:\Program Files\R\R-2.15.2\bin\Rscript.exe" --vanilla C:\python\buyback_parse_guide.r` – Ray Nov 29 '12 at 05:43
  • see my changes above - its likely the reason @hd1 states - there is a space in "Program Files" so quoting the path to the executable should account for that – cfeduke Nov 29 '12 at 16:15
0

It looks like you have a similar problem to mine. I had to reinstall RScript to a path which has no spaces.

See: Running Rscript via Python using os.system() or subprocess()

This is how I worked out the communication between Python and Rscript:

part in Python:

from subprocess import PIPE,Popen,call
p = subprocess.Popen([ path/to/RScript.exe, path/to/Script.R, Arg1], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
out = p.communicate()
outValue = out[0]

outValue contains the output-Value after executing the Script.R

part in the R-Script:

args <- commandArgs(TRUE)
argument1 <- as.character(args[1])
...
write(output, stdout())

output is the variable to send to Python

Community
  • 1
  • 1
EikeMike
  • 280
  • 3
  • 12