Re: [R] best practice(s) for retrieving a local variable from a closure

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri, 08 Apr 2011 21:55:33 -0400

On Fri, Apr 8, 2011 at 9:48 PM, Benjamin Tyner <btyner_at_gmail.com> wrote:
> Greetings,
>
> Say I have defined
>
>  mp <- function(a) function(x) x^a
>
>  f2 <- mp(2)
>
> and I would like to retrieve the "a" which is local to f2. Two options come
> to mind;
>
>  get("a", envir=environment(f2))
>  eval(substitute(a), environment(f2))
>
> I'm curious if one of these is preferred over the other in terms of
> efficiency, robustness, aesthetics, etc. Or perhaps someone can share
> another way to skin this cat, besides wrappers around eval() like
>
>  evalq(a, environment(f2))
>  with(environment(f2), a)
>
> Also, I'd like to confirm that I have used the correct terminology in the
> subject line, that "a" is a local variable with respect to f2, which is a
> closure?
>

Solutions involving eval tend to be less preferred. "a" is termed a free variable of f2.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sat 09 Apr 2011 - 01:57:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 09 Apr 2011 - 02:50:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive