R-Forge Logo

rJython R package

This package allows the user to call Python from R (technically, Jython; see below). The shipped version of Jython is 2.5.1, compatible with the same Python version.

What can be done with it?

rJython is intended for running Python code from R. R programs and packages can:

Here is a sample session with rJython:

Who may be interested in it?

Mainly, users and package developers who want to use functionalities available in Python but not in R.

There are many modules in Python which could easily extend R functionality if there would be a way to call Python code from R.

How does this package relate to rpy and rpy2?

rpy and rpy2, a redesign and rewrite of rpy, are Python interfaces to R. They allow users to call R from Python.

rJython provides the opposite interface.

Besides, no code from rpy/rpy2 has been used in the development of rJython.

How does rJython work?

rJython, being based on Jython, interfaces Python via Java. Therefore, it depends on package rJava. However, it proves an alternative interface. Only advanced users might be interested on making their own calls to Python via Java directly using rJava rather than using the standard interface provided by the package.

One of the most daunting tasks in building an interface between two languages is provide mechanisms for exchanging different data structures between them. rJython uses json:

  1. R/Python objects are dumped into json strings using their respective parsers (rjson and simplejson) respectively.
  2. These strings are passed between the two languages using their string exchange mechanisms.
  3. json strings are finally reconstructed into language objects on the other side.

This simple mechanism allow R and Python exchange relatively complex data structures with ease.

Is Jython the same language as Python?

From Jython's FAQ pages:

Yes. Jython is an implementation of the Python language for the Java platform. Jython 2.5 implements the same language as CPython 2.5, and nearly all of the Core Python standard library modules. (CPython is the C implementation of the Python language.) Jython 2.5 uses the same regression test suite as CPython, with some minor modifications.

There are a number of differences. First, Jython programs cannot use CPython extension modules written in C. These modules usually have files with the extension .so, .pyd or .dll. If you want to use such a module, you should look for an equivalent written in pure Python or Java. Although it is technically feasible to support such extensions - IronPython does so - there are no plans to do so in Jython.

There are a number of other differences between the two implementations that are unlikely to go away. These range from the trivial - Jython's code objects currently do not have a co_code attribute because it is not possible to directly access Java bytecode from a class, without loading the file; to the significant - Jython uses Java's true garbage collection rather than Python's reference counting scheme.

In summary: pure Python modules can be used in Jython; modules including C code, cannot.

Further info

The project summary page can be found here. You may also be interested in its page at crantastic.

The package was originally developed by G. Grothendieck and Carlos J. Gil Bellosta. It uses Jython, that is distributed along with the package, and Python's simplejson module.