Next: , Previous: Proc conversion, Up: R to Python


4.1.3 Class conversion

This mode converts a Robj object according to a Python dictionary, named class_table, whose keys are strings or tuples of strings and its values are functions of one parameter. If a Robj object matches this table (see below), the corresponding value of the dictionary is applied to the Robj object and the result is returned as the converted object. If the Robj object has no class attribute or the class attribute does not match in class_table, then this conversion mode fails.

In order to a Robj object match the class_table dictionary, one of the following cases must be satisfied:

  1. the class R attribute of the object is a string and it is found in the class_table dictionary; or
  2. the class R attribute of the object is a vector of strings and it is found in the class_table dictionary; or
  3. the class R attribute of the object is a tuple of strings and one of the tuple's elements is found in the class_table dictionary.

For example:

     >>> set_default_mode(CLASS_CONVERSION)
     >>> def f(o):
     ...     return 5
     ...
     >>> class_table['data.frame'] = f
     >>> r.as_data_frame([1,2,3])
     5

This table is used, mainly, to translate R objects of some class, to Python objects of a class which mimics the R original class behavior. See DataFrame class.

Note that this mode is far more efficient than the PROC_CONVERSION mode. It only needs a look up in a Python dictionary.