J R*_*J R 10 python subprocess r
我需要打开一个R脚本并为其提供由单独的python脚本制定的输入.该subprocess模块似乎是一个很好的方法来做到这一点.
我遇到了一些令人费解的结果,即我可以写一次,只能写一次p.stdin.这是我到目前为止:
from subprocess import Popen, PIPE, STDOUT
p = Popen(['r --no-save'],stdin=PIPE,stdout=PIPE,stderr=PIPE,shell=True)
p.stdin.write("source('myrscript.R')\n")
p.stdin.write('myfirstinput')
Run Code Online (Sandbox Code Playgroud)
运行此代码时会发生的情况是第一个stdin.write()按预期执行的实例(并打开我的R脚本),但第二行什么都不做,并且子进程(实际上是R脚本)退出时出现错误,表明子进程在预期输入并因此终止的情况下没有收到输入.
注意 - 在一个完美的世界中,我只是直接通过R进行交互,但是这个特殊的脚本需要复杂的输入,不能直接输入用于实际目的.此外,rpy/rpy2不是一个选项,因为此脚本的最终用户不一定有权访问该模块或其依赖项.rscript也不是一种选择(出于很多原因,但主要是因为最终用户R配置的可变性).
最后,p.communicate不是一个选项,因为显然会在写完后关闭这个过程,我需要保持它开放.
提前致谢
您需要致电.communicate():
from subprocess import Popen, PIPE, STDOUT
p = Popen(
['r', '--nosave'],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE)
p.stdin.write("source('myrscript.R')\n")
p.stdin.write('myfirstinput\n')
p.stdin.write('q\n')
stdout, stderr = p.communicate()
print '---STDOUT---'
print stdout
print '---STDERR---'
print stderr
print '---'
Run Code Online (Sandbox Code Playgroud)
shell=True并且似乎可以使用我的伪R脚本,因为我的系统中没有安装R。您可能需要,也可能不需要。r --nosave也可以使用;只是不要同时做它们两者。stdin.write()不写换行符\n,您必须自己提供。我的第一次尝试超出了预期,我希望第二次尝试更加接近。正如JF Sebastian所建议的那样,您可能要使用pexpect:
import pexpect
import sys
if __name__ == '__main__':
prompt = '> ' # Don't know what the R prompt looks like
lines = ['one', 'two', 'three']
r = pexpect.spawn('r --no-save', logfile=sys.stdout)
for line in lines:
r.expect(prompt)
r.sendline(line)
# If you want to interact with your script, use these two lines
# Otherwise, comment them out
r.logfile = None # Turn off logging to sys.stdout
r.interact()
Run Code Online (Sandbox Code Playgroud)
pexpect。我做到了pip install pexpectspawn()返回一个spawn对象,看文档在这里。| 归档时间: |
|
| 查看次数: |
4328 次 |
| 最近记录: |