Skip to content
Prev 5898 / 398506 Next

scoping problems?

Dear All,

This is driving me nuts; I think it should be very simple (and I am sure the
answer is in front of me somewhere in ch. 3 of "S programming") but I just don't
get it.

It seems that "weights" never gets passed on to the second funciton as it
should:



myf1<-function(formula,data,weights=NULL,max.num=0,exclude.tips=NULL){
  myf2<-function(formula,data,weights){
    mf <- match.call()
    mf[[1]] <- as.name("model.frame")
    print("we get up to here")
    mf<-eval(mf,sys.frame(sys.parent())) #this is the culprit doesn't work
    w <- model.weights(mf)
    # do several things here; for simplicity just
    print(w)
    }    
# the next two lines exclude certain data points
  if(!is.null(exclude.tips)) data<-data[match(data$Tips,exclude.tips,nomatch=0)==0,] 
  if(max.num) data<-data[data$sim.counter<max.num+1,]
  myf2(formula,data,weights=weights)
}


The following is an example:
[1] "we get up to here"
Error in eval(expr, envir, enclos) : Object "x2" not found

Why doesn't it find x2? Isn't x2 right there? Note that if I define myf2 as a
function in the global env. and I call it, it works fine.

What I am doing wrong?

Thanks,

Ramon

P.S. In case it matters, I am using R 1.0.1 (under Linux)