views:

47

answers:

1

I'm getting a strange error when I run the following function:

TypeIDs=c(18283,18284,17119,17121,17123,17125,17127,17129,17131,17133,18367,18369,18371,18373,18375,18377,18379)

featsave<-function(featfile,TypeIDs=TypeIDs) {
    mydata1<-read.table(featfile,header=TRUE)
    mydata2<-subset(mydata1,TypeID %in% TypeIDs)

    mydata<-as.data.frame(cast(mydata2, Feat1 + Feat2 + ID ~ TypeID,value="value"))
    save(mydata,file="mydatafile.Rdata",compress=TRUE)
    return(mydata)
}

with the following data:

Feat1   Feat2   ID  Feat3   Feat4   TypeID  value
1   1   1   6   266 18283   280.00
1   1   1   6   266 18284   20.00
1   1   1   6   266 18285   0.00
1   1   1   6   266 17116   0.00
1   1   1   6   266 17117   0.00
1   1   1   6   266 17118   0.00
1   1   1   6   266 17119   68.75
1   1   1   6   266 17120   0.00
1   1   1   6   266 17121   1.26

The error I get is:

Error in inherits(x, "factor") : 
  promise already under evaluation: recursive default argument reference or earlier problems?

This error occurs on the mydata2 line where I take a subset of the data. I can't seem to debug it because if I instead run each line of the function in the interpreter, I no longer get the error. What gives?

+3  A: 

It's your recursive use of TypeIDs. The thing to remember is that function arguments are lazily evaluated, which allows cool stuff like function(foo, bar = foo). Unfortunately in this case setting the default for TypeIDs to itself causes a recursion in the evaluation. Try changing the name of either the parameter or the outer object.

Jonathan Chang
Great. Thanks a lot!
fideli