Revision: 663 http://rpy.svn.sourceforge.net/rpy/?rev=663&view=rev Author: lgautier Date: 2008-10-26 14:09:49 +0000 (Sun, 26 Oct 2008)
Log Message: ----------- doc: - Added short introduction rpy2: - added __version__ rpy2.rinterface: - Added class BoolSexpVector rpy2.robjects: - Added classes BoolVector, IntVector, FloatVector, StrVector Modified Paths: -------------- branches/rpy_nextgen/NEWS branches/rpy_nextgen/doc/source/index.rst branches/rpy_nextgen/doc/source/robjects.rst branches/rpy_nextgen/rpy/__init__.py branches/rpy_nextgen/rpy/rinterface/__init__.py branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py branches/rpy_nextgen/rpy/robjects/__init__.py branches/rpy_nextgen/rpy/robjects/tests/testRVector.py Added Paths: ----------- branches/rpy_nextgen/doc/source/introduction.rst Modified: branches/rpy_nextgen/NEWS =================================================================== --- branches/rpy_nextgen/NEWS 2008-10-25 17:26:39 UTC (rev 662) +++ branches/rpy_nextgen/NEWS 2008-10-26 14:09:49 UTC (rev 663) @@ -8,6 +8,27 @@ - Constructor for :class:`rpy2.robjects.RDataFrame` checks that R lists are data.frames (not all lists are data.frame) +New features +------------ + +- added :data:`__version__` to rpy2/__init__.py + +:mod:`rpy2.robjects`: + +- added classes :class:`StrVector`, :class:`IntVector`, :class:`FloatVector`, :class:`BoolVector` + +:mod:`rpy2.rinterface`: + +- added missing class :class:`BoolSexpVector`. + + +Changes +------- + +:mod:`rpy2.robjects`: + +- does not alias :class:`rinterface.StrSexpVector`, :class:`rinterface.IntSexpVector`, :class:`rinterface.FloatSexpVector` anymore + Bugs fixed ---------- @@ -61,7 +82,7 @@ - setup.py: link to external lapack or blas library when relevant -- added a MANIFEST.in ensuring that headers get included in the source distribution +- added a MANIFEST.in ensuring that headers get included in the source distribution #missing headers reported by Nicholas Lewin-Koh - :func:`rinterface.str_typeint` was causing segfault when called with 99 Modified: branches/rpy_nextgen/doc/source/index.rst =================================================================== --- branches/rpy_nextgen/doc/source/index.rst 2008-10-25 17:26:39 UTC (rev 662) +++ branches/rpy_nextgen/doc/source/index.rst 2008-10-26 14:09:49 UTC (rev 663) @@ -6,6 +6,7 @@ :maxdepth: 2 overview + introduction robjects rinterface rpy_classic Added: branches/rpy_nextgen/doc/source/introduction.rst =================================================================== --- branches/rpy_nextgen/doc/source/introduction.rst (rev 0) +++ branches/rpy_nextgen/doc/source/introduction.rst 2008-10-26 14:09:49 UTC (rev 663) @@ -0,0 +1,118 @@ +******************** +Introduction to rpy2 +******************** + + +This introduction aims at being a gentle start to rpy2, +either when coming from R to Python/rpy2, from Python to rpy2/R, +or from elsewhere to Python/rpy2/R. + + +Getting started +=============== + +It is assumed here that the rpy2 package was properly installed. +In python, making a package or module available is achieved by +importing it. + +.. code-block:: python + + import rpy2.robjects as robjects + + +The `r` instance +================ + +The object :data:`r` in :mod:`rpy2.robjects` represents the running embedded +`R` process. + +If familiar with R and the R console, :data:`r` is a little like your window +to R from Python. + + +The method :meth:`__getitem__` functions like calling a variable from the +R console. + +Example in R: + +.. code-block:: r + + pi + +With :mod:`rpy2`: + +>>> robjects.r['pi'] +3.14159265358979 + + + +The :data:`r` object is also callable, and the string passed to it evaluated +as `R` code: + +Example in R: + +.. code-block:: r + + pi + +With :mod:`rpy2`: + +>>> robjects.r('pi') +3.14159265358979 + + +The evaluation is performed in what is know to R users as the +`Global Environment`, that is the place one starts at when starting +the R console. Whenever the `R` code creates variables, those +variables will be "located" in that `Global Environment` by default. + + +Example: + +.. code-block:: r + + robjects.r(''' + f <- function(r) { 2 * pi * r } + f(3) + ''') + + +The expression above will return the value 18.85, but also create an R function +`f`. That function `f` is present in the R `Global Environement`, and can +be accessed with the `__getitem__` mechanism outlined above: + + +>>> robjects.r['f'] +function (r) +{ + 2 * pi * r +} + + +R vectors +========= + +In `R`, data are mostly represented by vectors, even when looking +like scalars. + +When looking closely at the R object `pi` used previously, +we can observe that this is in fact a vector of length 1. + +>>> len(robjects.r['pi']) +1 + + +Creating R vectors can be achieved simply: + +>>> robjects.StrVector(['abc', 'def']) +c("abc", "def") +>>> robjects.IntVector([1, 2, 3]) +1:3 +>>> robjects.FloatVector([1.1, 2.2, 3.3]) +c(1.1, 2.2, 3.3) + + +R matrixes and arrays are just vector with a `dim` attribute. + + + \ No newline at end of file Modified: branches/rpy_nextgen/doc/source/robjects.rst =================================================================== --- branches/rpy_nextgen/doc/source/robjects.rst 2008-10-25 17:26:39 UTC (rev 662) +++ branches/rpy_nextgen/doc/source/robjects.rst 2008-10-26 14:09:49 UTC (rev 663) @@ -159,6 +159,20 @@ :class:`rpy2.rinterface.VectorSexp`. +Creating vectors +---------------- + +Creating vectors can be achieved either from R or from Python. + +When the vectors are created from R, one should not worry much +as they will be exposed as they should by :mod:`rpy2.robjects`. + +When one wants to create a vector from Python, either the +class :class:`RVector` or the convenience classes +:class:`IntVector`, :class:`FloatVector`, :class:`BoolVector`, :class:`StrVector` can +used. + + .. index:: pair: RVector;indexing Modified: branches/rpy_nextgen/rpy/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/__init__.py 2008-10-25 17:26:39 UTC (rev 662) +++ branches/rpy_nextgen/rpy/__init__.py 2008-10-26 14:09:49 UTC (rev 663) @@ -0,0 +1 @@ +__version__ = '2.0.0b1' Modified: branches/rpy_nextgen/rpy/rinterface/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/rinterface/__init__.py 2008-10-25 17:26:39 UTC (rev 662) +++ branches/rpy_nextgen/rpy/rinterface/__init__.py 2008-10-26 14:09:49 UTC (rev 663) @@ -58,7 +58,14 @@ def __init__(self, v): super(FloatSexpVector, self).__init__(v, REALSXP) +class BoolSexpVector(SexpVector): + """ + Vector of floats. + """ + def __init__(self, v): + super(BoolSexpVector, self).__init__(v, LGLSXP) + # wrapper because print is strangely not a function # Python prior to version 3.0 def consolePrint(x): Modified: branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py =================================================================== --- branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py 2008-10-25 17:26:39 UTC (rev 662) +++ branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py 2008-10-26 14:09:49 UTC (rev 663) @@ -27,6 +27,12 @@ ok = isStr(sexp)[0] self.assertTrue(ok) + def testBool(self): + sexp = ri.BoolSexpVector([True, ]) + isBool = ri.globalEnv.get("is.logical") + ok = isBool(sexp)[0] + self.assertTrue(ok) + class SexpVectorTestCase(unittest.TestCase): def testMissinfType(self): Modified: branches/rpy_nextgen/rpy/robjects/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/__init__.py 2008-10-25 17:26:39 UTC (rev 662) +++ branches/rpy_nextgen/rpy/robjects/__init__.py 2008-10-26 14:09:49 UTC (rev 663) @@ -12,11 +12,6 @@ import rpy2.rinterface as rinterface import rpy2.rlike.container as rlc -StrSexpVector = rinterface.StrSexpVector -IntSexpVector = rinterface.IntSexpVector -FloatSexpVector = rinterface.FloatSexpVector - - #FIXME: close everything when leaving (check RPy for that). def default_ri2py(o): @@ -278,6 +273,27 @@ res = r["names<-"](self, value) return res +class StrVector(RVector): + def __init__(self, obj): + obj = rinterface.StrSexpVector(obj) + super(StrVector, self).__init__(obj) + +class IntVector(RVector): + def __init__(self, obj): + obj = rinterface.IntSexpVector(obj) + super(IntVector, self).__init__(obj) + +class BoolVector(RVector): + def __init__(self, obj): + obj = rinterface.BoolSexpVector(obj) + super(BoolVector, self).__init__(obj) + +class FloatVector(RVector): + def __init__(self, obj): + obj = rinterface.FloatSexpVector(obj) + super(FloatVector, self).__init__(obj) + + class RArray(RVector): """ An R array """ def __init__(self, o): @@ -428,9 +444,9 @@ def __init__(self, formula, environment = rinterface.globalEnv): inpackage = rinterface.baseNameSpaceEnv["::"] - asformula = inpackage(StrSexpVector(['stats', ]), - StrSexpVector(['as.formula', ])) - formula = rinterface.SexpVector(StrSexpVector([formula, ])) + asformula = inpackage(rinterface.StrSexpVector(['stats', ]), + rinterface.StrSexpVector(['as.formula', ])) + formula = rinterface.SexpVector(rinterface.StrSexpVector([formula, ])) robj = asformula(formula, env = environment) super(RFormula, self).__init__(robj) Modified: branches/rpy_nextgen/rpy/robjects/tests/testRVector.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/tests/testRVector.py 2008-10-25 17:26:39 UTC (rev 662) +++ branches/rpy_nextgen/rpy/robjects/tests/testRVector.py 2008-10-26 14:09:49 UTC (rev 663) @@ -22,6 +22,30 @@ del(ri_v) self.assertEquals(ri.INTSXP, ro_v.typeof) + def testNewStrVector(self): + vec = robjects.StrVector(['abc', 'def']) + self.assertEquals('abc', vec[0]) + self.assertEquals('def', vec[1]) + self.assertEquals(2, len(vec)) + + def testNewIntVector(self): + vec = robjects.IntVector([123, 456]) + self.assertEquals(123, vec[0]) + self.assertEquals(456, vec[1]) + self.assertEquals(2, len(vec)) + + def testNewFloatVector(self): + vec = robjects.FloatVector([123.0, 456.0]) + self.assertEquals(123.0, vec[0]) + self.assertEquals(456.0, vec[1]) + self.assertEquals(2, len(vec)) + + def testNewBoolVector(self): + vec = robjects.BoolVector([True, False]) + self.assertEquals(True, vec[0]) + self.assertEquals(False, vec[1]) + self.assertEquals(2, len(vec)) + def testAddOperators(self): seq_R = robjects.r["seq"] mySeqA = seq_R(0, 3) 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