Next: , Previous: Installation, Up: Starting up


2.2 Invocation

Once installed, the module can be imported with:

     >>> from rpy import *

If an error occurs, refer to the section TROUBLESHOOTING on the README file.

The module rpy imports a Python object named r, from which all the R functions and objects can be retrieved, see R objects look up. This module also implements a new Python type: Robj, which represents an arbitrary R object, see Robj type. For example:

     >>> r.wilcox_test
     <Robj object at 0x8a9e120>

is the R function wilcox.test which computes the Wilcoxon statistical test. An object of type Robj is always callable as long as the corresponding R object is:

     >>> r.wilcox_test([1,2,3], [4,5,6])
     {'p.value': 0.10000000000000001, 'statistic': {'W': 0.0},
      'null.value': {'mu': 0.0}, 'data.name': 'c(1, 2, 3) and c(4, 5, 6)',
      'alternative': 'two.sided', 'parameter': None, 'method':
      'Wilcoxon rank sum test'}

The arguments are translated automatically to R objects and the return value is translated back to Python, when this is possible (see Conversion system). This autoconversion can be customized at several levels or disabled at all.

Objects of type Robj also support keyword arguments, in the same way as normal Python functions:

     >>> r.seq(1, 3, by=0.5)
     [1.0, 1.5, 2.0, 2.5, 3]

The module rpy defines a new exception type derived from the base class Exception, called RException, see R exceptions. When any kind of error in the R interpreter occurs, an exception of this type is raised:

     >>> r.plot()
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
     rpy.RException: Error in function (x, ...)  : Argument "x" is missing,
     with no default