从python执行R脚本

Arn*_*old 10 python r

我有一个R脚本,可以制作几个图.我希望能够从python执行此脚本.

我第一次尝试:

import subprocess
subprocess.call("/.../plottingfile.R", shell=True)
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

/bin/sh: /.../plottingfile.R: Permission denied
126
Run Code Online (Sandbox Code Playgroud)

我不知道126号的含义是什么.我的所有文件都在桌面上,因此我认为不需要任何特殊权限?我认为这个错误可能与cwd = none有关但我改变了这个并且我仍然有错误.

接下来我尝试了以下内容:

subprocess.Popen(["R --vanilla --args </.../plottingfile.R>"], shell = True)
Run Code Online (Sandbox Code Playgroud)

但这也给了我一个错误:

/bin/sh: Syntax error: end of file unexpected.
Run Code Online (Sandbox Code Playgroud)

最近我尝试过:

subprocess.Popen("konsole | /.../plottingfile.R", shell = True)
Run Code Online (Sandbox Code Playgroud)

这打开了一个新的konsole窗口但没有运行R脚本.另外,我收到以下错误:

/bin/sh: /.../plottingfile.R: Permission denied
Run Code Online (Sandbox Code Playgroud)

谢谢.

Sen*_*ran 11

首先,确保您在可以访问的地方使用platttingfile.R脚本.通常它是同一个目录.

我在互联网上读到,有一个被调用的实用程序,RScript用于R从命令行执行脚本.所以为了运行脚本你会像这样使用python:

import subprocess
retcode = subprocess.call(['/path/to/RScript','/path/to/plottingfile.R'])
Run Code Online (Sandbox Code Playgroud)

这将retcode在成功完成后返回0.如果plottingfile.R返回某种输出,它将被抛出STDOUT.如果它拉出一些GUI,那么就会出现.

如果你想捕获stdout和stderr,你可以这样做:

import subprocess
proc = subprocess.Popen(['/path/to/RScript','/path/to/plottingfile.R'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
Run Code Online (Sandbox Code Playgroud)


wes*_*erA 0

你有没有尝试过chmod u+x /pathTo/Rscript.R