Python is a popular all-purpose scripting language, while R (an open source implementation of the S/Splus language) is a scripting language mostly popular for data analysis, statistics, and graphics. If you are reading this, there are good chances that you are at least familiar with one of the two.
Having an interface between both languages, and benefit from the respective libraries of one language while working in the other language, appeared desirable and an early option to achieve it was the RSPython project, itself part of the Omegahat project.
A bit later, the RPy project appeared and focused on providing simple and robust access to R from within Python, with the initial Unix-only releases quickly followed by Microsoft and MacOS compatible versions. This project is referred to as RPy-1.x in the rest of this document.
The present documentation describes RPy2, an evolution of RPy-1.x. Naturally RPy2 is inspired by RPy, but also by A. Belopolskys’s contributions that were waiting to be included into RPy.
This effort can be seen as a redesign and rewrite of the RPy package.
Python version 2.4 or higher, as well as R-2.7.0 or higher are required.
Currently the development is mostly done on Linux and MacOS X with the following versions for the softwares:
Software | Versions |
---|---|
Python | 2.5; 2.6 |
R | 2.8; 2.9 |
gcc-4.3 is used for compiling the C parts.
In theory we could have available for download:
- Source packages.
- Pre-compiled binary packages for
- Microsoft’s Windows
- Apple’s MacOS X
- Linux distributions
rpy2 has been reported compiling successfully on all 3 platforms, provided that development items such as Python headers and a C compiler are installed.
Check on the Sourceforge download page what is available.
Note
Choose files from the rpy2 package, not rpy.
Debian packages are available thanks to Dirk Eddelbuettel. This is likely to mean that Ubuntu and other Debian-based distributions will also have it in their repositories.
If available, the executable can be run; this will install the package in the default Python installation.
At the time of writing, Microsoft Windows binaries are contributed by Laurent Oget (from Predictix) since version 2.0.0b1.
The source package is on the PYthon Package Index (PYPI), and the easy_install script can be used whenever available. The shell command will then just be:
easy_install rpy2
To install from a downloaded source archive <rpy_package> do in a shell:
tar -xzf <rpy_package>.tar.gz
cd <rpy_package>
python setup.py install
At any time, an installation can be tested as follows:
import rpy2.tests
import unittest
# the verbosity level can be increased if needed
tr = unittest.TextTestRunner(verbosity = 1)
suite = rpy2.tests.suite()
tr.run(suite)
Note
At the time of writing, 2 unit tests will fail. Their failure is forced, because terminating then starting again an embbeded R is causing problems.
Warning
Win32 versions are still lacking some of the functionalities in the UNIX-alike versions, most notably the callback function for console input and output.
The package is made of several sub-packages or modules:
Higher-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.
Higher-level interface, when ease-of-use matters most.
Low-level interface to R, when speed and flexibility matter most. Here the programmer gets close(r) to R’s C-level API.
Data structures and functions to mimic some of R’s features and specificities
When designing ryp2, attention was given to make:
rpy2.robjects implements an extension to the interface in rpy2.rinterface by extending the classes for R objects defined there with child classes.
The choice of inheritance was made to facilitate the implementation of mostly inter-exchangeable classes between rpy2.rinterface and rpy2.robjects. For example, an rpy2.rinterface.SexpClosure can be given any rpy2.robjects.RObject as a parameter while any rpy2.robjects.RFunction can be given any rpy2.rinterface.Sexp. Because of R’s functional basis, a container-like extension is also present.
The module rpy2.rpy_classic is using delegation, letting us demonstrate how to extend rpy2.rinterface with an alternative to inheritance.
Acknowledgements go to: