Python调用R编程——rpy2

在Python调用R,最常见的方式是使用rpy2模块。

简介

模块

The package is made of several sub-packages or modules:

  • rpy2.rinterface —— Low-level interface to R, when speed and flexibility matter most. Close to R’s C-level API.
  • rpy2.robjects —— High-level interface, when ease-of-use matters most. Should be the right pick for casual and general use. Based on the previous one.
  • rpy2.interactive —— High-level interface, with an eye for interactive work. Largely based on rpy2.robjects.
  • rpy2.rpy_classic —— High-level interface similar to the one in RPy-1.x. This is provided for compatibility reasons, as well as to facilitate the migration to RPy2.
  • rpy2.rlike —— Data structures and functions to mimic some of R’s features and specificities in pure Python (no embedded R process).

在Python导入R进程

import rpy2.robjects as robjects

Python中的R包

导入R包

Importing R packages is often the first step when running R code, and rpy2 is providing a function rpy2.robjects.packages.importr() that makes that step very similar to importing Python packages.

from rpy2.robjects.packages import importr

# import R's "base" package
base = importr('base')

r实例

We mentioned earlier that rpy2 is running an embedded R. This is may be a little abstract, so there is an object rpy2.robjects.r to make it tangible.

在Python获得R对象

The __getitem__() method of rpy2.robjects.r, gets the R object associated with a given symbol

>>> pi = robjects.r['pi']
>>> pi[0]
3.14159265358979

执行R语句

The object r is also callable, and the string passed in a call is evaluated as R code.

>>> piplus2 = robjects.r('pi') + 2
>>> piplus2.r_repr()
c(3.14159265358979, 2)
>>> pi0plus2 = robjects.r('pi')[0] + 2
>>> print(pi0plus2)
5.1415926535897931

R对象的表达方式

An R object has a string representation that can be used directly into R code to be evaluated.

>>> letters = robjects.r['letters']
>>> rcode = 'paste(%s, collapse="-")' %(letters.r_repr())
>>> res = robjects.r(rcode)
>>> print(res)
"a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z"

R向量

In R, data are mostly represented by vectors, even when looking like scalars. When looking closely at the R object pi used previously, we can observe that this is in fact a vector of length 1.

>>> len(robjects.r['pi'])

>>> robjects.r['pi'][0]
3.1415926535897931

创建R向量

Creating R vectors can be achieved simply.

>>> res = robjects.StrVector(['abc', 'def'])
>>> print(res.r_repr())
c("abc", "def")
>>> res = robjects.IntVector([1, 2, 3])
>>> print(res.r_repr())
1:3
>>> res = robjects.FloatVector([1.1, 2.2, 3.3])
>>> print(res.r_repr())
c(1.1, 2.2, 3.3)

The easiest way to create such objects is to do it through R functions.

>>> v = robjects.FloatVector([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])
>>> m = robjects.r['matrix'](v, nrow = 2)
>>> print(m)
     [,1] [,2] [,3]
[1,]  1.1  3.3  5.5
[2,]  2.2  4.4  6.6

调用R函数

Calling R functions is disappointingly similar to calling Python functions.

>>> rsort = robjects.r['sort']
>>> res = rsort(robjects.IntVector([1,2,3]), decreasing=True)
>>> print(res.r_repr())
c(3L, 2L, 1L)

By default, calling R functions return R objects.

一些例子

Linear models

from rpy2.robjects import FloatVector
from rpy2.robjects.packages import importr
stats = importr('stats')
base = importr('base')

ctl = FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14])
trt = FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69])
group = base.gl(2, 10, 20, labels = ["Ctl","Trt"])
weight = ctl + trt

robjects.globalenv["weight"] = weight
robjects.globalenv["group"] = group
lm_D9 = stats.lm("weight ~ group")
print(stats.anova(lm_D9))

# omitting the intercept
lm_D90 = stats.lm("weight ~ group - 1")
print(base.summary(lm_D90))

>>> print(lm_D9.names)
 [1] "coefficients"  "residuals"     "effects"       "rank"
 [5] "fitted.values" "assign"        "qr"            "df.residual" 
 [9] "contrasts"     "xlevels"       "call"          "terms"
[13] "model"

>>> print(lm_D9.rx2('coefficients'))
(Intercept)    groupTrt
      5.032      -0.371

