Next: , Previous: Interface description, Up: Interface description


3.1 R objects look up

There are two ways of retrieving a R object via the Python r object:

The two ways are completely equivalent, the only difference is that some names cannot be used as attributes, so in some cases you must use the second way. The first time that a R object is required, it is looked up in the R global environment and it is cached in a dictionary in the r object. After then, retrieving the same object is only a look up in a Python dictionary.

The first way of retrieving a R object is as attributes of the r object. For example:

     r.seq
     r.as_data_frame
     r.print_

refer to the R functions seq, as.data.frame and print respectively. Note that some kind of name conversion is required in order to make the attributes valid Python identifiers. But the rules of name conversions are pretty simple; namely, the following conversions are applied to Python identifiers

Python name R name
—– —–
Underscore (‘_’) dot (‘.’)
Double underscore (‘__’) arrow (<-)
Final underscore (preceded by a letter) is removed

The final rule is used to allow the retrieving of R objects whose names are Python keywords. Some additional examples:

Python name R name
—– —–
t_test t.test
attr__ attr<-
parent_env__ parent.env<-
class_ class

The second way of retrieving a R object is as keywords of the r object. In this form, no name conversion is required. The string used as keyword must be, exactly, the R name of the object. For example:

     r['as.data.frame']
     r['print']
     r['$']

refer to the corresponding R functions. Note that with this syntax you can retrieve functions such as $, $<- or [[, which are impossible to express with the attribute syntax. However, the attributes are more appealing to the eyes.

Due to the dynamic nature of the look up, when installing additional modules in the R system, it is not necessary to make changes in the interface. All you have to do is to load the module in the same way as in R:

     >>> r.library('splines')
     ['splines', 'ctest', 'base']