[R] Scoping

Thomas Lumley thomas at biostat.washington.edu
Fri Nov 20 18:47:25 CET 1998


On Fri, 20 Nov 1998, Goran Brostrom wrote:

> I must admit that I don't fully appreciate the scoping rules in R.
> For instance:
> 
> myfunc <-function(tt, event, x)
>    {
>      cat(tt, "\n")
>      coxph(Surv(tt, event) ~ x)
>    }
> 


This seems to be a bug in coxph, I'm already looking into it.  However,
the scoping rules are more complicated in the case of model formulae than
for normal functions.  Most functions that use model formulae explicitly
bypass the normal scoping rules using eval(). This is to allow the formula
and other objects like weights= and subset= to be evaluated in a supplied
data frame.

There is a genuine problem with model frames and formulae in keeping track
of where things should be evaluated.  In this case you clearly want it
evaluated in the calling frame, but if you had fitted a cox model and did

myotherfunc<-function(object){
	residuals(object,type="score")
}

you would want the object's model frame recomputed in the global
environment (or wherever it was first computed). The regression objects do
not necessarily store enough information to do this.

	-thomas


Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle.



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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