作者:plutoese
链接:https://www.jianshu.com/p/d8578362245a
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 functionrpy2.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 objectrpy2.robjects.rto 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]
函数
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()
>>> 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 classrobjects.Formulais 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 classesIntVector, FloatVector, BoolVector, StrVectorcan 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 objectsNA_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,DataFramerepresents 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) orany 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]
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)
>>> print(dataf.rx(1))
letter
1 x
2 y
3 z
>>> dataf.rx2(1)
>>> 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()