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

梦想与她 提交于 2019-12-02 07:29:57

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

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!

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