Revision: 706 http://rpy.svn.sourceforge.net/rpy/?rev=706&view=rev Author: lgautier Date: 2008-11-24 22:36:20 +0000 (Mon, 24 Nov 2008)
Log Message: ----------- doc: split conversion into a separate section Modified Paths: -------------- rpy2/branches/version_2.0.x/NEWS rpy2/branches/version_2.0.x/doc/source/index.rst rpy2/branches/version_2.0.x/doc/source/numpy.rst rpy2/branches/version_2.0.x/doc/source/robjects.rst Added Paths: ----------- rpy2/branches/version_2.0.x/doc/source/robjects_convert.rst Modified: rpy2/branches/version_2.0.x/NEWS =================================================================== --- rpy2/branches/version_2.0.x/NEWS 2008-11-24 22:02:50 UTC (rev 705) +++ rpy2/branches/version_2.0.x/NEWS 2008-11-24 22:36:20 UTC (rev 706) @@ -1,5 +1,5 @@ -SVN -=== +Release 2.0.0 +============= New features ------------ Modified: rpy2/branches/version_2.0.x/doc/source/index.rst =================================================================== --- rpy2/branches/version_2.0.x/doc/source/index.rst 2008-11-24 22:02:50 UTC (rev 705) +++ rpy2/branches/version_2.0.x/doc/source/index.rst 2008-11-24 22:36:20 UTC (rev 706) @@ -9,6 +9,7 @@ introduction robjects numpy + robjects_convert rinterface rpy_classic rlike Modified: rpy2/branches/version_2.0.x/doc/source/numpy.rst =================================================================== --- rpy2/branches/version_2.0.x/doc/source/numpy.rst 2008-11-24 22:02:50 UTC (rev 705) +++ rpy2/branches/version_2.0.x/doc/source/numpy.rst 2008-11-24 22:36:20 UTC (rev 706) @@ -50,13 +50,13 @@ Although both are valid and reasonable options, the design decision was taken in order to decouple `rpy2` from `numpy` the most, and do not assume that having `numpy` installed automatically - meant that a programmer wanted to use it. + meant that a programmer wanted to use it. +.. note:: -.. literalinclude:: ../../rpy/robjects/numpy2ri.py + The module :mod:`numpy2ri` is an example of how custom conversion to + and from :mod:`rpy2.robjects` can be performed. - - Low-level interface ------------------- Modified: rpy2/branches/version_2.0.x/doc/source/robjects.rst =================================================================== --- rpy2/branches/version_2.0.x/doc/source/robjects.rst 2008-11-24 22:02:50 UTC (rev 705) +++ rpy2/branches/version_2.0.x/doc/source/robjects.rst 2008-11-24 22:36:20 UTC (rev 706) @@ -518,69 +518,3 @@ fit = robjects.r('lm(%s)' %fmla.r_repr()) - -Mapping rpy2 objects to arbitrary python objects -===================================================== - -The conversion, often present when working with RPy-1.x, is no longer -necessary as the R objects can be either passed on to R functions -or used in Python. - -However, there is a low-level mapping between `R` and `Python` objects -performed behind the (Python-level) scene, done by the :mod:`rpy2.rinterface`, -while an higher-level mapping is done between low-level objects and -higher-level objects using the functions: - -:meth:`conversion.ri2py` - :mod:`rpy2.rinterface` to Python. By default, this function - is just an alias for the function :meth:`default_ri2py`. - -:meth:`conversion.py2ri` - Python to :mod:`rpy2.rinterface`. By default, this function - is just an alias for the function :meth:`default_py2ri`. - -:meth:`conversion.py2ro` - Python to :mod:`rpy2.robjects`. By default, that one function - is merely a call to :meth:`conversion.py2ri` - followed by a call to :meth:`conversion.ri2py`. - -Those functions can be re-routed to satisfy all requirements, with -the easiest option being to write a custom function calling itself -the default functions. - -Switching to `numpy`-to-`rpy2` is using this mechanism. - -As an example, let's assume that one want to return atomic values -whenever an R numerical vector is of length one. This is only a matter -of writing a new function `ri2py` that handles this, as shown below: - -.. code-block:: python - - import rpy2.robjects as robjects - - def my_ri2py(obj): - res = robjects.default_ri2py(obj) - if isinstance(res, robjects.RVector) and (len(res) == 1): - res = res[0] - return res - - robjects.conversion.ri2py = my_ri2py - -Once this is done, we can verify immediately that this is working with: - ->>> pi = robjects.r.pi ->>> type(pi) -<type 'float'> ->>> - -The default behavior can be restored with: - ->>> robjects.conversion.ri2py = default_ri2py - -The docstrings for :meth:`default_ri2py`, :meth:`default_py2ri`, and :meth:`py2ro` are: - -.. autofunction:: rpy2.robjects.default_ri2py -.. autofunction:: rpy2.robjects.default_py2ri -.. autofunction:: rpy2.robjects.default_py2ro - - Added: rpy2/branches/version_2.0.x/doc/source/robjects_convert.rst =================================================================== --- rpy2/branches/version_2.0.x/doc/source/robjects_convert.rst (rev 0) +++ rpy2/branches/version_2.0.x/doc/source/robjects_convert.rst 2008-11-24 22:36:20 UTC (rev 706) @@ -0,0 +1,71 @@ + +Mapping rpy2 objects to arbitrary python objects +===================================================== + +Switching between a conversion and a no conversion mode, +an operation often present when working with RPy-1.x, is no longer +necessary as the R objects can be either passed on to R functions +or used in Python. + +However, there is a low-level mapping between `R` and `Python` objects +performed behind the (Python-level) scene, done by the :mod:`rpy2.rinterface`, +while an higher-level mapping is done between low-level objects and +higher-level objects using the functions: + +:meth:`conversion.ri2py` + :mod:`rpy2.rinterface` to Python. By default, this function + is just an alias for the function :meth:`default_ri2py`. + +:meth:`conversion.py2ri` + Python to :mod:`rpy2.rinterface`. By default, this function + is just an alias for the function :meth:`default_py2ri`. + +:meth:`conversion.py2ro` + Python to :mod:`rpy2.robjects`. By default, that one function + is merely a call to :meth:`conversion.py2ri` + followed by a call to :meth:`conversion.ri2py`. + +Those functions can be re-routed to satisfy all requirements, with +the easiest option being to write a custom function calling itself +the default function when the custom conversion should not apply. + +A simple example +---------------- + +As an example, let's assume that one want to return atomic values +whenever an R numerical vector is of length one. This is only a matter +of writing a new function `ri2py` that handles this, as shown below: + +.. code-block:: python + + import rpy2.robjects as robjects + + def my_ri2py(obj): + res = robjects.default_ri2py(obj) + if isinstance(res, robjects.RVector) and (len(res) == 1): + res = res[0] + return res + + robjects.conversion.ri2py = my_ri2py + +Once this is done, we can verify immediately that this is working with: + +>>> pi = robjects.r.pi +>>> type(pi) +<type 'float'> +>>> + +The default behavior can be restored with: + +>>> robjects.conversion.ri2py = default_ri2py + +Default functions +----------------- + +The docstrings for :meth:`default_ri2py`, :meth:`default_py2ri`, and :meth:`py2ro` are: + +.. autofunction:: rpy2.robjects.default_ri2py +.. autofunction:: rpy2.robjects.default_py2ri +.. autofunction:: rpy2.robjects.default_py2ro + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list