[R] frames and formulas

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Jul 20 12:25:59 CEST 2001


Kevin Murphy <murphyk at cs.berkeley.edu> writes:

> I can build a tree at the command line using
>   tr <- tree(V4 ~ ., dat)
> but I don't know how to build one inside a function - the following does
> not work
> 
>   foo <- function(dummy) {
>     tr <- tree(V4 ~ ., dummy)
>   }
>   tr2 <- foo(dat)
> 
> and produces the error
> 
>   Error in model.frame.default(formula = V4 ~ ., data = dummy) : 
> 	Object "dummy" not found
> 
> I know the solution has something to do with terms, formulas, frames,
> environments, etc., but I can't figure it out (despite having read sec
> 3.4-3.5 of 'S progamming').

Jonathan Li reported the same thing just three days ago on R-help. It
happens because tree uses the S-PLUS form of passing the frame number
of the parent (i.e. eval(model, sys.parent())) rather than the parent
environment, which conspires with an age-old bug in R that makes this
work only on the command line. The quick fix is to modify the tree
function to use eval(model, parent.frame()) instead. This is better R
style anyway, but we'll get around to fixing eval() eventually.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list