rpy2(版本2.3.10)——从R包中导入数据到python中。

时间:2023-02-01 23:16:17

So I am trying to import some data from an R package into python in order to test some other python-rpy2 functions that I have written. In particular, I am using the SpatialEpi package in R and the pennLC dataset.

因此,我试图从一个R包中导入一些数据到python中,以便测试我编写的其他一些python-rpy2函数。特别是,我正在使用R和pennLC数据集中的SpatialEpi包。

So I was able to import the rpy2 package and connect to the package correctly. However, I am not sure how to access the data in the package.

因此我能够导入rpy2包并正确地连接到包。但是,我不确定如何访问包中的数据。

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
spep = importr("SpatialEpi")

However, I can't seem to access the data object pennLC in the SpatialEpi package to test the function. The equivalent R command would be:

但是,我似乎无法访问spatial alepi包中的数据对象pennLC来测试这个函数。等效的R命令是:

data(pennLC)

Any suggestions.

任何建议。

2 个解决方案

#1


2  

In R, doing data("foo") can create an arbitrary number of objects in the workspace. In rpy2 things are contained in an environment. This is making it cleaner.

在R中,执行data(“foo”)可以在工作区中创建任意数量的对象。在rpy2中,事物包含在一个环境中。这使它更干净。

from rpy2.robjects.packages import importr, data
spep = importr("SpatialEpi")
pennLC_data = data(spep).fetch('pennLC')

pennLC_data is an Environment (think of it as a namespace).

pennLC_data是一个环境(可以将它看作一个名称空间)。

To list what was fetched:

列出所取之物:

pennLC_data.keys()

To get the data object wanted:

想要得到数据对象:

pennLC_data['pennLC'] # guessing here, it might be a different name

#2


1  

So I figured out an answer based upon some guidance from Laurent's message above.

所以我根据上面劳伦特的信息找到了答案。

I am using rpy2 version 2.3.10, so that introduces some differences from Laurent's code above. Here is what I did.

我正在使用rpy2版本2.3.10,因此这与上面的Laurent代码有些不同。这是我所做的。

import rpy2.objects as robj
from rpy2.robjects.packages import importr
spep = importr('SpatialEpi', data = True)
data = spep.__rdata__.fetch('pennLC')

First note that there is no .data method in rpy2 2.3.10--the name might have changed. But instead, the 2.3.10 documentation indicates that using the data=True argument in the importr will place an PackageData object under .Package.__rdata__ . So I can do afetchon therdata` object.

首先注意,rpy2 2.3.10中没有.data方法——名称可能已经更改。但是,2.3.10文档指出,在importr中使用data=True参数将把PackageData对象放在. package下面。__rdata__。所以我可以做一个afetchon therdata的对象。

Then when I want to access the data, I can use the following code.

然后,当我想访问数据时,我可以使用以下代码。

data['pennLC'][1]

In [43]: type(d['pennLC'][1])
Out[43]: rpy2.robjects.vectors.DataFrame

To view the data:

查看数据:

print(data['pennLC'][1])

#1


2  

In R, doing data("foo") can create an arbitrary number of objects in the workspace. In rpy2 things are contained in an environment. This is making it cleaner.

在R中,执行data(“foo”)可以在工作区中创建任意数量的对象。在rpy2中,事物包含在一个环境中。这使它更干净。

from rpy2.robjects.packages import importr, data
spep = importr("SpatialEpi")
pennLC_data = data(spep).fetch('pennLC')

pennLC_data is an Environment (think of it as a namespace).

pennLC_data是一个环境(可以将它看作一个名称空间)。

To list what was fetched:

列出所取之物:

pennLC_data.keys()

To get the data object wanted:

想要得到数据对象:

pennLC_data['pennLC'] # guessing here, it might be a different name

#2


1  

So I figured out an answer based upon some guidance from Laurent's message above.

所以我根据上面劳伦特的信息找到了答案。

I am using rpy2 version 2.3.10, so that introduces some differences from Laurent's code above. Here is what I did.

我正在使用rpy2版本2.3.10,因此这与上面的Laurent代码有些不同。这是我所做的。

import rpy2.objects as robj
from rpy2.robjects.packages import importr
spep = importr('SpatialEpi', data = True)
data = spep.__rdata__.fetch('pennLC')

First note that there is no .data method in rpy2 2.3.10--the name might have changed. But instead, the 2.3.10 documentation indicates that using the data=True argument in the importr will place an PackageData object under .Package.__rdata__ . So I can do afetchon therdata` object.

首先注意,rpy2 2.3.10中没有.data方法——名称可能已经更改。但是,2.3.10文档指出,在importr中使用data=True参数将把PackageData对象放在. package下面。__rdata__。所以我可以做一个afetchon therdata的对象。

Then when I want to access the data, I can use the following code.

然后,当我想访问数据时,我可以使用以下代码。

data['pennLC'][1]

In [43]: type(d['pennLC'][1])
Out[43]: rpy2.robjects.vectors.DataFrame

To view the data:

查看数据:

print(data['pennLC'][1])