简体   繁体   English

如何从在Python中执行的R脚本返回多个对象

[英]How to return multiple objects from R script executed in Python

So I am struggling to use the rpy2 package to integrate some workflow between R and Python. 因此,我努力使用rpy2包在R和Python之间集成一些工作流。

For example, imagine I'm trying to run a Python script that does a linear regression in R and I want to return all of the elements of that (in reality I'm trying to do something much more complicated than this). 例如,假设我正在尝试运行一个在R中进行线性回归的Python脚本,并且我想返回该脚本的所有元素(实际上,我正在尝试做比这复杂得多的事情)。

I execute the following in Python (calling R; This assumes you have rpy2 installed): 我在Python中执行以下命令(调用R;这假定您已安装rpy2 ):

import rpy2.robjects as ro
test = ro.r('''
            # Load in data
            df <- mtcars
            # Run regression
            out = lm(formula='mpg ~ cyl + hp + wt',data=df)
            ''')

And now what? 现在呢? I have a questions: 我有一个问题:

  1. How do I pull the various elements from the result? 如何从结果中提取各种元素? In R they would be out$coefficients and out$residuals , etc. I know there is documentation for this, but I'm a bit lost. 在R中,它们的输出将是out$coefficientsout$residuals等。我知道有关于此的文档,但是我有点迷失了。 Ideally, I would want the elements in useful formats, so pandas dataframes or indexed lists, etc. 理想情况下,我希望元素使用有用的格式,例如pandas数据框或索引列表等。

  2. What happens to df ? df怎样? robjects.r() seems to just save whatever the last thing you gave and throw away everything else. robjects.r()似乎只是保存您最后提供的所有内容,而丢弃其他所有内容。 I suppose I can work with this, but it's not ideal. 我想我可以解决这个问题,但这并不理想。

  3. Related to 2: Is there a much much better way to do this? 与2相关:有更好的方法吗? In general if someone could put forward a "best practice" for this sort of thing, that would be helpful, since I'm sure that there are many people interested in using Python, but occasionally have a very custom function they need to call using R but they don't want to get to fancy with the integration. 通常,如果有人可以为这种事情提出“最佳实践”,那将会很有帮助,因为我确信有很多人对使用Python感兴趣,但是偶尔会有一个非常自定义的函数需要使用R,但是他们不想看上这种集成。 Perhaps a way to call an R function using Pythonic input arguments would be great. 使用Pythonic输入参数调用R函数的方法也许很棒。

Q.1: How do I pull the various elements from the result? 问题1:如何从结果中提取各种元素?

Ans.1: After you run your R script: 答案1:运行R脚本后:

test = ro.r(your_R_script)

You can use this code to print out all the names and values in the test object. 您可以使用此代码打印出test对象中的所有namesvalues

# iterate on names and values
# be careful output is v long
for n,v in test.items():
    print(n)
    print(v)

To list all available names , run this code: 要列出所有可用names ,请运行以下代码:

test.names

The output: 输出:

StrVector with 12 elements.
'coeffici... 'residuals' 'effects' 'rank' ... 'xlevels' 'call'  'terms' 'model'

To print values of the 'residuals', run this: 要打印“残差”的值,请运行以下命令:

test[test.names.index('residuals')]

Q.2: What happens to df? 问题2:df会怎样?

Ans.2: It is still available in R environment until you delete it. 回答2:在您删除它之前,它在R环境中仍然可用。 You can run simple R code to check: 您可以运行简单的R代码来检查:

ro.r('''
        # View dataframe
        df
        ''')

Q.3: Is there a much much better way to do this? Q.3:有更好的方法吗?

Ans.3: (No answer.) 答案3 :(无答案。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM