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.4...@gmail.com>
To: Iris Simmons <ikwsi...@gmail.com>
Cc: R-help <R-help@r-project.org>
Subject: Re: [R] Extracting specific arguments from "..."
Message-ID:
        <cagxfjbronopt-bodf6srpp79bwucppoo3+ycdgn3y-ytdu5...@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 <ikwsi...@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.4...@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@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]]

______________________________________________
R-help@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.

Reply via email to