[R] list of funtions

Jonathan Phillips 994phij at gmail.com
Thu Sep 13 19:01:05 CEST 2012


Hi,
I have a function called fitMicroProtein which takes a value called
form, this can be any integer from 0-38.
In a larger function I'm making (it's called Newton), the first thing
I want to do is construct a list of functions where form is already
set.  So in pseudocode

fs[[1]](...) <- fitMicroProtein(form=0,...)
fs[[2]](...) <- fitMicroProtein(form=1,...)
.
.
.

I've tried that and it doesn't work.  Here's my code:

Newton <- function(metaf,par,niter,dealwith_NA,...)
{
        fs <- list()
        for(i in 0:(length(par)-1))
        {
                fs[[i+1]] <- function(par) return(metaf(par,form=i,...))
                }
.
.
.


and the problem is with the variable 'i'.
If I use the debugger, I find that it is specifically that:

When it makes f[[1]] we have
   f[[1]] == function(par) return(metaf(par,form=0,...)
but the next thing it does is increment 'i', so f[[1]] becomes
   function(par) return(metaf(par,form=1,...)
where I want f[[1]] to stay as
   function(par) return(metaf(par,form=0,...)

Does anybody know how to stop the value of f[[1]] being dependant on
the current value of 'i'?



More information about the R-help mailing list