Skip to content
Prev 5905 / 398506 Next

scoping problems?

On Mon, 29 May 2000, Ramon Diaz-Uriarte wrote:

            
Put a browser in myf2 and take a closer look. Your call is

Browse[1]> mf
model.frame(formula = formula, data = data, weights = weights)

Now, weights is x2, and x2 exists nowhere visible (it is a column of data).
So you want the value of weights substituted but not evaluated, and that
gets tricky, if not impossible.  So let's look for an easier way.

The usual way to do this is to assemble the call in myf1, not myf2.  As in

myf1 <- function(formula,data,weights=NULL,max.num=0,exclude.tips=NULL)
{
    mf <- match.call()
    mf$nax.num <- mf$exclude.tips <- NULL
    mf[[1]] <- as.name("model.frame")

    myf2<-function(mf) {
        mf <- eval(mf,sys.frame(sys.parent(2)))
        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(mf)
}
 
Note that I go up two parents, as the data are in the caller of myf1.
eval.parent(mf, 2) would be neater, or you could do this in myf1.

Another idea is to use subset= rather than the data manipulations here.