R already evaluated when using a subset in a function, but errors in the script

I get 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 am getting is:

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

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

+1
source share
1 answer

This is your recursive use of TypeIDs . Keep in mind that function arguments are lazily evaluated, which allows you to use cool things like function(foo, bar = foo) . Unfortunately, in this case, the default setting for TypeID itself causes recursion in the evaluation. Try changing the name of either the parameter or the external object.

+6
source

Source: https://habr.com/ru/post/948111/


All Articles