[Rd] Creating functions programmatically

peter dalgaard pdalgd at gmail.com
Wed Oct 3 17:41:17 CEST 2012


On Oct 3, 2012, at 16:49 , Hadley Wickham wrote:

> On Wed, Oct 3, 2012 at 9:33 AM, Gabriel Becker <gmbecker at 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 work.
> 
> Oh that's an approach that's easier to explain to others - thanks!
> 
> Hadley

There's also as.function(), although its semantics are a bit arcane. It and the converse as.list.function() have their origin in S-PLUS, where it was commonplace (or relatively so) to treat a function as a list, like in ls[[2]].

> 
> -- 
> RStudio / Rice University
> http://had.co.nz/
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-devel mailing list