0

I am facing problems running a Rscript via Python using os.system() or subprocess().

Using os.system() to run commands via python works generally fine for me (e.g. with gdalwarp.exe) but not with Rscript.exe.

The only difference I can see are spaces in the path.

Avoiding problems with spaces in the path are easy overcome in the CMD-window by putting the paths in quotation marks. Executing the following command is successfull.

"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"

But I am stuck with Python. What I tried so far with python:

os.system("C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r'"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"')
subprocess.call([r'C:/Program Files/R/R-3.0.2/bin/Rscript.exe', r'D:/.../otsu_Script.R'])

Does anybody see what I am doing wrong? Thanks in advance, Eike

3
  • the correct way to call external programms, is to use subprocess. So what's wrong with that solution? Commented Feb 14, 2015 at 10:58
  • The subprocess command should work. system uses cmd /c commandline, so wrap the entire commandline in quotes, e.g. os.system('""C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R""'). Forward slashes work here because the argument is quoted (i.e. forward slashes won't be interpreted as switches), so there's no need for a r"raw string". Commented Feb 14, 2015 at 12:47
  • sorry, neighter the os.system('""C:/....script.R""') nor the subprocess.call([r'C:/....script.R') works. Commented Feb 14, 2015 at 19:26

2 Answers 2

1

After getting mental on such a simple problem. I decided to reinstall RStatistics to a path with no spaces or points, like: C:/R/bin/Rscript.exe.

Now subprocess.call(["C:/R/bin/Rscript.exe", "D:/otsu_Script.R"] ) or os.system("C:/R/bin/Rscript.exe D:/otsu_Script.R") are working just fine. Should have tried it two days ago...

... but now I am a happy monkey anyway :-)

Sign up to request clarification or add additional context in comments.

Comments

1

It probably is way too late now and I have seen you solved the issue, but I was having a similar issue (although in a Linux system) and it might help someone else now; this command was not working when called inside python although it worked directly on the terminal/command-line.

os.system("R CMD BATCH ./read_lengths_batch.R")

I tried many solutions, including subprocess and others but found it to be easier than that. In my case, and I understand it might be different in Windows, I just had to add a & at the end of the call for it to run in the background. Somehow it seemed R would shut down with the Python script instead of doing its work.

os.system("R CMD BATCH ./read_lengths_batch.R &")

Strangely, it was also working if in my folder I would have the same file copied with a .txt extension: read_lengths_batch.R and read_lengths_batch.txt.

Hope it helps someone!

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.