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

坚强是说给别人听的谎言 提交于 2019-12-02 20:11:54

问题


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


回答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 :-)




回答2:


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!



来源:https://stackoverflow.com/questions/28514769/running-rscript-via-python-using-os-system-or-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!