From pdalgd at gmail.com  Wed Oct  3 17:41:17 2012
From: pdalgd at gmail.com (peter dalgaard)
Date: Wed, 3 Oct 2012 17:41:17 +0200
Subject: [Rd] Creating functions programmatically
In-Reply-To: <CABdHhvEF04Z0bzWZP-BU60ymdMpgaS68tVsEq5XFme_1-USiQA@mail.gmail.com>
References: <CABdHhvH9Q3L16aCQPg=Tn5N+GqJTDW2vhf3=y6_DYcUOXwYvmQ@mail.gmail.com>
	<506C4B1F.3070403@gmail.com>
	<CADwqtCMEax2uGOioVDJhWsobJaB5Tk0XT=Ympze6wLvUMsrLSQ@mail.gmail.com>
	<CABdHhvEF04Z0bzWZP-BU60ymdMpgaS68tVsEq5XFme_1-USiQA@mail.gmail.com>
Message-ID: <A67545E5-155E-40F7-9B94-E30AB41EFE37@gmail.com>


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



[Rd] Creating functions programmatically
peter dalgaard