2016-03-02 38 views
1

我有一个R脚本,它自己能正常工作,但我需要它是Python脚本的一部分。所以,当我运行python脚本时,R脚本会自动执行。 我使用下面的命令;没有错误,但是不会创建R脚本输出文件。使用Python通过子程序执行R脚本

import subprocess 

retcode = subprocess.call("C:/Program Files/R/R-3.2.2/bin/Rscript --vanilla T:/2012.R", shell=True) 

非常感谢你提前。

+0

我尝试了它,但仍然没有结果,没有错误信息。 – user460213

+2

简单测试程序会发生什么情况:'print.hw < - function(){print(“hello world”) }; print.hw()' –

+0

Rscript解释器的路径是否存在语法错误?我的直觉是你需要逃离路径中的任何空间角色。 C:/ Program Files/R/R-3.2.2/bin/Rscript – IcarianComplex

回答

1

只需将您的字符串命令,在支架和字符串分解成独立的组件的功能的第一个参数预计参数列表,每doc

subprocess.call(参数,*,标准输入=无,由于输出=无,标准错误=无, 壳=假)

import subprocess 

retcode = subprocess.call(['C:/Program Files/R/R-3.2.2/bin/Rscript', '--vanilla', 
          'T:/2012.R'], shell=True) 

或者,把它分解成多个字符串:

command = 'C:/Program Files/R/R-3.2.2/bin/Rscript' 
arg = '--vanilla' 
path2script = 'T:/2012.R' 

retcode = subprocess.call([command, arg, path2script], shell=True)