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

**Peter Dalgaard BSA
**
p.dalgaard@biostat.ku.dk

*12 Oct 2000 19:37:49 +0200*

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:
lapply(split(datai,datai$counter),
function(datos,formula) {
environment(formula)<-environment()
lm(formula = formula, data = datos,
weights = datos$x2)
},
formula = formula)
but I bet Luke wants to comment on this...
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._