On 7/21/10 9:39 PM, rey sebastien wrote: > On 21/07/2010 18:27, Laurent wrote: >> On 21/07/10 16:54, rey sebastien wrote: >>> Hello guys, >>> I'm having some trouble to understand the mapping with rpy2 object and >>> python object. >>> >>> I have a function(x) which return a tuple object in python, and i want >>> to map this tuple object with R object list or vector. >>> _ >>> First, i'm trying to do this : _ >>> >>> robjects.r.tlist = get_max_ticks(x) # return a tuple into this object >>> tlist >>> >>> #Convert list into dataframe >>> r('x <- as.data.frame(tlist,row.names=c("seed","ticks"))') >>> >>> FAIL with error : >>> rinterface.RRuntimeError: Error in eval(expr, envir, enclos) : object >>> 'tlist' not found >>> _ >>> So i'm trying an other strategy : _ >>> >>> robjects.r["tlist"] = get_max_ticks(x) >>> r('x <- as.data.frame(tlist,row.names=c("seed","ticks"))') >>> >>> >>> FAIL with this error : >>> TypeError: 'R' object does not support item assignment >>> >>> Could you help me to understand ? >> >> >> What about starting with the documentation ? >> >> http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects.html#creating-objects >> >> >> >> > Thx but i have already read a part of the official documentation, but it > seems this paragraph don't resolve my problem :/
You may still want to consider spending a little more time on the introduction, as you might continue struggling much otherwise. > A guys on stackoverflow resolve my problem, and explain this :* The code below only works on an older version of rpy2. I'd go for the most recent version as much I could. > import rpy2.robjects as ro > r=ro.r > > def get_max_ticks(): > return (1,2) > ro.globalEnv['tlist'] = ro.FloatVector(get_max_ticks()) > r('x <- as.data.frame(tlist,row.names=c("seed","ticks"))') > print(r['x']) > # tlist > # seed 1 > # ticks 2 > > > It may be possible to access symbols in the R namespace with this type Explained in the "introduction": http://rpy.sourceforge.net/rpy2/doc-2.1/html/introduction.html#getting-r-objects > of notation: robjects.r.tlist, but you can not assign values this way. > The way to assign symbol is to use robject.globalEnv. The link given above has link to the URL below: http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects.html#robjects-environments This tells about globalenv. It also tells that assigning to globalenv should be considered with care. > Moreover, some symbols in R may contain a period, such as data.frame. > You can not access such symbols in Python using notation similar to > robjects.r.data.frame, since Python interprets the period differently > than R. The last link given above is pointing to the use of packages, where a dot-to-underscore translation is performed: http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects.html#robjects-packages The section on functions is also talking about dots and underscores: http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects.html?highlight=dataframe#functions > So I'd suggest avoiding this notation entirely, and instead use > robjects.r['data.frame'], since this notation works no matter what the > symbol name is.* I'd suggest investing time in learning about the class rpy2.robjects.DataFrame(), really. > the link for answer : > http://stackoverflow.com/questions/3300671/mapping-python-tuple-and-r-list-with-rpy2 > > Thx a lot! > S.Rey > >> >> >> >> >>> Thanks a lot !! >>> >>> ------ >>> S.Rey >>> Phd @ "geographie-cités" Paris >>> >>> >>> >>> ------------------------------------------------------------------------------ >>> >>> This SF.net email is sponsored by Sprint >>> What will you do first with EVO, the first 4G phone? >>> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first >>> >>> >>> >>> _______________________________________________ >>> rpy-list mailing list >>> rpy-list@lists.sourceforge.net >>> https://lists.sourceforge.net/lists/listinfo/rpy-list >> > ------------------------------------------------------------------------------ This SF.net email is sponsored by Sprint What will you do first with EVO, the first 4G phone? Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list