Skip to content

dynamically creating functions in r

4 messages · honeyoak, Sarah Goslee, Duncan Murdoch +1 more

#
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 . 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.
#
Hi,
On Wed, Oct 5, 2011 at 10:57 AM, honeyoak <honeyoak at gmail.com> wrote:
I'm not sure I understand what you want. Do you want to set a new seed for
the random number generator, or do you want a random vector of length i
each time?

If the former, I'm not sure why you'd want to do that, but your choice
of variable
names makes me wonder.

If the latter, you just need a bit of clean-up.

     a <- list()
     for (i in 1:10) {
     	a[[i]] <- runif(i)
     }

But that's not dynamically creating a function, so maybe I'm missing the point.
What's wrong with lapply? I think we need to know more about what you're
trying to do. You might also want to look at do.call().

Sarah
#
On 05/10/2011 10:57 AM, honeyoak wrote:
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
#
Creating expressions and functions dynamically can be
tricky.  Usually I use functions like call(), substitute(),
and formals(); very occasionally I use parse(text=).

Here is one way to make a family of functions that differ
only in the default value their their argument:
  > funsA <- lapply(1:3, function(i){
        retval <- function(arg=i)arg^2
        formals(retval)$arg <- i
        retval
    })
  > sapply(funsA, function(f)f())
  [1] 1 4 9
  > funsA[[2]]
  function (arg = 2L)
  arg^2
  <environment: 0x2985d18>

Here is a way to make the functions differ in their bodies:
  > funsB <- lapply(c("sin", "cos", "sqrt"),
       function(fname) eval(substitute(function(x)f(x)^2, list(f=as.name(fname)))))
  > sapply(funsB, function(f)f(pi/3))
  [1] 0.750000 0.250000 1.047198
  > funsB[[2]]
  function (x)
  cos(x)^2
  <environment: 0x2412b20>

You can also add things to environment(yourFunction), where
you arrange that each function has its own personal environment,
instead of altering the function itself.  This works, but can look
a bit mysterious to the na?ve user who doesn't know to look
in the environment of the function:

  > funsC <- lapply(1:3, function(i){
      retval <- function(arg=i)arg^2
      with(environment(retval), i <- i)
      retval
    })
  > sapply(funsC, function(f)f())
  [1] 1 4 9
  > funsC[[2]]
  function (arg = i)
  arg^2
  <environment: 0x2b0f230>
  > as.list(environment(funsC[[2]]))
  $retval
  function (arg = i)
  arg^2
  <environment: 0x2b0f230>

  $i
  [1] 2

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com