On 05/10/2011 10:57 AM, honeyoak wrote:
it is possible to dynamically create functions in R using lists? what I want
to do is something like this:

       a = list()
       for (i in 1:10) a[[i]] = function(seed = i) runif(seed)

so that when I call a[i] I get random draws 1,2,....i unfortunately R only
uses the last i .


That is because you never evaluate it until you call the function. You can do what you want in several ways; one is

for (i in 1:10)
  a[[i]] <- local( { default <- i; function(seed = default) runif(seed) } )

Duncan Murdoch
  I would also like to know if there is a run-all function
without explicitly looping or using lapply. for example if I have a list 'b'
of functions if I called

       run-all(b)

all the functions in list 'b' would be run

thanks.

--
View this message in context: 
http://r.789695.n4.nabble.com/dynamically-creating-functions-in-r-tp3874767p3874767.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to