Can anyone explain what I'm doing wrong here: > fred <- data.frame() > class(fred) [1] "data.frame" > test.fn <- function(x,class=class(x)) {class} > test.fn(fred) Error in test.fn(fred) : promise already under evaluation: recursive default argument reference or earlier problems? R 2.5.1 on both Windows and SUSE Linux. -- Sanford Weisberg, sandy at stat.umn.edu Office and mailing address: University of Minnesota, School of Statistics 312 Ford Hall, 224 Church St. SE, Minneapolis, MN 55455 612-625-8355, FAX 612-624-8868 St. Paul office: 146 Classroom-Office Building, 612-625-8777
You have a recursive reference. Try using a different name other than class. For example, in the following we use class. with a dot at the end (so that abbreviation of it still allows the user to write class): fred <- data.frame() test.fn <- function(x,class.=class(x)) {class.} test.fn(fred) # "data.frame" test.fn(fred, class = "x") # "x" On 9/12/07, Sandy Weisberg <sandy at stat.umn.edu> wrote:> Can anyone explain what I'm doing wrong here: > > > fred <- data.frame() > > class(fred) > [1] "data.frame" > > test.fn <- function(x,class=class(x)) {class} > > test.fn(fred) > Error in test.fn(fred) : promise already under evaluation: recursive > default argument reference or earlier problems? > > R 2.5.1 on both Windows and SUSE Linux. > > > -- > Sanford Weisberg, sandy at stat.umn.edu > Office and mailing address: > University of Minnesota, School of Statistics > 312 Ford Hall, 224 Church St. SE, Minneapolis, MN 55455 > 612-625-8355, FAX 612-624-8868 > > St. Paul office: > 146 Classroom-Office Building, 612-625-8777 > > ______________________________________________ > 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. >
On 9/12/2007 12:48 PM, Sandy Weisberg wrote:> Can anyone explain what I'm doing wrong here: > > > fred <- data.frame() > > class(fred) > [1] "data.frame" > > test.fn <- function(x,class=class(x)) {class} > > test.fn(fred) > Error in test.fn(fred) : promise already under evaluation: recursive > default argument reference or earlier problems? > > R 2.5.1 on both Windows and SUSE Linux.As Gabor said, this is a recursive reference. In order to know whather your default value class(x) should evaluate using the local variable named class or should go back to the base package, R needs to evaluate it. And there's the loop. Another solution besides Gabor's suggestion of renaming the argument is to be explicit that you want the base class() function, e.g. > test.fn <- function(x,class=base::class(x)) {class} > fred <- data.frame() > test.fn(fred) [1] "data.frame" Duncan Murdoch