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
>