Allan Engelhardt
2009-Jul-02 16:46 UTC
[R] How to use current value of variable in function definition?
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.
William Dunlap
2009-Jul-02 17:28 UTC
[R] How to use current value of variable in function definition?
> From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Allan Engelhardt > Sent: Thursday, July 02, 2009 9:47 AM > To: r-help at r-project.org > Subject: [R] How to use current value of variable in function > definition? > > 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] 1You might try local(), as in > c<-1 ; foo.local<-local({orig.c <- c ; function()orig.c}) > foo.local() [1] 1 > c<-3 > foo.local() [1] 1 It is possible for someone to alter the orig.c after you create foo.local, as in > assign("orig.c", 17, env=environment(foo.local)) > foo.local() [1] 17 Looking at the function's code will not make it clear where orig.c is coming from. The clue is that its environment is not one of the standard named ones, but it given by a hex number. > foo.local function()orig.c <environment: 0x02108c54> You could also use substitute() to change the code in the function. It can be messy to do but the resulting code may be clearer (although it won't give a hint as to where that constant came from). E.g., > foo.substitute<-function()orig.c > c<-1 ; functionBody(foo.substitute)<-do.call(substitute, list(functionBody(foo.substitute), list(orig.c=c))) > foo.substitute() [1] 1 > foo.substitute function () 1 Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com> but that just hurts. Please make the pain go away. > > Can someone wake up my brain? > > Allan. > > ______________________________________________ > 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. >
Mark Wardle
2009-Jul-02 20:02 UTC
[R] How to use current value of variable in function definition?
Hi. I've stared at your code for a number of minutes and can't understand what you're trying to achieve here. It looks as if you're fighting scope - it may be worth refactoring your approach to simplify matters. Otherwise, it sounds like a recipe for obfuscation! What are you trying to do really? b/w Mark 2009/7/2 Allan Engelhardt <allane at cybaea.com>:> 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 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. > >-- Dr. Mark Wardle Specialist registrar, Neurology Cardiff, UK