[Rd] How do you construct a function from a list?

Kurt Hornik Kurt.Hornik@ci.tuwien.ac.at
Wed, 22 Nov 2000 13:08:26 +0100


>>>>> Duncan Murdoch writes:

> I'm trying to get a data.restore function to work on functions.  One
> thing I can't figure out:  how do you construct a function from it's
> component parts?

> For example, I can construct a for loop as

> forloop <-
> as.call(list(as.name('for'),as.name('i'),1,as.call(list(as.name('junk')))))

> which results in 

>  for (i in 1) junk()

> But how do I put that in a function?  For example, I'd like the
> function

>   function()  for (i in 1) junk()

> but  as.function(list(forloop)) results in

>  function () 
>  list(for (i in 1) junk())

> which returns the for loop in a list, it doesn't execute it.  How do I
> construct the function?

> (This might be a bug in as.function, because 

>   as.function(c(alist(a=,b=2),forloop))

> returns the expected function

>  function (a, b = 2) 
>  for (i in 1) junk()

> It's just that I can't seem to find a way to do it when there are no
> arguments.)

Duncan,

I think the easiest approach is to do

	foo <- function() {}
	body(foo) <- forloop

Re the above, I guess Peter would know more about this but is currently
swamped with other work.  I also observe that

R> as.function(alist(forloop))
function () 
list(forloop)
R> as.function(list(forloop))
function () 
list(for (i in 1) junk())

which does not seem right.

-k
	
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._