Revision: 626
          http://rpy.svn.sourceforge.net/rpy/?rev=626&view=rev
Author:   lgautier
Date:     2008-08-11 21:35:10 +0000 (Mon, 11 Aug 2008)

Log Message:
-----------
- fixed 'assign' to handle rlike.TaggedList as a parameter (solving the issue
of both having the value parameter for R set functions "<-" last in the call
and allowing for keywords
- added docstrings

Modified Paths:
--------------
    branches/rpy_nextgen/rpy/robjects/__init__.py
    branches/rpy_nextgen/rpy/robjects/tests/testRVector.py

Modified: branches/rpy_nextgen/rpy/robjects/__init__.py
===================================================================
--- branches/rpy_nextgen/rpy/robjects/__init__.py       2008-08-09 18:50:36 UTC 
(rev 625)
+++ branches/rpy_nextgen/rpy/robjects/__init__.py       2008-08-11 21:35:10 UTC 
(rev 626)
@@ -9,6 +9,7 @@
 import os, sys
 import array
 import rpy2.rinterface as rinterface
+import rpy2.rlike.container as rlc
 
 StrSexpVector = rinterface.StrSexpVector
 IntSexpVector = rinterface.IntSexpVector
@@ -89,7 +90,7 @@
     elif isinstance(o, list):
         res = r.list(*[ri2py(py2ri(x)) for x in o])
     else:
-        raise(ValueError("Nothing can be done for this type at the moment."))
+        raise(ValueError("Nothing can be done for the type %s at the moment." 
%(type(o))))
     return res
 
 py2ri = default_py2ri
@@ -211,17 +212,19 @@
 
     def subset(self, *args, **kwargs):
         """ Subset the "R-way.", using R's "[" function. 
-           In a nutshell, R indexing differs from Python's with
+           In a nutshell, R indexing differs from Python's on:
+
            - indexing can be done with integers or strings (that are 'names')
+
            - an index equal to TRUE will mean everything selected (because of 
the recycling rule)
+
            - integer indexing starts at one
+
            - negative integer indexing means exclusion of the given integers
+
            - an index is itself a vector of elements to select
         """
         
-        #for a in args:
-        #    if not isinstance(a, Rvector):
-        #        raise(TypeError("Subset only take R vectors"))
         args = [py2ro(x) for x in args]
         for k, v in kwargs.itervalues():
             args[k] = py2ro(v)
@@ -229,13 +232,18 @@
         res = r["["](*([self, ] + [x for x in args]), **kwargs)
         return res
 
-    def assign(self, *args):
-        #FIXME: value must be the last argument, but this can be
-        # challenging in since python kwargs do not enforce any order
-        #(an ordered dictionary class will therefore be implemented)
-        args = [py2ro(x) for x in args]
-        res = r["[<-"](*([self, ] + [x for x in args]))
-
+    def assign(self, index, value):
+        if not (isinstance(index, rlc.TaggedList) | \
+                    isinstance(index, rlc.ArgsDict)):
+            args = rlc.TaggedList([py2ro(index), ])
+        else:
+            for i in xrange(len(index)):
+                index[i] = py2ro(index[i])
+            args = index
+        args.append(py2ro(value))
+        args.insert(0, self)
+        res = r["[<-"].rcall(args.items())
+        res = ri2py(res)
         return res
 
     def __add__(self, x):
@@ -297,11 +305,13 @@
     """ An R matrix """
 
     def nrow(self):
-        """ Number of rows """
+        """ Number of rows.
+        :rtype: integer """
         return self.dim[0]
 
     def ncol(self):
-        """ Number of columns """
+        """ Number of columns.
+        :rtype: integer """
         return self.dim[1]
 
 class RDataFrame(RVector):
@@ -326,17 +336,25 @@
             raise(TypeError("The object must be representing an R data.frame"))
     
     def nrow(self):
-        """ Number of rows """
+        """ Number of rows. 
+        :rtype: integer """
         return baseNameSpaceEnv["nrow"](self)[0]
 
     def ncol(self):
-        """ Number of columns """
+        """ Number of columns.
+        :rtype: integer """
         return baseNameSpaceEnv["ncol"](self)[0]
     
     def rownames(self):
+        """ Row names
+        :rype: SexpVector
+        """
         return baseNameSpaceEnv["colnames"](self)[0]
 
     def colnames(self):
+        """ Column names
+        :rype: SexpVector
+        """
         return baseNameSpaceEnv["colnames"](self)[0]
 
 
@@ -374,6 +392,10 @@
         super(REnvironment, self).__setitem__(item, robj)
 
     def get(self, item):
+        """ Get a object from its R name/symol
+        :param item: string (name/symbol)
+        :rtype: object (as returned by :func:`ri2py`)
+        """
         res = super(REnvironment, self).get(item)
         res = ri2py(res)
         return res

Modified: branches/rpy_nextgen/rpy/robjects/tests/testRVector.py
===================================================================
--- branches/rpy_nextgen/rpy/robjects/tests/testRVector.py      2008-08-09 
18:50:36 UTC (rev 625)
+++ branches/rpy_nextgen/rpy/robjects/tests/testRVector.py      2008-08-11 
21:35:10 UTC (rev 626)
@@ -2,6 +2,7 @@
 import rpy2.robjects as robjects
 ri = robjects.rinterface
 import array
+import rpy2.rlike.container as rlc
 
 rlist = robjects.baseNameSpaceEnv["list"]
 
@@ -91,12 +92,21 @@
 
     def testAssign(self):
         vec = robjects.r.seq(1, 10)
-        vec = vec.assign(array.array('i', [1, 3, 5]), 20)
+        i = array.array('i', [1, 3])
+        vec = vec.assign(i, 20)
         self.assertEquals(20, vec[0])
+        self.assertEquals(2, vec[1])
         self.assertEquals(20, vec[2])
-        self.assertEquals(20, vec[4])
+        self.assertEquals(4, vec[3])
 
-    
+        i = array.array('i', [1, 5])
+        vec = vec.assign(rlc.TaggedList([i, ]), 50)
+        self.assertEquals(50, vec[0])
+        self.assertEquals(2, vec[1])
+        self.assertEquals(20, vec[2])
+        self.assertEquals(4, vec[3])
+        self.assertEquals(50, vec[4])
+                         
     def testSubsetRecyclingRule(self):
         # recycling rule
         v = robjects.RVector(array.array('i', range(1, 23)))


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

Reply via email to