[R] list of funtions

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Sep 13 19:06:35 CEST 2012



On 13.09.2012 19:01, Jonathan Phillips wrote:
> 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'?

Er, you know that you have

  function(par) return(metaf(par,form=i,...))

in your loop. If you want to have it independent if i, why do you 
specify it?

Uwe Ligges

>
> ______________________________________________
> 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