>>> print(lm_D9.rx('coefficients'))
$coefficients
(Intercept)    groupTrt
      5.032      -0.371

Creating an R vector or matrix, and filling its cells using Python code

from rpy2.robjects import NA_Real
from rpy2.rlike.container import TaggedList
from rpy2.robjects.packages import importr

base = importr('base')

# create a numerical matrix of size 100x10 filled with NAs
m = base.matrix(NA_Real, nrow=100, ncol=10)

# fill the matrix
for row_i in xrange(1, 100+1):
    for col_i in xrange(1, 10+1):
        m.rx[TaggedList((row_i, ), (col_i, ))] = row_i + col_i * 100

R的高级接口

robject包

This module should be the right pick for casual and general use. Its aim is to abstract some of the details and provide an intuitive interface to both Python and R programmers.

>>> import rpy2.robjects as robjects

r:R的实例

The instance can be seen as the entry point to an embedded R process. The elements that would be accessible from an equivalent R environment are accessible as attributes of the instance.

>>> pi = robjects.r.pi
>>> letters = robjects.r.letters
>>> plot = robjects.r.plot
>>> dir = robjects.r.dir

When safety matters most, we recommend using __getitem__() to get a given R object.

>>> as_null = robjects.r['as.null']

Storing the object in a python variable will protect it from garbage collection, even if deleted from the objects visible to an R user.

>>> robjects.globalenv['foo'] = 1.2
>>> foo = robjects.r['foo']
>>> foo[0]
1.2

>>> robjects.r['rm']('foo')
>>> robjects.r['foo']
LookupError: 'foo' not found

>>> foo[0]
1.2

执行字符串中的R代码

Just like it is the case with RPy-1.x, on-the-fly evaluation of R code contained in a string can be performed by calling the r instance.

>>> print(robjects.r('1+2'))
[1] 3
>>> sqr = robjects.r('function(x) x^2')

>>> print(sqr)
function (x)
x^2
>>> print(sqr(2))
[1] 4

The astute reader will quickly realize that R objects named by python variables can be plugged into code through their R representation.

>>> x = robjects.r.rnorm(100)
>>> robjects.r('hist(%s, xlab="x", main="hist(x)")' %x.r_repr())

R语言环境

R environments can be described to the Python user as an hybrid of a dictionary and a scope.

The first of all environments is called the Global Environment, that can also be referred to as the R workspace.

Assigning a value to a symbol in an environment has been made as simple as assigning a value to a key in a Python dictionary.

>>> robjects.r.ls(globalenv)
>>> robjects.globalenv["a"] = 123
>>> print(robjects.r.ls(globalenv))

An environment is also iter-able, returning all the symbols (keys) it contains.

>>> env = robjects.r.baseenv()
>>> [x for x in env]
<a long list returned>

函数

R functions exposed by rpy2's high-level interface can be used:

  • like any regular Python function as they are callable objects
  • through their method rcall()

可调用性callable

from rpy2.robjects.packages import importr
base = importr('base')
stats = importr('stats')
graphics = importr('graphics')

plot = graphics.plot
rnorm = stats.rnorm
plot(rnorm(100), ylab="random")

This is all looking fine and simple until R arguments with names such as na.rm are encountered. By default, this is addressed by having a translation of ‘.’ (dot) in the R argument name into a ‘_’ in the Python argument name.

In Python one can write:

from rpy2.robjects.packages import importr
base = importr('base')

base.rank(0, na_last = True)

R is capable of introspection, and can return the arguments accepted by a function through the function formals().

>>> from rpy2.robjects.packages import importr
>>> stats = importr('stats')
>>> rnorm = stats.rnorm
>>> rnorm.formals()
<Vector - Python:0x8790bcc / R:0x93db250>
>>> tuple(rnorm.formals().names)
('n', 'mean', 'sd')

rcall()

The method Function.rcall() is an alternative way to call an underlying R function.

R的表达式——Formulae

For tasks such as modelling and plotting, an R formula can be a terse, yet readable, way of expressing what is wanted.

The class robjects.Formula is representing an R formula.

import array
from rpy2.robjects import IntVector, Formula
from rpy2.robjects.packages import importr
stats = importr('stats')

x = IntVector(range(1, 11))
y = x.ro + stats.rnorm(10, sd=0.2)

