R error promise already under evaluation when using subset in function but no error in script

StackOverflow https://stackoverflow.com/questions/3073746

  •  28-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top