Revision: 662 http://rpy.svn.sourceforge.net/rpy/?rev=662&view=rev Author: lgautier Date: 2008-10-25 17:26:39 +0000 (Sat, 25 Oct 2008)
Log Message: ----------- - Added check in constructor for robjects.RDataFrame - Edits in the doc regarding data frames Modified Paths: -------------- branches/rpy_nextgen/NEWS branches/rpy_nextgen/doc/source/robjects.rst branches/rpy_nextgen/rpy/robjects/__init__.py branches/rpy_nextgen/rpy/robjects/tests/testRDataFrame.py Modified: branches/rpy_nextgen/NEWS =================================================================== --- branches/rpy_nextgen/NEWS 2008-10-20 18:37:54 UTC (rev 661) +++ branches/rpy_nextgen/NEWS 2008-10-25 17:26:39 UTC (rev 662) @@ -1,6 +1,13 @@ SVN === +Changes +------- + +:mod:`rpy2.robjects`: + +- Constructor for :class:`rpy2.robjects.RDataFrame` checks that R lists are data.frames (not all lists are data.frame) + Bugs fixed ---------- @@ -32,7 +39,7 @@ :mod:`rpy2.robjects`: -- constructor for RDataFrame now now accepts either :class:`rlike.container.TaggedList` or :class:`rinterface.SexpEnvironment` +- constructor for RDataFrame now now accepts either :class:`rlike.container.TaggedList` or :class:`rinterface.SexpVector` :mod:`rpy2.rinterface`: Modified: branches/rpy_nextgen/doc/source/robjects.rst =================================================================== --- branches/rpy_nextgen/doc/source/robjects.rst 2008-10-20 18:37:54 UTC (rev 661) +++ branches/rpy_nextgen/doc/source/robjects.rst 2008-10-25 17:26:39 UTC (rev 662) @@ -286,18 +286,56 @@ A :class:`RMatrix` is a special case of :class:`RArray`. -:class:`RDataFrame` -------------------- +Data frames +----------- -A :class:`RDataFrame` represents the `R` class `data.frame`. -Currently, the constructor is flagged as experimental. -It accepts either a :class:`rinterface.SexpVector` -(with :attr:`typeof` equal to *VECSXP*) +Data frames are important data structures in R, as they are used to +represent a data to analyze in a study in a relatively +large nunmber of cases. + +A data frame can be thought of as a tabular representation of data, +with one variable per column, and one data point per row. Each column +is an R vector, which implies one type for all elements +in one given column, and which allows for possibly different types across +different columns. + + +In :mod:`rpy2.robjects`, +:class:`RDataFrame` represents the `R` class `data.frame`. + +Creating an :class:`RDataFrame` can be done by: + +* Using the constructor for the class + +* Create the data.frame through R + +The constructor for :class:`RDataFrame` accepts either a +:class:`rinterface.SexpVector` +(with :attr:`typeof` equal to *VECSXP*, that is an R `list`) or an instance of class :class:`rpy2.rlike.container.TaggedList`. >>> robjects.RDataFrame() + +Creating the data.frame in R can be achieved in numerous ways, +as many R functions do return a data.frame. +In this example, will use the R function `data.frame()`, that +constructs a data.frame from named arguments + +>>> import array +>>> d = {'value': robjects.IntSexpVector((1,2,3)), + 'letter': robjects.StrSexpVector(('x', 'y', 'z'))} +>>> dataf = robjects.r['data.frame'](**d) +>>> dataf.colnames() +c("letter", "value") + +.. note:: + The order of the columns `value` and `letter` cannot be conserved, + since we are using a Python dictionnary. This difference between + R and Python can be resolved by using TaggedList instances + (XXX add material about that). + .. autoclass:: rpy2.robjects.RDataFrame :show-inheritance: :members: Modified: branches/rpy_nextgen/rpy/robjects/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/__init__.py 2008-10-20 18:37:54 UTC (rev 661) +++ branches/rpy_nextgen/rpy/robjects/__init__.py 2008-10-25 17:26:39 UTC (rev 662) @@ -330,7 +330,7 @@ def __init__(self, tlist): """ Create a new data frame. - :param tlist: TaggedList + :param tlist: rpy2.rlike.container.TaggedList or rpy2.rinterface.SexpVector (and of class 'data.frame' for R) """ if isinstance(tlist, rlc.TaggedList): df = baseNameSpaceEnv["data.frame"].rcall(tlist.items()) @@ -338,6 +338,8 @@ elif isinstance(tlist, rinterface.SexpVector): if tlist.typeof != rinterface.VECSXP: raise ValueError("tlist should of typeof VECSXP") + if not r['inherits'](tlist, 'data.frame')[0]: + raise ValueError('tlist should of R class "data.frame"') super(RDataFrame, self).__init__(tlist) else: raise ValueError("tlist can be either"+ Modified: branches/rpy_nextgen/rpy/robjects/tests/testRDataFrame.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/tests/testRDataFrame.py 2008-10-20 18:37:54 UTC (rev 661) +++ branches/rpy_nextgen/rpy/robjects/tests/testRDataFrame.py 2008-10-25 17:26:39 UTC (rev 662) @@ -15,6 +15,17 @@ self.assertEquals("data.frame", df.rclass()[0]) + def testNewFromRObject(self): + numbers = robjects.r('1:5') + self.assertRaises(ValueError, robjects.RDataFrame, numbers) + + rlist = robjects.r('list(a=1, b=2, c=3)') + self.assertRaises(ValueError, robjects.RDataFrame, rlist) + + rdataf = robjects.r('data.frame(a=1:2, b=c("a", "b"))') + dataf = robjects.RDataFrame(rdataf) + + def testDim(self): letters = robjects.r.letters numbers = robjects.r('1:26') 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