fmla = Formula('y ~ x')
env = fmla.environment
env['x'] = x
env['y'] = y

fit = stats.lm(fmla)

Other options are:

  • Evaluate R code on the fly so we that model fitting function has a symbol in R
fit = robjects.r('lm(%s)' %fmla.r_repr())
  • Evaluate R code where all symbols are defined

R包

导入R包

This is achieved by the R functions library() and require() (attaching the namespace of the package to the R search path).

from rpy2.robjects.packages import importr
utils = importr("utils")

向量和数组

Beside functions and environments, most of the objects an R user is interacting with are vector-like. For example, this means that any scalar is in fact a vector of length one.

The class Vector has a constructor:

>>> x = robjects.Vector(3)

创建向量

Creating vectors can be achieved either from R or from Python.

When the vectors are created from R, one should not worry much as they will be exposed as they should by rpy2.robjects.

When one wants to create a vector from Python, either the class Vector or the convenience classes IntVector, FloatVector, BoolVector, StrVector can be used.

因素向量 —— FactorVector

>>> sv = ro.StrVector('ababbc')
>>> fac = ro.FactorVector(sv)
>>> print(fac)
[1] a b a b b c
Levels: a b c
>>> tuple(fac)
(1, 2, 1, 2, 2, 3)
>>> tuple(fac.levels)
('a', 'b', 'c')

解析向量元素

Extracting, Python-style

The python __getitem__() method behaves like a Python user would expect it for a vector (and indexing starts at zero).

>>> x = robjects.r.seq(1, 5)
>>> tuple(x)
(1, 2, 3, 4, 5)
>>> x.names = robjects.StrVector('abcde')
>>> print(x)
a b c d e
1 2 3 4 5
>>> x[0]
1
>>> x[4]
5
>>> x[-1]
5

Extracting, R-style

