在Python中使用Rscript.exe什

2024-09-28 22:39:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在本地保存了一个.R文件,路径如下:

Rfilepath = "C:\\python\\buyback_parse_guide.r"

命令RScript.exe是:

^{pr2}$

我试着跑步:

subprocess.call([RScriptCmd,Rfilepath],shell=True)

但是它返回1,.R脚本没有成功运行。我做错什么了?我是Python新手,所以这可能是一个简单的语法错误。。。我也试过这些,但都返回1:

subprocess.call('"C:\Program Files\R\R-2.15.2\bin\Rscript.exe"',shell=True)

subprocess.call('"C:\\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe"',shell=True)

subprocess.call('C:\Program Files\R\R-2.15.2\bin\Rscript.exe',shell=True)

subprocess.call('C:\\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe',shell=True)

谢谢!在


Tags: 文件路径truebinparsefilesshellcall
2条回答

看来你和我也有类似的问题。我不得不将RScript重新安装到一个没有空格的路径上。在

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

我是这样计算Python和Rscript之间的通信的:

Python部分:

from subprocess import PIPE,Popen,call
p = subprocess.Popen([ path/to/RScript.exe, path/to/Script.R, Arg1], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
out = p.communicate()
outValue = out[0]

outValue包含执行脚本后的输出值。R

R脚本中的一部分:

^{pr2}$

output是要发送给Python的变量

RScriptCmd需要只是可执行文件,没有命令行参数。所以:

RScriptCmd = "\"C:\\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe\""

那么Rfilepath实际上可以是所有参数-并重命名为:

^{pr2}$

相关问题 更多 >