tolga.i.uzuner at jpmorgan.com
2008-Nov-25 09:26 UTC
[R] "recursive default argument" error
Dear R Users, I have a function foo in a script with default values for some of the parameters as follows: testparams=list(a=1,b=2,c=3) foo<-function(x,y,testparams=testparams) x+y+testparams$a+testparams$b+testparams$c When I try to run foo(1,2), I get the following error:> testparams=list(a=1,b=2,c=3) > foo<-function(x,y,testparams=testparams)+ x+y+testparams$a+testparams$b+testparams$c> foo(1,2)Error in foo(1, 2) : promise already under evaluation: recursive default argument reference or earlier problems?>I can obviously rename testparams but I would still like to understand why this breaks. I would have thought the right hand side of "testparams=testparams" would have been evaluated in the namespace outside the script and not the function. How can I force it to be evaluated in the namespace outside the function ? Thanks in advance, Tolga Generally, this communication is for informational purposes only and it is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. In the event you are receiving the offering materials attached below related to your interest in hedge funds or private equity, this communication may be intended as an offer or solicitation for the purchase or sale of such fund(s). All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to UK legal entities. [[alternative HTML version deleted]]
tolga.i.uzuner at jpmorgan.com wrote:> Dear R Users, > > I have a function foo in a script with default values for some of the > parameters as follows: > > testparams=list(a=1,b=2,c=3) > foo<-function(x,y,testparams=testparams) > x+y+testparams$a+testparams$b+testparams$c > > > When I try to run foo(1,2), I get the following error: > > >> testparams=list(a=1,b=2,c=3) >> foo<-function(x,y,testparams=testparams) >> > + x+y+testparams$a+testparams$b+testparams$c > >> foo(1,2) >> > Error in foo(1, 2) : > promise already under evaluation: recursive default argument reference > or earlier problems? > > >sure. from the r language manual, sec. 4.3.3: "One of the most important things to know about the evaluation of arguments to a function is that supplied arguments and default arguments are treated differently. The supplied arguments to a function are evaluated in the evaluation frame of the calling function. The default arguments to a function are evaluated in the evaluation frame of the function." the parameter testparams, when no matching argument is passed, is given the default value which is the value of the variable testparams looked-up *not* in the environment where foo is defined, and *not* in the environment where foo is called, but rather in the local environment created when the function is called and where parameters are mapped to values -- and in this environment, testparams is a parameter, which is already being under evaluation, hence the recursive lookup error. in some programming languages, testparams (the default value) would come from the definition-time environment, so you'd not have problems; in others, it's more like in r: python> x = 1; def foo(x, y=x): return y; foo(2) # 1 ruby> x = 1; def foo(x, y=x); y; end; foo(2) # 2 you just have to get used to it.> I can obviously rename testparams but I would still like to understand why > this breaks. I would have thought the right hand side of > "testparams=testparams" would have been evaluated in the namespace outside > the script and not the function. How can I force it to be evaluated in the > namespace outside the function ? >something like this ugly hack seems to solve the problem: foo = function(testparams = get("testparams", envir=environment(foo)) vQ