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!

2 Answers 2

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\""
Sign up to request clarification or add additional context in comments.

6 Comments

It's still not working. I tried the above and did: subprocess.call([RScriptCmd,RArguments],shell=True)
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)
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.
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
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
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.