[R] Use of ellipsis

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu May 26 21:57:23 CEST 2022


On Thu, 26 May 2022 20:54:50 +0200
Andreas Matre <r using elonus.net> wrote:

> fit_model <- function(formula, data, ...) {
>    lm(formula, data, ...)
> }

> fit_model(y ~ x1 + x2, data = data, weights = rep(1, 100)) # This
> does not work

When I run traceback(), I see an eval() there: lm() captures its own
call, replaces lm with stats::model.frame and evaluates that call in
the parent frame. I don't have a good explanation why it's the ellipsis
that fails here, but adding more non-standard evaluation seems to fix
the problem:

fit_model2 <- function(formula, data, ...)
 eval(substitute(lm(formula, data, ...)), parent.frame())

fit_model2(y ~ x1 + x2, data = data, weights = rep(1, 100))

I think this works because substitute() expands the ellipsis into its
return value.

-- 
Best regards,
Ivan



More information about the R-help mailing list