Revision: 669 http://rpy.svn.sourceforge.net/rpy/?rev=669&view=rev Author: lgautier Date: 2008-10-30 21:03:28 +0000 (Thu, 30 Oct 2008)
Log Message: ----------- robjects: Formerly-new attribute is now gone. The doc points to rpy_classic if for that kind of things rinterface: Fixed test for NA doc: Bumped target R-version in the doc to R-2.8 Modified Paths: -------------- branches/rpy_nextgen/doc/source/introduction.rst branches/rpy_nextgen/doc/source/overview.rst branches/rpy_nextgen/doc/source/robjects.rst branches/rpy_nextgen/doc/source/rpy_classic.rst branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py branches/rpy_nextgen/rpy/robjects/__init__.py branches/rpy_nextgen/rpy/robjects/tests/testRobjects.py Modified: branches/rpy_nextgen/doc/source/introduction.rst =================================================================== --- branches/rpy_nextgen/doc/source/introduction.rst 2008-10-29 20:39:25 UTC (rev 668) +++ branches/rpy_nextgen/doc/source/introduction.rst 2008-10-30 21:03:28 UTC (rev 669) @@ -208,3 +208,120 @@ More information on functions is in Section :ref:`robjects-functions` + + +Examples +======== + +This section demonstrates some of the features of +rpy2 by the example. The wiki on the sourceforge website +will hopefully be used as a cookbook. + + +.. code-block:: python + + import rpy2.robjects as robjects + import array + + r = robjects.r + + x = array.array('i', range(10)) + y = r.rnorm(10) + + r.X11() + + r.layout(r.matrix(array.array('i', [1,2,3,2]), nrow=2, ncol=2)) + r.plot(r.runif(10), y, xlab="runif", ylab="foo/bar", col="red") + + kwargs = {'ylab':"foo/bar", 'type':"b", 'col':"blue", 'log':"x"} + r.plot(x, y, **kwargs) + +.. note:: + Since the named parameters are a Python :class:`dict`, + the order of the parameters is lost. + + +Linear models +------------- + +The R code is: + +.. code-block:: r + + ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) + trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) + group <- gl(2, 10, 20, labels = c("Ctl","Trt")) + weight <- c(ctl, trt) + + anova(lm.D9 <- lm(weight ~ group)) + + summary(lm.D90 <- lm(weight ~ group - 1))# omitting intercept + +One way to achieve the same with :mod:`rpy2.robjects` is + +.. code-block:: python + + import rpy2.robjects as robjects + + r = robjects.r + + ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14]) + trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69]) + group = r.gl(2, 10, 20, labels = ["Ctl","Trt"]) + weight = ctl + trt + + robjects.globalEnv["weight"] = weight + robjects.globalEnv["group"] = group + lm_D9 = r.lm("weight ~ group") + print(r.anova(lm_D9)) + + lm_D90 = r.lm("weight ~ group - 1") + print(r.summary(lm_D90)) + + + +Principal component analysis +---------------------------- + +The R code is + +.. code-block:: r + + m <- matrix(rnorm(100), ncol=5) + pca <- princomp(m) + plot(pca, main="Eigen values") + biplot(pca, main="biplot") + +The :mod:`rpy2.robjects` code is + +.. testcode:: + + import rpy2.robjects as robjects + + r = robjects.r + + m = r.matrix(r.rnorm(100), ncol=5) + pca = r.princomp(m) + r.plot(pca, main="Eigen values") + r.biplot(pca, main="biplot") + + + +S4 classes +---------- + +.. code-block:: python + + import rpy2.robjects as robjects + import array + + r = robjects.r + + r.setClass("Track", + r.representation(x="numeric", y="numeric")) + + a = r.new("Track", x=0, y=1) + + a.x + + Modified: branches/rpy_nextgen/doc/source/overview.rst =================================================================== --- branches/rpy_nextgen/doc/source/overview.rst 2008-10-29 20:39:25 UTC (rev 668) +++ branches/rpy_nextgen/doc/source/overview.rst 2008-10-30 21:03:28 UTC (rev 669) @@ -41,14 +41,12 @@ Python version 2.4 or higher, and R-2.7.0 or higher are required. -Although the development was done with R-2.7.2 and Python-2.5.2, -it has also been tested with: +Although the development was first done with R-2.7.2 (now with R-2.8.0) +and Python-2.5.2, it has also been tested with: - * R-2.8.0 - * Python-2.6.0 (numpy-support not tested) -Gcc-4.2.3 was used for compiling the C parts. +Gcc-4.2.3, then gcc-4.2.4 were used for compiling the C parts. Download Modified: branches/rpy_nextgen/doc/source/robjects.rst =================================================================== --- branches/rpy_nextgen/doc/source/robjects.rst 2008-10-29 20:39:25 UTC (rev 668) +++ branches/rpy_nextgen/doc/source/robjects.rst 2008-10-30 21:03:28 UTC (rev 669) @@ -65,18 +65,19 @@ This approach has limitation as: - * The actual Python attributes for the object masks the R elements +* The actual Python attributes for the object masks the R elements - * '.' (dot) is syntactically valid in names for R objects, but not for +* '.' (dot) is syntactically valid in names for R objects, but not for python objects. -That last limitation can partly be removed by setting the attribute -:attr:`_dotter` from False to True. +That last limitation can partly be removed by using :mod:`rpy.rpy_classic` if +this feature matters most to you. >>> robjects.r.as_null # AttributeError raised ->>> robjects.r._dotter = True ->>> robjects.r.as_null +>>> import rpy2.rpy_classic as rpy +>>> rpy.set_default_mode(NO_CONVERSION) +>>> rpy.r.as_null # R function as.null() returned .. warning:: @@ -92,11 +93,10 @@ 2. Check if the attribute is can be accessed in R, starting from `globalEnv` - 3. If :attr:`_dotter` is True, turn all `_` into `.` and repeat the step above +When safety matters most, or when getting extraordinary funds for a bailout +is unlikely, we recommed using :meth:`__getitem__` to get +a given R object (and store it in a python variable if wanted): -When safety matters most, we recommed using :meth:`__getitem__` to get -and R object (and store it in a python variable if wanted): - >>> as_null = robjects.r['as.null'] @@ -124,7 +124,10 @@ >>> x = robjects.r.rnorm(100) >>> robjects.r('hist(%s, xlab="x", main="hist(x)")' %repr(x)) +.. warning:: + Doing this with large objects might not be the best use of + your computing power. .. index:: pair: robjects;RObject @@ -571,119 +574,3 @@ .. autofunction:: rpy2.robjects.default_py2ro -Examples -======== - -This section demonstrates some of the features of -rpy2 by the example. The wiki on the sourceforge website -will hopefully be used as a cookbook. - - -.. code-block:: python - - import rpy2.robjects as robjects - import array - - r = robjects.r - - x = array.array('i', range(10)) - y = r.rnorm(10) - - r.X11() - - r.layout(r.matrix(array.array('i', [1,2,3,2]), nrow=2, ncol=2)) - r.plot(r.runif(10), y, xlab="runif", ylab="foo/bar", col="red") - - kwargs = {'ylab':"foo/bar", 'type':"b", 'col':"blue", 'log':"x"} - r.plot(x, y, **kwargs) - -.. note:: - Since the named parameters are a Python :class:`dict`, - the order of the parameters is lost. - Check :meth:`rpy2.rinterface.SexpClosure.rcall` - to know how to keep the order of parameters. - -Linear models -------------- - -The R code is: - -.. code-block:: r - - ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) - trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) - group <- gl(2, 10, 20, labels = c("Ctl","Trt")) - weight <- c(ctl, trt) - - anova(lm.D9 <- lm(weight ~ group)) - - summary(lm.D90 <- lm(weight ~ group - 1))# omitting intercept - -One way to achieve the same with :mod:`rpy2.robjects` is - -.. code-block:: python - - import rpy2.robjects as robjects - - r = robjects.r - - ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14]) - trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69]) - group = r.gl(2, 10, 20, labels = ["Ctl","Trt"]) - weight = ctl + trt - - robjects.globalEnv["weight"] = weight - robjects.globalEnv["group"] = group - lm_D9 = r.lm("weight ~ group") - print(r.anova(lm_D9)) - - lm_D90 = r.lm("weight ~ group - 1") - print(r.summary(lm_D90)) - - - -Principal component analysis ----------------------------- - -The R code is - -.. code-block:: r - - m <- matrix(rnorm(100), ncol=5) - pca <- princomp(m) - plot(pca, main="Eigen values") - biplot(pca, main="biplot") - -The :mod:`rpy2.robjects` code is - -.. testcode:: - - import rpy2.robjects as robjects - - r = robjects.r - - m = r.matrix(r.rnorm(100), ncol=5) - pca = r.princomp(m) - r.plot(pca, main="Eigen values") - r.biplot(pca, main="biplot") - - - -S4 classes ----------- - -.. code-block:: python - - import rpy2.robjects as robjects - import array - - r = robjects.r - - r.setClass("Track", - r.representation(x="numeric", y="numeric")) - - a = r.new("Track", x=0, y=1) - - a.x - - Modified: branches/rpy_nextgen/doc/source/rpy_classic.rst =================================================================== --- branches/rpy_nextgen/doc/source/rpy_classic.rst 2008-10-29 20:39:25 UTC (rev 668) +++ branches/rpy_nextgen/doc/source/rpy_classic.rst 2008-10-30 21:03:28 UTC (rev 669) @@ -14,7 +14,7 @@ To match examples and documentation for *rpy*, we load the module as: ->>> from rpy2.rpy_classic import * +>>> import rpy2.rpy_classic as rpy .. index:: single: conversion @@ -28,36 +28,36 @@ existing in *rpy* is provided, and the default mode can be set with :func:`set_default_mode`: ->>> set_default_mode(NO_CONVERSION) ->>> set_default_mode(BASIC_CONVERSION) +>>> rpy.set_default_mode(rpy.NO_CONVERSION) +>>> rpy.set_default_mode(rpy.BASIC_CONVERSION) R instance ========== The ``r`` instance of class :class:`R` behaves like before: ->>> r.help +>>> rpy.r.help 'dots' in the R name are translated to underscores: ->>> r.wilcox_test +>>> rpy.r.wilcox_test ->>> r.wilcox_test([1,2,3], [4,5,6]) +>>> rpy.r.wilcox_test([1,2,3], [4,5,6]) ->>> x = r.seq(1, 3, by=0.5) ->>> r.plot(x) +>>> x = rpy.r.seq(1, 3, by=0.5) +>>> rpy.r.plot(x) An example:: degrees = 4 - grid = r.seq(0, 10, length=100) - values = [r.dchisq(x, degrees) for x in grid] - r.par(ann=0) - r.plot(grid, values, type='l') + grid = rpy.r.seq(0, 10, length=100) + values = [rpy.r.dchisq(x, degrees) for x in grid] + rpy.r.par(ann=0) + rpy.r.plot(grid, values, type='l') - r.library('splines') + rpy.r.library('splines') - type(r.seq) + type(rpy.r.seq) .. index:: pair: rpy_classic;function @@ -67,29 +67,29 @@ As in RPy-1.x, all R objects are callable: ->>> callable(r.seq) +>>> callable(rpy.r.seq) True ->>> callable(r.pi) +>>> callable(rpy.r.pi) True >>> If an object is not a R function, a :class:`RuntimeError` is thrown by R whenever called: ->>> r.pi() +>>> rpy.r.pi() The function are called like regular Python functions: ->>> r.seq(1, 3) ->>> r.seq(1, 3, by=0.5) ->>> r['options'](show_coef_Pvalues=0) +>>> rpy.r.seq(1, 3) +>>> rpy.r.seq(1, 3, by=0.5) +>>> rpy.r['options'](show_coef_Pvalues=0) >>> ->>> m = r.matrix(r.rnorm(100), 20, 5) ->>> pca = r.princomp(m) ->>> r.plot(pca, main = "PCA") +>>> m = rpy.r.matrix(r.rnorm(100), 20, 5) +>>> pca = rpy.r.princomp(m) +>>> rpy.r.plot(pca, main = "PCA") >>> Modified: branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py =================================================================== --- branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py 2008-10-29 20:39:25 UTC (rev 668) +++ branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py 2008-10-30 21:03:28 UTC (rev 669) @@ -98,7 +98,7 @@ ok = isCharacter(sexp)[0] self.assertTrue(ok) - ri.NA_STRING[0] + self.assertRaises(ValueError, ri.NA_STRING.__getitem__, 0) def testNewUnicode(self): sexp = ri.SexpVector([u'abc', ], ri.STRSXP) Modified: branches/rpy_nextgen/rpy/robjects/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/__init__.py 2008-10-29 20:39:25 UTC (rev 668) +++ branches/rpy_nextgen/rpy/robjects/__init__.py 2008-10-30 21:03:28 UTC (rev 669) @@ -473,7 +473,6 @@ class R(object): _instance = None - _dotter = False def __init__(self): if R._instance is None: @@ -492,17 +491,8 @@ try: return self[attr] except LookupError, le: - pass + raise orig_ae - if self._dotter: - attr = attr.replace('_', '.') - try: - return self[attr] - except LookupError, le: - pass - - raise orig_ae - def __getitem__(self, item): res = rinterface.globalEnv.get(item) Modified: branches/rpy_nextgen/rpy/robjects/tests/testRobjects.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/tests/testRobjects.py 2008-10-29 20:39:25 UTC (rev 668) +++ branches/rpy_nextgen/rpy/robjects/tests/testRobjects.py 2008-10-30 21:03:28 UTC (rev 669) @@ -27,13 +27,6 @@ for i, li in enumerate(myList): self.assertEquals(i, myList[i][0]) - - def testGetAttr(self): - robjects.r._dotter = True - self.assertRaises(LookupError, robjects.r.__getitem__, 'as_null') - res = robjects.r.as_null - self.assertTrue(isinstance(res, rinterface.SexpClosure)) - def testEval(self): # vector long enough to span across more than one line x = robjects.baseNameSpaceEnv['seq'](1, 50, 2) 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