user1798507
user1798507

Reputation: 21

Object not found when called within a function with a formula

I'm trying to call the checkm function from within another function that accepts a formula as a parameter. I'm getting an object not found error. Here is the minimal implementation and error.

library(lrmest)
data(pcd)
form<-formula(Y~X1+X2+X3+X4)
checkm(form,data=pcd)


Wrap<-function(f){
  checkm(f,data=pcd)
}

Wrap(form)

The error is:

Error in model.frame(formula = f, data = pcd, NULL) : 
  object 'f' not found
Called from: eval(expr, envir, enclos)

My guess from reading around is this has to do with my not understanding environments or promises but given that I don't understand them, I'm probably wrong.

Any quick fixes?

Upvotes: 0

Views: 1403

Answers (2)

MrFlick
MrFlick

Reputation: 206566

As @joran pointed out, there is a bug/error in the function caused from not using the correct frame to evaluate the command. If you swap out checkm for lm you'll see it runs just fine. You can create your own function that changes just that one line of code with

checkm2<-checkm
body(checkm2)[[6]]<-quote(cal <- eval(cal, parent.frame()))

And then run

library(lrmest)
data(pcd)
form<-formula(Y~X1+X2+X3+X4)
checkm2(form,data=pcd)


Wrap<-function(f){
  checkm2(f,data=pcd)
}

Wrap(form)

and everything seems to run properly. So that just appears to be the fault of the people who wrote the code. You might consider contacting them to file a bug report.

Upvotes: 1

oropendola
oropendola

Reputation: 1101

One quick fix is to change the name of your formula argument. It happens to conflict with the eval(cal) call within checkm. I suspect @joran is right that this isn't your fault. This works:

library(lrmest)
data(pcd)
form<-Y~X1+X2+X3+X4
checkm(form,data=pcd)


Wrap<-function(formula){
  checkm(formula,data=pcd)
}

Wrap(form)

Upvotes: 2

Related Questions