Hi, I know S manuals used to warn against using the same names for a variable and a function, but I have never seen that cause problems in R, so I usually don't pay much attention to it. Which is why the following behaviour came as a surprise:> bar <- function() 1 > foo <- function(bar = bar()) {+ bar + }> foo(9)[1] 9> foo()Error in foo() : recursive default argument reference Exactly what rule am I violating here? The following gives a slightly different error, but I assume it has a similar origin: bar <- function() 1 foo <- function(bar) { if (missing(bar)) bar <- bar() bar } foo() This version works fine though (so the rule probably involves function arguments somehow): foo <- function(baz) { if (missing(baz)) { baz <- function() 2 baz <- baz() } baz } foo() -Deepayan
Gabor Grothendieck
2006-Sep-12 05:55 UTC
[Rd] unexpected behaviour when defining a function
You can't have x=x in an argument list. Try the following noting that we put a dot at the end of bar:> bar <- function() 1 > foo <- function(bar. = bar()) {+ bar + }> foo()function() 1> foo(bar = bar)function() 1 On 9/11/06, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:> Hi, > > I know S manuals used to warn against using the same names for a > variable and a function, but I have never seen that cause problems in > R, so I usually don't pay much attention to it. Which is why the > following behaviour came as a surprise: > > > bar <- function() 1 > > foo <- function(bar = bar()) { > + bar > + } > > foo(9) > [1] 9 > > foo() > Error in foo() : recursive default argument reference > > Exactly what rule am I violating here? > > The following gives a slightly different error, but I assume it has a > similar origin: > > bar <- function() 1 > foo <- function(bar) { > if (missing(bar)) bar <- bar() > bar > } > foo() > > This version works fine though (so the rule probably involves function > arguments somehow): > > foo <- function(baz) { > if (missing(baz)) { > baz <- function() 2 > baz <- baz() > } > baz > } > foo() > > -Deepayan > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Prof Brian Ripley
2006-Sep-12 06:42 UTC
[Rd] unexpected behaviour when defining a function
On Mon, 11 Sep 2006, Deepayan Sarkar wrote:> Hi, > > I know S manuals used to warn against using the same names for a > variable and a function, but I have never seen that cause problems in > R, so I usually don't pay much attention to it.But in this case you have a promise. (BTW, it can still cause problems in R, hence the following NEWS item for 2.4.0: Lookup for S3 methods is confined to functions: previously a non-function 'fun.class' could have masked a function of the same name. ) Note that you do have to look at an object to find out if it is a function, and that means forcing promises, the problem here.> Which is why the following behaviour came as a surprise: > > > bar <- function() 1 > > foo <- function(bar = bar()) { > + bar > + } > > foo(9) > [1] 9 > > foo() > Error in foo() : recursive default argument reference > > Exactly what rule am I violating here?That an argument default value cannot refer to the argument. This is an argument with a default value that is relying on lazy evaluation. When you come to evaluate 'bar' it is a promise with value bar(). Evaluating that value looks up 'bar' from the evaluation frame of foo() and the first candidate it finds is the argument it is the process of evaluating, hence the message.> The following gives a slightly different error, but I assume it has a > similar origin: > > bar <- function() 1 > foo <- function(bar) { > if (missing(bar)) bar <- bar() > bar > } > foo()It says> Error in foo() : argument "bar" is missing, with no defaultand that is caused by bar <- bar(): it is looking for argument bar (to see if it is a function which can be called) and that argument has no default. (I would have thought that one was clear enough.) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595