本文介绍了使用os.system()或subprocess()通过Python运行Rscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用os.system()或subprocess()通过Python运行Rscript时遇到问题。

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

使用os.system()通过python工作通常对我很好(例如用gdalwarp.exe),但不是与Rscript.exe。

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.

避免路径中的空格问题在CMD窗口中通过将路径放在引号中容易克服。
执行以下命令是successfull。

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"

坚持用Python。
到目前为止,我尝试使用python:

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'])

有人看到我做错了吗?
提前感谢,
Eike

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

推荐答案

在得到这样一个简单问题的心灵。我决定将RStatistics重新安装到没有空格或分数的路径,例如:
C:/R/bin/Rscript.exe

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.

现在
subprocess.call([C:/R/bin/Rscript.exe,D:/ otsu_Script。 R])
os.system(C:/R/bin/Rscript.exe D:/otsu_Script.R)正在工作很好。
两天前应该试过...

Nowsubprocess.call(["C:/R/bin/Rscript.exe", "D:/otsu_Script.R"] ) oros.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 :-)

这篇关于使用os.system()或subprocess()通过Python运行Rscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 04:58