[R] How to use current value of variable in function definition?

Mark Wardle mark at wardle.org
Fri Jul 3 08:16:15 CEST 2009


2009/7/2 Peter Dalgaard <p.dalgaard at biostat.ku.dk>:
> Mark Wardle wrote:
>> [...]
>> Otherwise, it sounds like a recipe for obfuscation! What are you
>> trying to do really?
>
> Not really obfuscation, there are legitimate reasons to want this sort of
> behaviour. See demo(scoping) for a related example
> [...]

I would have implemented this using either a OO approach or (if there
was not going to be future code re-use) by defining a hidden variable
with a lazy initialisation helper function that returns a cached value
of the data or acquires data for the first time. Not as elegant as the
solution given by Dr. Dalgaard.

-- 
Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK




More information about the R-help mailing list