Skip to content
Prev 12393 / 398502 Next

scoping problem?

Jonathan Qiang Li <jonqli at labs.agilent.com> writes:
In general, you need to know that scoping and evaluation rules can be
broken in the R system. It is possible for a function to get a hold of
the expression that is passed for an argument and evaluate it in an
environment of its own choice, or even use the symbolic expression as
such (that is how you get away with saying "library(tree)" without
getting a "there is no object called `tree'" type error.)

tree() is doing some such trickery at the start of the function.
Basically, it gets hold of the entire call, and then replaces the
function being called with model.frame.default and reevaluates in the
parent frame. Or rather, it tries to do that. The code is using
eval(model, sys.parent()) and somehow that fails. 

Replacing sys.parent() with parent.frame() inside tree does make it
work, which points to a possible bug in eval() when used with an
integer envir= argument. Will have a look.