Technical requirements
Currently the development is done on UNIXlike operating systems with the following software versions. Those are the recommended versions to run
rpy2
with.
Software

Version


Python  3.4 
R  3.2+ 
Running
rpy2
will require compiled libraries for R, Python, and readline; building rpy2
will require the corresponding development headers (check the documentation for more information about builing rpy2
).Getting started
It is assumed here that the rpy2 package has been properly installed. In python, making a package or module available is achieved by importing it. rpy2 is just a python package. We are going to interact with R using the
robjects
layer, a highlevel interface that tries to hide R behind a Pythonlike behavior.The r instance
The object
:data:r
in :mod:rpy2.robjects
represents the running embedded R process.
If you are familiar with R and the R console,
:data:r
is a little like a communication channel from Python to R.Getting R objects
In Python the
[
operator is an alias for the method :meth:__getitem__
.
The
:meth:__getitem__
method of :mod:rpy2.robjects.r
, evaluates a variable from the R console.
Example in R:
With
:mod:rpy2
:Under the hood, the variable
pi
is gotten by default from the R base package, unless an other variable with the name pi
was created in R’s .globalEnv
.
Whenever one wishes to be specific about where the symbol should be looked for (which should be most of the time), it possible to wrap R packages in Python namespace objects (see
:ref:robjectspackages
).Evaluating R code
The evaluation is performed in what is known to R users as the
Global Environment
, that is the place one starts at when starting the R console. Whenever the R code creates variables, those variables are “located” in that Global Environment
by default.
Example:
The expression above returns the value
18.85
, but first creates an R function f
. That function f
is present in the R Global Environement
, and can be accessed with the __getitem__
mechanism outlined above:As shown earlier, an alternative way to get the function is to get it from the
:class:R
singletonThe function
r_f
is callable, and can be used like a regular Python function.
Please check
:ref:robjectsintroductionfunctions
out for more info on calling functions.Interpolating R objects into R code strings
Against the first impression one may get from the title of this section, simple and handy features of
:mod:rpy2
are presented here.
An R object has a string representation that can be used directly into R code to be evaluated.
Simple example:
R vectors
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.
As such, the python method
:meth:add
will result in a concatenation (function c()
in R), as this is the case for regular python lists.
Accessing the one value in that vector has to be stated explicitly:
There is much that can be achieved with vectors, having them to behave more like Python lists or R vectors. A comprehensive description of the behavior of vectors is found in
:mod:robjects.vector
.
Creating rpy2
vectors
Creating R vectors can be achieved simply:
R matrixes and arrays are just vectors with a
dim
attribute.
The easiest way to create such objects is to do it through R functions:
Calling R functions
Calling R functions is similar to calling Python functions:
Keywords are also working:
By default, calling R functions return R objects.
More information on functions is in
:ref:robjectsfunctions
.
This is the nicelooking version of the official Rpy2 documentation, which can be found here: http://rpy2.readthedocs.org
No comments:
Post a Comment