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

Matt Asher status at quo.org
Fri Feb 26 17:57:11 CET 2010


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.

Cheers.



On 26.02.2010 16:33, Matt Asher wrote:
> Hi folks,
>
> I am having trouble accessing sub-functions when the main function is
> stored in an array. For example, the following test code works fine:
>
> fcns = c(abs, sqrt)
> fcns[[1]](-2)
> fcns[[2]](2)
>
> However, when I try to access sub-functions declared within list() in a
> function, this only works directly. When I try to access these within an
> array only the first declared sub-function is run. For example I have
> the function:
>
> agent <- function(id) {
>
> # MANY VARIABLES DECLARED
>
> list( set_id = function(newid) {
> id <<- newid
> },
> get_id = function(newid) {
> return(id)
> },
>
> # LOTS MORE SUB FUNCTIONS
> )
> }
>
> If I create a variable to hold this function, I can then access all the
> subfunctions without problem Example:
>
> myAgent = agent(1)
> myAgent$get_id() # Works fine
>
> However, once this function is stored in a vector, I can no longer
> access the subfunctions.
>
> agents = c(agent(1), agent(2))


agents is still a list (or in other words a vector of mode "list"), but
since you c()'ed, it has one hierarchy level less than you expect.

In order to make your code below work, you rather need:

agents <- list(agent(1), agent(2))

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 <<- ) ...


> agents[[1]] # This shows the set_id function only, unnamed
>
> agents[[1]]$get_id() # Leads to error below:
>
> Error in agents[[1]]$get_id : object of type 'closure' is not subsettable
>
> How can I access these sub methods within the vector?
>
> I am using R version 2.8.1


... and upgrade to some recent version of R.


Uwe Ligges


> TIA for the help!
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list