# [Rd] works in R-1.1.1 but not in R-development; why?

**Thomas Lumley
**
thomas@biostat.washington.edu

*Thu, 12 Oct 2000 10:35:03 -0700 (PDT)*

On 12 Oct 2000, Peter Dalgaard BSA wrote:
>* Ramon Diaz-Uriarte <ramon-diaz@teleline.es> writes:
*>*
*>* > Dear All,
*>* >
*>* > A library (PHYLOGR) that passed the usual tests in R-1.1.1 gives errors with
*>* > R-devel; my (mis?)understanding of scoping rules is
*>* > that it should have worked in both. The problems seem related to using the
*>* > name of the data frame for extracting weights or subsets within a function
*>* > call. The problems can be reproduced as follows:
*>* >
*>* > **********************
*>* >
*>* > datai <- data.frame( y = rnorm(10), x1 = rnorm(10), x2 = abs(rnorm(10)),
*>* > x3 = rep(seq(1,5),2), counter = rep(c(1,2),c(5,5)))
*>* >
*>* > formula <- as.formula(y ~ x1)
*>* >
*>* >
*>* > # the following fails in R-1.2.0 but not in R-1.1.1
*>* > # > Error in eval(expr, envir, enclos) : Object "datos" not found
*>* > lapply(split(datai,datai$counter),
*>* > function(datos,formula) {lm(formula = formula, data = datos,
*>* > weights = datos$x2)},
*>* > formula = formula)
*>*
*>* Ow!... This happens because of a change that makes formulas capture
*>* their environment of definition.
*>*
*>* A workaround is to explicitly set the environment of the formula to
*>* the current environment, like this:
*
There's a much simpler workaround
lapply(split(datai,datai$counter),function(dataos,formula) lm(formula,
data=datos,weights=x2), formula=formula)
because the weights will first be looked for in the data= argument
-thomas
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._