Java Mailing List Archive

http://www.r-help.com/

Home » R Help for Statistical Computing »

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

Allan Engelhardt

2009-07-02

Replies: Find Java Web Hosting

Author LoginPost Reply
Must be the heat or something but I can't get my brain into gear and
figure out how to get something like

if (1) { c <- 1; foo <- function () print(c); }
c <- 2
foo()

to print 1, not 2. (The real life example is a little more complex, but
you get the idea. I don't want the variable c in the function
definition, I want its value at that time.)

The only thing I have been able to come up with is something like

if (1) foo <- (function () { c <- 1; return(function () print(c)) })()
c <- 2
foo()
# [1] 1

but that just hurts. Please make the pain go away.

Can someone wake up my brain?

Allan.

______________________________________________
R-help@(protected)
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.
©2008 r-help.com - Jax Systems, LLC, U.S.A.