> lm2 <- function(...) lm(...) > lm2(mpg ~ wt, data=mtcars)Call: lm(formula = ..1, data = ..2) Coefficients: (Intercept) wt 37.285 -5.344> lm2(mpg ~ wt, weights=cyl, data=mtcars)Error in eval(expr, envir, enclos) : ..2 used in an incorrect context, no ... to look in Can anyone explain why this is happening? (Obviously this is a manufactured example, but it crops up in ggplot when you want to provide a function to perform smoothing) Thanks, Hadley
"hadley wickham" <h.wickham at gmail.com> writes:> > lm2 <- function(...) lm(...) > > lm2(mpg ~ wt, data=mtcars) > > Call: > lm(formula = ..1, data = ..2) > > Coefficients: > (Intercept) wt > 37.285 -5.344 > > > lm2(mpg ~ wt, weights=cyl, data=mtcars) > Error in eval(expr, envir, enclos) : ..2 used in an incorrect context, > no ... to look in > > > Can anyone explain why this is happening? (Obviously this is a > manufactured example, but it crops up in ggplot when you want to > provide a function to perform smoothing)Presumably this is due to the nonstandard evaluation of the weights argument. This is a long story, the short version is that the argument ought to have been designed to be a model formula, but we haven't been willing to sacrifice S-PLUS compatibility. A more extensive white-paper on the issues, written by Thomas Lumley, is at http://developer.r-project.org/nonstandard-eval.pdf -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
If you change it to: lm3 <- function(...) eval.parent(substitute(lm(...))) then it works.> lm3(mpg ~ wt, weights=cyl, data=mtcars)Call: lm(formula = mpg ~ wt, data = mtcars, weights = cyl) Coefficients: (Intercept) wt 35.50 -4.91 On 9/3/06, hadley wickham <h.wickham at gmail.com> wrote:> > lm2 <- function(...) lm(...) > > lm2(mpg ~ wt, data=mtcars) > > Call: > lm(formula = ..1, data = ..2) > > Coefficients: > (Intercept) wt > 37.285 -5.344 > > > lm2(mpg ~ wt, weights=cyl, data=mtcars) > Error in eval(expr, envir, enclos) : ..2 used in an incorrect context, > no ... to look in > > > Can anyone explain why this is happening? (Obviously this is a > manufactured example, but it crops up in ggplot when you want to > provide a function to perform smoothing) > > Thanks, > > Hadley > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >