[R] Question concerning library function "nlme" and lexical scoping

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Mar 10 19:07:18 CET 2004


On Wed, 10 Mar 2004 Jens_Praestgaard at hgsi.com wrote:

> I am running into problems calling the library function nlme from within
> another function.  Basically,the following function (with v a list
> containing data and initialization values)
> 
> > testfunc
> function(dat=v) {
> test<-nlsList(result~a+(b-a)/(1+(conc/(c+z*cdiff))^d)
> |rep,start=dat$init,data=dat$mixeddat)
> return(nlme(test,random=b~1))
> }
>  produces the error message
> 
> Error in eval(expr, envir, enclos) : Object "dat" not found.
> 
> However, if the assignment dat<-v is done globally, then testfunc()
> produces the desired mixed model analysis.
> 
> As I understand lexical scoping, the nlme function should take the value of
> "dat" from the calling environment through its "data = sys.frame(sys.parent
> ())" argument. Instead it seems to only recognize dat if it is defined
> globally.

Let's be careful. Lexical scoping is to do with where a function is 
defined, so the lexical scope for nlme() is namespace:nlme.  That's not 
pertinent here.

But if you do traceback() you will see that nlme is not looking in data 
for dat, but using the result `test'.  I would need a working example to 
be sure enough of what is going on.

http://developer.r-project.org/nonstandard-eval.pdf
may be instructive.  nlme is particularly non-standard as it has to deal 
with several formulae, and get them in several ways.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list