Re: [Rd] problem subsetting of a reference class

From: John Chambers <jmc_at_r-project.org>
Date: Thu, 21 Apr 2011 13:36:00 -0700

You're confusing functional and OOP-style methods.

Since you define an OOP-style method, you need to invoke it in OOP style.

With your example:

 > tmp$`[`("random")
  [1] -1.439131143 -0.630354726 0.822006263 -0.651707539 0.475332681   [6] 0.002680224 1.539035675 -0.117609566 2.066227300 1.111270997  >

You could if you wanted define a functional method via setMethod() to allow functional access, by invoking the $`[`() method--preferably after changing its name. It's probably a matter of opinion whether that's a good use of OOP-style methods.

On 4/21/11 12:24 PM, A Zege wrote:
> I am trying to define subset operator for a reference class and hitting some
> problem i am unable to diagnose.To give an example, here is a toy class
> generator that is a wrapper around a list
>
>
>
>
> tmpGEN<-setRefClass("TMP", fields=list(
> namelist="list"
> ))
> tmpGEN$methods('add'=function(obj, name){
> namelist[[name]]<<-obj
> })
>
> tmpGEN$methods('['=function(name){
> if(class(name)!="character")
> stop('to return cache element need to pass its name')
> ind<-match(name, names(namelist))
> if(is.na(ind))
> stop('data to remove is not in namelist')
> namelist[[name]]
> })
>
>
> ==============
>
> when i try to use it, the following happens
> v<-rnorm(10)
> tmp<-tmpGEN$new()
> tmp$add(v, 'random')
>
> ..... up until here everything is ok, class is generated and vector is
> added. Now when i do
> tmp['random']
>
> i get error message
>
> Error in tmp["random"] : object of type 'S4' is not subsettable
>
> Not sure if it means that i cannot define "[" operator for a class or if i
> am doing it syntactically wrong
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/problem-subsetting-of-a-reference-class-tp3466690p3466690.html
> Sent from the R devel mailing list archive at Nabble.com.
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 21 Apr 2011 - 20:38:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 22 Apr 2011 - 18:30:50 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive