Dear all,

I have been a long time rpy user. rpy is *very simple* to use in that
you essentially only need to remember two things:

1. Call a R function using r.func(). The only exception is to use _
for . in case that there is a dot in the function name (r.R_Version()
for R.Version() and r.dev_off() for dev.off()).

2. Call a R function or a long script using r('blah blah').

Because rpy2 is said to be replacing rpy. I had a quick look at the
rpy2 manual (I admit that I am positively impressed)  and tried to use
rpy2 today. In one of my scripts, the first call is to a R.Version
function to determine its revision number. This is where the nightmare
began.

Using rpy, this is simply int(r.R_Version()['svn rev']). Please allow
me to quote and comment on what I have done. Note that this is using
Python 2.4.3,  rpy2.0.3 on a Redhat RHEL5 system.

% python
Python 2.4.3 (#1, Sep 17 2008, 16:07:08)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-41)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from rpy2.robjects import *
>>> r.R_Version()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib64/python2.4/site-packages/rpy2/robjects/__init__.py",
line 511, in __getattribute__
    raise orig_ae
AttributeError: 'R' object has no attribute 'R_Version'
>>> r.R.Version()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib64/python2.4/site-packages/rpy2/robjects/__init__.py",
line 511, in __getattribute__
    raise orig_ae
AttributeError: 'R' object has no attribute 'R'

The _ to . conversion is gone! Then I read
http://rpy.sourceforge.net/rpy2/doc/html/robjects.html and notice
that, to use such functions, I have to use rpy_classic.

>>> from rpy2.rpy_classic import *
>>> r.R_Version()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib64/python2.4/site-packages/rpy2/rpy_classic.py", line
254, in __getattr__
    res = self.__getitem__(name)
  File "/usr/lib64/python2.4/site-packages/rpy2/rpy_classic.py", line
261, in __getitem__
    res = rpy2py(res)
  File "/usr/lib64/python2.4/site-packages/rpy2/rpy_classic.py", line
176, in rpy2py
    raise ValueError("Invalid default mode.")
ValueError: Invalid default mode.

Well, I remember there was a mode thing in rpy, but BASIC_CONVERSION
was set by default. Anyway,

>>> set_default_mode(BASIC_CONVERSION)
>>> r.R_Version()
[['x86_64-redhat-linux-gnu'], ['x86_64'], ['linux-gnu'], ['x86_64,
linux-gnu'], [''], ['2'], ['7.1'], ['2008'], ['06'], ['23'],
['45970'], ['R'], ['R version 2.7.1 (2008-06-23)']]

But where is the 'svn rev' key?

In R, this function returns something like

> R.Version()
$platform
[1] "x86_64-redhat-linux-gnu"

$arch
[1] "x86_64"

$os
[1] "linux-gnu"

and rpy returns a dictionary, as expected.

>>> r.R_Version()
{'status': '', 'major': '2', 'version.string': 'R version 2.7.1
(2008-06-23)', 'language': 'R', 'os': 'linux-gnu', 'svn rev': '45970',
'system': 'x86_64, linux-gnu', 'month': '06', 'platform':
'x86_64-redhat-linux-gnu', 'year': '2008', 'arch': 'x86_64', 'day':
'23', 'minor': '7.1'}

Maybe the raw R object has more information?

>>> set_default_mode(NO_CONVERSION)
>>> r.R_Version()
<rpy2.rpy_classic.Robj object at 0x2b3f5bda4810>
>>> r.R_Version()[0]
<rpy2.rpy_classic.Robj object at 0x2b3f5bda4b90>

What is this? I admit that I am completely lost and do not know how to proceed.

Now, what about the old faithful r('R.Version()')? In rpy, I could do
>>> r('R.Version()')
{'status': '', 'major': '2', 'version.string': 'R version 2.7.1
(2008-06-23)', 'language': 'R', 'os': 'linux-gnu', 'svn rev': '45970',
'system': 'x86_64, linux-gnu', 'month': '06', 'platform':
'x86_64-redhat-linux-gnu', 'year': '2008', 'arch': 'x86_64', 'day':
'23', 'minor': '7.1'}

In rpy2,

>>> from rpy2.rpy_classic import *
>>> set_default_mode(NO_CONVERSION)
>>> r('R.Version()')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib64/python2.4/site-packages/rpy2/rpy_classic.py", line
265, in __call__
    return self.eval(self.parse(text=s))
  File "/usr/lib64/python2.4/site-packages/rpy2/rpy_classic.py", line
199, in __call__
    a = a.getSexp()
AttributeError: 'Robj' object has no attribute 'getSexp'

What about the robject mode?

>>> from rpy2.robjects import *
>>> r('R.Version()')
<RVector - Python:0x2b74072e0830 / R:0x20256eb8>
>>> r('R.Version()')[0]
<RVector - Python:0x2b740af211b8 / R:0x20a4db48>
>>> r('R.Version()')[0][0]
'x86_64-redhat-linux-gnu'

Looks better, it is a vector, but why the first element is [0][0], not [0]?


>>> print r('R.Version()')
$platform
[1] "x86_64-redhat-linux-gnu"

..

Great, the information is there, but how can I get 'svn rev'???

>>> print r('R.Version()').getnames()
 [1] "platform"       "arch"           "os"             "system"
 [5] "status"         "major"          "minor"          "year"
 [9] "month"          "day"            "svn rev"        "language"
[13] "version.string"
>>> print r('R.Version()')['svn rev']
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib64/python2.4/site-packages/rpy2/robjects/__init__.py",
line 266, in __getitem__
    res = super(RVector, self).__getitem__(i)
TypeError: an integer is required

Finally, I found the subset function,

>>> r('R.Version()').subset('svn rev')
<RVector - Python:0x2b740af231b8 / R:0x20556ec8>
>>> print r('R.Version()').subset('svn rev')
$`svn rev`
[1] "45970"

but the only way to get the revision number seems to be

>>> r('R.Version()').subset('svn rev')[0][0]
'45970'

If the correct answer is r('R.Version()').subset('svn rev')[0][0], I
have to say that rpy2 is not for me. I can see that a lot of energy
has been devoted to make Python and R integrate better, but I do not
see any benefit from such an effort. In summary,

1. rpy_classic is not the same as rpy.
2. The R vector etc are too complicated to use, and I do not see any
benefit of using them.
3. The rpy_object stuff is almost useless without support for the . to
_ conversion, because there are so many functions in R with .
(dev.off(), as.xxxx()...).

I apologize if all these were due to my incorrect use of rpy2.
Bo

------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to