Next: , Previous: Modes, Up: R to Python


4.1.2 Proc conversion

This mode converts a Robj object according to a Python dictionary, named proc_table, whose keys and values are functions of one parameter. The keys are applied sequentially to the Robj object:

For example:

     >>> set_default_mode(PROC_CONVERSION)
     >>> def check_str(o):
     ...     return r.is_character(o)
     ...
     >>> def f(o):
     ...     o_py = o.as_py(BASIC_CONVERSION)
     ...     if o_py == 'foo':
     ...          return 'cannot return "foo"'
     ...     return o_py
     ...
     >>> proc_table[check_str] = f
     >>> r.c('bar')
     'bar'
     >>> r.c('foo')
     'cannot return "foo"'
     >>> r.c(['bar','foo'])
     ['bar', 'foo']

Note that the conversion is not applied recursively. This mode is applied only before returning the final result to Python.

This conversion mode can be used for many purposes (see Useful examples); but, mainly, it is used to test whether a R object has some attribute, and to act in consequence.

Note that this conversion mode is not efficient if the proc_table dictionary has many keys, because, usually, all of them must be checked. On the other hand, with only one key which always returns true, it can be used to completely intercept the conversion system (see Enhanced Robj).