Revision: 573 http://rpy.svn.sourceforge.net/rpy/?rev=573&view=rev Author: lgautier Date: 2008-07-05 07:48:02 -0700 (Sat, 05 Jul 2008)
Log Message: ----------- robjects: - Experimental class RDataFrame Modified Paths: -------------- branches/rpy_nextgen/rpy/robjects/__init__.py branches/rpy_nextgen/rpy/robjects/tests/__init__.py branches/rpy_nextgen/rpy/robjects/tests/testRArray.py Added Paths: ----------- branches/rpy_nextgen/rpy/robjects/tests/testRDataFrame.py Modified: branches/rpy_nextgen/rpy/robjects/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/__init__.py 2008-07-05 14:44:57 UTC (rev 572) +++ branches/rpy_nextgen/rpy/robjects/__init__.py 2008-07-05 14:48:02 UTC (rev 573) @@ -18,14 +18,22 @@ #FIXME: close everything when leaving (check RPy for that). def default_ri2py(o): + res = None if isinstance(o, RObject): res = o elif isinstance(o, rinterface.SexpVector): try: - dim = o.do_slot("dim") - res = RArray(o) + cl = o.do_slot("class")[0] + if cl == 'data.frame': + res = RDataFrame(o) except LookupError, le: - res = RVector(o) + pass + if res is None: + try: + dim = o.do_slot("dim") + res = RArray(o) + except LookupError, le: + res = RVector(o) elif isinstance(o, rinterface.SexpClosure): res = RFunction(o) elif isinstance(o, rinterface.SexpEnvironment): @@ -42,6 +50,7 @@ #FIXME: better names for the functions ri2py = default_ri2py + def default_py2ri(o): if isinstance(o, RObject): res = rinterface.Sexp(o) @@ -252,7 +261,9 @@ def __setattr__(self, name, value): if name == 'dim': value = py2ro(value) - res = r["dim<-"](value) + res = r["dim<-"](self, value) + #FIXME: not properly done + raise(Exception("Not yet implemented")) def getnames(self): """ Return a list of name vectors @@ -262,6 +273,7 @@ return res + class RMatrix(RArray): """ An R matrix """ @@ -273,13 +285,43 @@ """ Number of columns """ return self.dim[1] -class DataFrame(RVector): - #FIXME: not implemented - def __init__(self, o): - raise Exception("not implemented.") +class RDataFrame(RVector): + def __init__(self, *args, **kwargs): + if len(args) > 1: + raise(ValueError("Only one unnamed parameter is allowed.")) + if len(args) == 1: + if len(kwargs) != 0: + raise(ValueError("No named parameters allowed when there is an unnamed parameter.")) + else: + super(RDataFrame, self).__init__(args[0]) + else: + if len(kwargs) == 0: + raise(ValueError("Initialization parameters needed.")) + df = baseNameSpaceEnv["data.frame"](**kwargs) + super(RDataFrame, self).__init__(df) + #import pdb; pdb.set_trace() + if not baseNameSpaceEnv["is.data.frame"](self)[0]: + raise(TypeError("The object must be representing an R data.frame")) + + def nrow(self): + """ Number of rows """ + return baseNameSpaceEnv["nrow"](self)[0] + + def ncol(self): + """ Number of columns """ + return baseNameSpaceEnv["ncol"](self)[0] + + def rownames(self): + return baseNameSpaceEnv["colnames"](self)[0] + + def colnames(self): + return baseNameSpaceEnv["colnames"](self)[0] + + + class RFunction(RObjectMixin, rinterface.SexpClosure): """ An R function (aka "closure"). Modified: branches/rpy_nextgen/rpy/robjects/tests/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/tests/__init__.py 2008-07-05 14:44:57 UTC (rev 572) +++ branches/rpy_nextgen/rpy/robjects/tests/__init__.py 2008-07-05 14:48:02 UTC (rev 573) @@ -3,6 +3,7 @@ import testRObject import testRVector import testRArray +import testRDataFrame import testRFormula import testRFunction import testREnvironment @@ -12,6 +13,7 @@ suite_RObject = testRObject.suite() suite_RVector = testRVector.suite() suite_RArray = testRArray.suite() + suite_RDataFrame = testRDataFrame.suite() suite_RFunction = testRFunction.suite() suite_REnvironment = testREnvironment.suite() suite_RFormula = testRFormula.suite() @@ -19,6 +21,7 @@ alltests = unittest.TestSuite([suite_RObject, suite_RVector, suite_RArray, + suite_RDataFrame, suite_RFunction, suite_REnvironment, suite_RFormula, Modified: branches/rpy_nextgen/rpy/robjects/tests/testRArray.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/tests/testRArray.py 2008-07-05 14:44:57 UTC (rev 572) +++ branches/rpy_nextgen/rpy/robjects/tests/testRArray.py 2008-07-05 14:48:02 UTC (rev 573) @@ -20,7 +20,11 @@ self.assertEquals(5, d[0]) self.assertEquals(3, d[1]) - def testGetNames(self): +# rd = robjects.r.rev(d) +# a.dim = rd + + + def testGetnames(self): dimnames = robjects.r.list(['a', 'b', 'c'], ['d', 'e']) m = robjects.r.matrix(1, nrow=3, ncol=2, Added: branches/rpy_nextgen/rpy/robjects/tests/testRDataFrame.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/tests/testRDataFrame.py (rev 0) +++ branches/rpy_nextgen/rpy/robjects/tests/testRDataFrame.py 2008-07-05 14:48:02 UTC (rev 573) @@ -0,0 +1,29 @@ +import unittest +import rpy2.robjects as robjects +rinterface = robjects.rinterface +import array + +class RDataFrameTestCase(unittest.TestCase): + + def testNew(self): + letters = robjects.r.letters + numbers = robjects.r('1:26') + df = robjects.RDataFrame(letters=letters, numbers=numbers) + + self.assertEquals("data.frame", df.rclass()[0]) + + def testDim(self): + letters = robjects.r.letters + numbers = robjects.r('1:26') + df = robjects.RDataFrame(letters=letters, numbers=numbers) + + self.assertEquals(26, df.nrow()) + self.assertEquals(2, df.ncol()) + + +def suite(): + suite = unittest.TestLoader().loadTestsFromTestCase(RDataFrameTestCase) + return suite + +if __name__ == '__main__': + unittest.main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08 _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list