William Stein wrote:
> Hi Andrew (cc: sage-devel),
> 
> I just tried out rpy2 (a superfast C-level Python<-->R interface) and
> the user interface is amazingly ridiculously painfully to actually
> interactively use.   Evidently rpy was removed from sage and replaced
> by rpy2 sometime this summer.  Anyway, with rpy we had:
> 
> sage: import rpy; rpy.sd([1..100])    # no longer works in sage
> 
> to compute the standard deviation of a list.  With rpy2 the same thing 
> becomes:
> 
> sage: import rpy2.robjects              # using sage-4.2.1
> sage: rpy2.robjects.r['sd'](rpy2.robjects.IntVector([1..100])).r_repr()
> '29.011491975882'
> 
> Just look at that carefully.  Ouch.
> 
> Anyway, any thoughts?   I wonder if it would be easy to provide a
> lightweight wrapper around rpy2 that would
> provide a much more sensible interface.   At least the docs at
> http://rpy.sourceforge.net/rpy2/doc/html/index.html are pretty good.
> 

Like this?

import rpy2.rpy_classic as rpy
r=rpy.r
rpy.set_default_mode(rpy.BASIC_CONVERSION)

r.sd([int(i) for i in [1..100]])
[29.011491975882016]

(the only problem here is that the internal function doesn't know about 
Sage types, which is very easily fixed...)


Or do you mean a wrapper like this?

import rpy2
import rpy2.robjects as ro
import numpy
import rpy2.robjects.numpy2ri as numpy2ri
def my_py2ri(o):
     if isinstance(o,(list,tuple)):
         o=ro.IntVector(o)
     else:
         o=ro.default_py2ri(o)
     return o
ro.conversion.py2ri=my_py2ri

def my_ri2py(obj):
     res = ro.default_ri2py(obj)
     if isinstance(res, ro.RVector):
         if len(res) == 1:
             res = res[0]
         else:
             res = [res[i] for i in range(len(res))]
     return res
ro.conversion.ri2py = my_ri2py

r=rpy2.robjects.r
r.sd([1..100])
        

29.011491975882016



-Jason


-- 
Jason Grout

-- 
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to