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:
data.ex2<-data.frame(sim.counter=c(rep(0,3),rep(1,3)),Tips=rep(c("a","b","c"),2),y=rnorm(6),x1=rnorm(6),x2=rnorm(6))
myf1(y~x1,data.ex2,weights=x2)
[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)
Ramón DÃaz-Uriarte Dept. Zoology and Statistics University of Wisconsin-Madison Madison, WI 53706-1381 email: rdiazuri at students.wisc.edu phone: 608-238-8041 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._