Revision: 412 http://rpy.svn.sourceforge.net/rpy/?rev=412&view=rev Author: lgautier Date: 2008-03-03 13:28:08 -0800 (Mon, 03 Mar 2008)
Log Message: ----------- - added pydoc strings - fixed Renvironment - added class for S4 object (not doing much at the moment) - start R with --quiet Modified Paths: -------------- trunk/sandbox/rpy_nextgen/robjects/__init__.py Modified: trunk/sandbox/rpy_nextgen/robjects/__init__.py =================================================================== --- trunk/sandbox/rpy_nextgen/robjects/__init__.py 2008-03-02 19:52:28 UTC (rev 411) +++ trunk/sandbox/rpy_nextgen/robjects/__init__.py 2008-03-03 21:28:08 UTC (rev 412) @@ -1,3 +1,11 @@ +""" +R objects as Python objects. + +The module is structured around the singleton r of class R, +that represents an embedded R. + +""" + import array import rinterface @@ -4,8 +12,6 @@ #FIXME: close everything when leaving (check RPy for that). - - def defaultPy2RMapper(self, o): if isinstance(o, Robject): return o._sexp @@ -37,6 +43,8 @@ res = Rfunction(o) elif isinstance(o, rinterface.SexpEnvironment): res = Renvironment(o) + elif isinstance(o, rinterface.SexpS4): + res = RS4(o) else: res = o return res @@ -50,9 +58,14 @@ #FIXME: Looks hackish. inheritance should be used ? class Robject(object): - pass + def __repr__(self): + r.show(self) + class Rvector(Robject): + """ R vector-like object. Items in those instances can + be accessed with the method "__getitem__" ("[" operator), + or with the method "subset".""" def __init__(self, o): if (isinstance(o, rinterface.SexpVector)): @@ -61,6 +74,7 @@ self._sexp = defaultPy2RMapper(self, o) def subset(self, *args): + """ Subset the "R-way.", using R's "[" function """ for a in args: if not isinstance(a, rinterface.SexpVector): raise(TypeError("Subset only take R vectors")) @@ -101,6 +115,11 @@ class Rfunction(Robject): + """ An R function (aka "closure"). + + The attribute "mapper" is a function that will + transform Python objects into Sexp objects. + """ mapper = defaultPy2RMapper @@ -121,17 +140,28 @@ class Renvironment(Robject): + """ An R environement. """ def __init__(self, o): if (isinstance(o, rinterface.SexpEnvironment)): self._sexp = o else: raise(ValueError("Cannot instantiate")) - def __getattr__(self, attr): - res = self._sexp[attr] + def __getitem__(self, item): + res = self._sexp[item] + res = mapper(res) return res +class RS4(Robject): + def __init__(self, o): + if (isinstance(o, rinterface.SexpS4)): + self._sexp = o + else: + raise(ValueError("Cannot instantiate")) + def __getattr__(self, attr): + res = r.get("@")(self, attr) + return res class R(object): _instance = None @@ -145,10 +175,13 @@ raise(ValueError("Only one instance of R can be created")) def __getattribute__(self, attr): - res = rinterface.globalEnv.get(attr) + return self[attr] + + def __getitem__(self, item): + res = rinterface.globalEnv.get(item) res = mapper(res) return res -r = R(["--no-save", ]) +r = R(["--no-save", "--quiet"]) 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: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list