Skip to content
Back to formatted view

Raw Message

Message-ID: <4D9FBAD5.7070801@gmail.com>
Date: 2011-04-09T01:48:05Z
From: Benjamin Tyner
Subject: best practice(s) for retrieving a local variable from a closure

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?

Thanks,
Ben