Search code examples
pythonwindowscommand-linerscript

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


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


Solution

  • 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 :-)