[R] Use of ellipsis

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Fri May 27 00:51:38 CEST 2022

Slight addendum. I would appreciate correction if anything I say is wrong.

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

That seems to be correct. ?substitute explicitly states:
"Substitution takes place by examining each component of the parse
tree as follows: If it is not a bound symbol in env, it is unchanged.
***If it is a promise object, i.e., a formal argument to a function or
explicitly created using delayedAssign(), the expression slot of the
promise replaces the symbol. ***..."
I interpret the starred phrase as saying that the '...' argument is
replaced by its expression slot, which in the example call is

FWIW, you don't need to explicitly provide parent.frame() as it's the
default of the 'envir' argument of eval anyway.
My do.call() idiom works because, as usual,  list(...) evaluates its
... argument before calling list() on it.

Either way, the confusion of nse is avoided for the 'weights' argument.


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Thu, May 26, 2022 at 12:57 PM Ivan Krylov <krylov.r00t using gmail.com> wrote:
> 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
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list