[R] Problem accessing sub-methods of functions stored in a vector

Gabor Grothendieck ggrothendieck at gmail.com
Fri Feb 26 18:19:18 CET 2010


On Fri, Feb 26, 2010 at 11:57 AM, Matt Asher <status at quo.org> wrote:
> Hi,
>
> This:
>
> agents <- list(agent(1), agent(2))
>
> worked perfectly. Thanks Uwe!
>
> I'm not fully sure what you mean by this:
>
> "
> Anyway, I hope you know that lexical scoping will yield in the
> environments attached to all those functions they have been generated in
> and you know about possible consequences. If not, you really should not
> be doing this ... (nor using <<- ) ...
> "
>
> I used the <<- assignment because I am treating these variables in the
> sub-function as "belonging" to the parent function (like using "self" or
> "this" in other languages). I am basically treating my agent function like a
> class that can have instances and class vars, without having to use R's
> backwards (IMO) way of doing OOP.
>

In that case you might want to look at the proto package

http://r-proto.googlecode.com

which can do this a bit more cleanly and also allows for delgation
(i.e. the counterpart to inheritance in the prototype model)

> library(proto)
> agent <- proto(id = NA, get_id = function(self) self$id, set_id = function(self, id) self$id <- id)
>
> agent$set_id(2)
> agent$get_id()
[1] 2



More information about the R-help mailing list