[Rd] Creating functions programmatically

2012-10-03 Thread Hadley Wickham
Hi all, A function has three components: arguments, body and environment. Is there no base function that allows us to create a function from those three components? The best I could come up with is: make_function - function(args, body, env = parent.frame()) { args - as.pairlist(args)

Re: [Rd] Creating functions programmatically

2012-10-03 Thread Hadley Wickham
Am I missing a built in way to do this? Also, is there a built in equivalent to my mquote (= multiquote, for producing a named list of quoted inputs)? Oops, built in equivalent to mquote is alist. (It would be really handy if that was referenced from quote) Hadley -- RStudio / Rice

Re: [Rd] Creating functions programmatically

2012-10-03 Thread Duncan Murdoch
On 03/10/2012 10:16 AM, Hadley Wickham wrote: Hi all, A function has three components: arguments, body and environment. Is there no base function that allows us to create a function from those three components? There is: it is `function`. The parser converts your function definitions into

Re: [Rd] Creating functions programmatically

2012-10-03 Thread Gabriel Becker
Hadley, You could do this: make_fun = function(args, body, env) { f = function() formals(f) = args body(f) = body environment(f) = env f } If for some reason using function() itself as Duncan suggested won't work. Note: args will need to be the right kind of pairlist, eg what is returned from

Re: [Rd] Creating functions programmatically

2012-10-03 Thread Hadley Wickham
There is: it is `function`. The parser converts your function definitions into a call to it. (It has 3 arguments: the formals, the body, and the srcref. The environment is added when it is evaluated.) So your make_function below is pretty similar (but because `function` is primitive,

Re: [Rd] Creating functions programmatically

2012-10-03 Thread Hadley Wickham
On Wed, Oct 3, 2012 at 9:33 AM, Gabriel Becker gmbec...@ucdavis.edu wrote: Hadley, You could do this: make_fun = function(args, body, env) { f = function() formals(f) = args body(f) = body environment(f) = env f } If for some reason using function() itself as Duncan suggested won't

Re: [Rd] Creating functions programmatically

2012-10-03 Thread Gabriel Becker
Err, typo: function() should of course say function() {} ~G On Wed, Oct 3, 2012 at 7:49 AM, Hadley Wickham h.wick...@gmail.com wrote: On Wed, Oct 3, 2012 at 9:33 AM, Gabriel Becker gmbec...@ucdavis.edu wrote: Hadley, You could do this: make_fun = function(args, body, env) { f =

Re: [Rd] Creating functions programmatically

2012-10-03 Thread peter dalgaard
On Oct 3, 2012, at 16:49 , Hadley Wickham wrote: On Wed, Oct 3, 2012 at 9:33 AM, Gabriel Becker gmbec...@ucdavis.edu wrote: Hadley, You could do this: make_fun = function(args, body, env) { f = function() formals(f) = args body(f) = body environment(f) = env f } If for some

Re: [Rd] Creating functions programmatically

2012-10-03 Thread Peter Meilstrup
On Wed, Oct 3, 2012 at 7:47 AM, Hadley Wickham h.wick...@gmail.com wrote: There is: it is `function`. The parser converts your function definitions into a call to it. (It has 3 arguments: the formals, the body, and the srcref. The environment is added when it is evaluated.) So your

Re: [Rd] Creating functions programmatically

2012-10-03 Thread Hadley Wickham
I think `function` does not eval its arguments, and it demands a pairlist. So this works: f - eval(substitute(`function`(args, body), list(args=as.pairlist(alist(a=1)), body=quote(a+1 The other thing to notice is a syntax difference between function and ordinary calls: when writing