[R] Extracting specific arguments from "..."

Bert Gunter
Mon, 06 Jan 2025 22:22:10 -0000

Thanks Jorgen.

I thought your approach to getting the argument expressions was clever, but
somewhat convoluted. I think the usual simple way is to use match.call()
(or sys.call() )to get the unevaluated argument expressions;  e.g. ...

f <- function(...){
   match.call()
}
> f(a = 'red', b = sin(zzz))
f(a = "red", b = sin(zzz))

The return value is an object of class call that can be subscripted as (or
converted by as.list() to) a list to extract the argument
expressions:
> f(a = 'red', b = sin(zzz))$b
sin(zzz)

You'll note that the $b component is again of class "call". So you
may wish
to convert it to character or expression or whatever for further
processing, depending on context. Obviously, I haven't thought about this
carefully.

You raise an important point about robustness. I believe this approach to
extracting the call expressions should be fairly robust, but I do get
confused about the lay of the land when you add promises with default
arguments that may not yet have been forced before match.call() is called.
You may have to wrestle with sys.call() and it's "wh" argument to
make
things work the way you want in that situation. I leave such delights to
wiser heads, as well as any corrections or refinements to anything that
I've said here.

Cheers,
Bert

On Mon, Jan 6, 2025 at 9:55?AM Jorgen Harmse <JHarmse at roku.com> wrote:

> I think Bert Gunter is right, but do you want partial matches (not found
> by match), and how robust do you want the code to be?
>
>
>
> f <- function(?)
>
> { pos <- match('a', ...names())
>
>   if (is.na(pos))
>
>     stop("a is required.")
>
>   ?elt(pos)
>
> }
>
>
>
> Incidentally, what is the best way to extract the expression without
> evaluating it?
>
>
>
> g <- function(...)
>
> { pos <- match('a',...names())
>
>   if (is.na(pos))
>
>     stop("a is missing.")
>
>   (function(a,...) substitute(a)) (...)
>
> }
>
>
>
> Regards,
>
> Jorgen Harmse.
>
>
>
> Message: 8
> Date: Sun, 5 Jan 2025 11:17:02 -0800
> From: Bert Gunter <bgunter.4567 at gmail.com>
> To: Iris Simmons <ikwsimmo at gmail.com>
> Cc: R-help <R-help at r-project.org>
> Subject: Re: [R] Extracting specific arguments from "..."
> Message-ID:
>         <
> CAGxFJbROnopt-boDF6sRPP79bWUcPPOO3+ycDGN3y-YTDU5b6Q at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Thanks, Iris.
> That is what I suspected, but it wasn't clear to me from the docs.
>
> Best,
> Bert
>
> On Sun, Jan 5, 2025 at 10:16?AM Iris Simmons <ikwsimmo at gmail.com>
wrote:
> >
> > I would use two because it does not force the evaluation of the other
> arguments in the ... list.
> >
> >
> >
> > On Sun, Jan 5, 2025, 13:00 Bert Gunter <bgunter.4567 at
gmail.com> wrote:
> >>
> >> Consider:
> >>
> >> f1 <- function(...){
> >>   one <- list(...)[['a']]
> >>   two <- ...elt(match('a', ...names()))
> >>   c(one, two)
> >> }
> >> ## Here "..." is an argument list with "a"
somewhere in it, but in an
> >> unknown position.
> >>
> >> > f1(b=5, a = 2, c=7)
> >> [1] 2 2
> >>
> >> Which is better for extracting a specific named argument, one<-
or
> >> two<- ?  Or a third alternative that is better than both?
> >> Comments and critiques welcome.
> >>
> >> Cheers,
> >> Bert
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more,
see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> https://www.r-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>

	[[alternative HTML version deleted]]