使用rpy2从python调用R脚本。

时间:2022-12-31 01:13:06

I'm very new to rpy2, as well as R.

我对rpy2和R都很陌生。

I basically have a R script, script.R, which contains functions, such as rfunc(folder). It is located in the same directory as my python script. I want to call it from Python, and then launch one of its functions. I do not need any output from this R function. I know it must be very basic, but I cannot find examples of R script-calling python codes. What I am currently doing, in Python:

我基本上有一个R脚本,脚本。R,包含函数,如rfunc(文件夹)。它位于与python脚本相同的目录中。我想从Python中调用它,然后启动它的一个函数。我不需要这个R函数的任何输出。我知道它必须是非常基本的,但是我找不到R脚本调用python代码的例子。我现在用Python做的是:

import rpy2.robjects as robjects

def pyFunction(folder):
    #do python stuff 
    r=robjects.r
    r[r.source("script.R")]
    r["rfunc(folder)"]
    #do python stuff

pyFunction(folder)

I am getting an error on the line with source:

我在与来源的连线中得到一个错误:

r[r.source("script.R")] File "/usr/lib/python2.7/dist-packages/rpy2/robjects/__init__.py", line 226, in __getitem__ res = _globalenv.get(item) TypeError: argument 1 must be string, not ListVector

r(r.source(“script.R”)]文件“/ usr / lib / python2.7 / dist-packages / rpy2 / robjects / __init__。在__getitem__ res = _globalenv.get(item)类型错误:参数1必须是字符串,而不是ListVector。

I quite do not understand how the argument I give it is not a string, and I guess the same problem will then happen on the next line, with folder being a python string, and not a R thingie.

我很不理解为什么我给出的参数不是字符串,我猜下一行也会出现同样的问题,文件夹是python字符串,而不是R什么的。

So, how can I properly call my script?

那么,我该如何正确地调用我的脚本呢?

2 个解决方案

#1


19  

source is a r function, which runs a r source file. Therefore in rpy2, we have two ways to call it, either:

source是一个r函数,它运行一个r源文件。因此在rpy2中,我们有两种方法来称呼它:

r['source']("script.R")

or

r.source("script.R")

r[r.source("script.R")] is a wrong way to do it.

r[r。source(" scriptr . r ")]是一种错误的方法。

Same idea may apply to the next line.

同样的道理也适用于下一行。

#2


3  

Rpy2 can let you turn an R script defining functions and other objects into a Python namespace:

Rpy2可以让您将定义函数和其他对象的R脚本转换为Python名称空间:

http://rpy.sourceforge.net/rpy2/doc-2.4/html/robjects_rpackages.html#importing-arbitrary-r-code-as-a-package

http://rpy.sourceforge.net/rpy2/doc - 2.4 - / - html/robjects_rpackages.html # importing-arbitrary-r-code-as-a-package

#1


19  

source is a r function, which runs a r source file. Therefore in rpy2, we have two ways to call it, either:

source是一个r函数,它运行一个r源文件。因此在rpy2中,我们有两种方法来称呼它:

r['source']("script.R")

or

r.source("script.R")

r[r.source("script.R")] is a wrong way to do it.

r[r。source(" scriptr . r ")]是一种错误的方法。

Same idea may apply to the next line.

同样的道理也适用于下一行。

#2


3  

Rpy2 can let you turn an R script defining functions and other objects into a Python namespace:

Rpy2可以让您将定义函数和其他对象的R脚本转换为Python名称空间:

http://rpy.sourceforge.net/rpy2/doc-2.4/html/robjects_rpackages.html#importing-arbitrary-r-code-as-a-package

http://rpy.sourceforge.net/rpy2/doc - 2.4 - / - html/robjects_rpackages.html # importing-arbitrary-r-code-as-a-package