Access to R-style extracting/subsetting is granted though the two delegators rx and rx2, representing the R functions [ and [[ respectively.

>>> print(x.rx(1))
[1] 1
>>> print(x.rx('a'))
a
1

向量赋值

Assigning, Python-style

Since vectors are exposed as Python mutable sequences, the assignment works as for regular Python lists.

>>> x = robjects.IntVector((1,2,3))
>>> print(x)
[1] 1 2 3
>>> x[0] = 9
>>> print(x)
[1] 9 2 3

In R vectors can be named, that is elements of the vector have a name.

>>> x = robjects.ListVector({'a': 1, 'b': 2, 'c': 3})
>>> x[x.names.index('b')] = 9

Assigning, R-style

The attributes rx and rx2 used previously can again be used:

>>> x = robjects.IntVector(range(1, 4))
>>> print(x)
[1] 1 2 3
>>> x.rx[1] = 9
>>> print(x)
[1] 9 2 3

For the sake of complete compatibility with R, arguments can be named (and passed as a dict or rpy2.rlike.container.TaggedList).

>>> x = robjects.ListVector({'a': 1, 'b': 2, 'c': 3})
>>> x.rx2[{'i': x.names.index('b')}] = 9

缺失值

In S/Splus/R special NA values can be used in a data vector to indicate that fact, and rpy2.robjects makes aliases for those available as data objects NA_Logical, NA_Real, NA_Integer, NA_Character, NA_Complex.

>>> x = robjects.IntVector(range(3))
>>> x[0] = robjects.NA_Integer
>>> print(x)
[1] NA  1  2

>>> x[0] is robjects.NA_Integer
True
>>> x[0] == robjects.NA_Integer
True
>>> [y for y in x if y is not robjects.NA_Integer]
[1, 2]

运算

To expose that to Python, a delegating attribute ro is provided for vector-like objects.

>>> x = robjects.r.seq(1, 10)
>>> print(x.ro + 1)
2:11

名字 —— Names

R vectors can have a name given to all or some of the elements. The property names can be used to get, or set, those names.

>>> x = robjects.r.seq(1, 5)
>>> x.names = robjects.StrVector('abcde')
>>> x.names[0]
'a'
>>> x.names[0] = 'z'
>>> tuple(x.names)
('z', 'b', 'c', 'd', 'e')

Array

In R, arrays are simply vectors with a dimension attribute. That fact was reflected in the class hierarchy with robjects.Array inheriting from robjects.Vector.

Matrix

A Matrix is a special case of Array. As with arrays, one must remember that this is just a vector with dimension attributes (number of rows, number of columns).

>>> m = robjects.r.matrix(robjects.IntVector(range(10)), nrow=5)
>>> print(m)
     [,1] [,2]
[1,]    0    5
[2,]    1    6
[3,]    2    7
[4,]    3    8
[5,]    4    9

>>> m = ro.r.matrix(ro.IntVector(range(2, 8)), nrow=3)
>>> print(m)
     [,1] [,2]
[1,]    2    5
[2,]    3    6
[3,]    4    7
>>> m[0]
2
>>> m[5]
7
>>> print(m.rx(1))
[1] 2
>>> print(m.rx(6))
[1] 7

DataFrame

In rpy2.robjects, DataFrame represents the R class data.frame.

Creating a DataFrame can be done by:

  • Using the constructor for the class
  • Create the data.frame through R
  • Read data from a file using the instance method from_csvfile()

The DataFrame constructor accepts either an rinterface.SexpVector (with typeof equal to VECSXP, that is, an R list) or any Python object implementing the method items() (for example dict or rpy2.rlike.container.OrdDict).

>>> d = {'a': robjects.IntVector((1,2,3)), 'b': robjects.IntVector((4,5,6))}
>>> dataf = robject.DataFrame(d)

To create a DataFrame and be certain of the clumn order order, an ordered dictionary can be used:

>>> import rpy2.rlike.container as rlc
>>> od = rlc.OrdDict([('value', robjects.IntVector((1,2,3))),
                      ('letter', robjects.StrVector(('x', 'y', 'z')))])
>>> dataf = robjects.DataFrame(od)
>>> print(dataf.colnames)
[1] "letter" "value"

Here again, Python’s __getitem__() will work as a Python programmer will expect it to:

>>> len(dataf)
2
>>> dataf[0]
<Vector - Python:0x8a58c2c / R:0x8e7dd08>

The DataFrame is composed of columns, with each column being possibly of a different type:

>>> [column.rclass[0] for column in dataf]
['factor', 'integer']
>>> dataf.rx(1)
<DataFrame - Python:0x8a584ac / R:0x95a6fb8>
>>> print(dataf.rx(1))
  letter
1      x
2      y
3      z

>>> dataf.rx2(1)
<Vector - Python:0x8a4bfcc / R:0x8e7dd08>
>>> print(dataf.rx2(1))
[1] x y z
Levels: x y z

转换R对象到Python对象

The approach followed in rpy2 has 2 levels (rinterface and robjects), and conversion functions help moving between them.

协议 —— Protocols

R vectors are mapped to Python objects implementing the methods __getitem__() / __setitem__() in the sequence protocol so elements can be accessed easily.

R functions are mapped to Python objects implementing the __call__() so they can be called just as if they were functions.

R environments are mapped to Python objects implementing __getitem__() / __setitem__() in the mapping protocol so elements can be accessed similarly to in a Python dict.

转换 —— Conversion

In its high-level interface rpy2 is using a conversion system that has the task of convertion objects between the following 3 representations: - lower-level interface to R (rpy2.rinterface level), - higher-level interface to R (rpy2.robjects level) - other (no rpy2) representations

Numpy包

高级接口

From rpy2 to numpy

R vectors or arrays can be converted to numpy arrays using numpy.array() or numpy.asarray().

import numpy

ltr = robjects.r.letters
ltr_np = numpy.array(ltr)

From numpy to rpy2

The activation (and deactivation) of the automatic conversion of numpy objects into rpy2 objects can be made with:

from rpy2.robjects import numpy2ri
numpy2ri.activate()
numpy2ri.deactivate()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,442评论 0 13
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,805评论 0 0
  • 如果可以,你是否 也选择, 不动声色的 把自己遗忘在某个角落? 沉默,再沉默 日子就这样在辗转中 成了,滑过指尖的...
    旗袍阿姐阅读 319评论 0 1
  • 先上个效果图吧 改个颜色,瞬间帅气了有木有。 实现思路: 自定义一个View,在游戏开始的时候,开启一个定时器,不...
    废柴大妈阅读 2,153评论 0 0
  • 颜青【厦门千叶集贸易有限公司】 【日精进打卡第139天】 【知~学习】 《六项精进》大纲2遍共280遍 《大学》开...
    千叶集青阅读 213评论 0 0