[R] Extracting specific arguments from "..."
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Wed Jan 8 23:57:36 CET 2025
That's very nice, Hadley. Simple and clean. Never would have thought of it
myself.
As usual, however, in the course of my churnings, I have a further
complication to add. But first ...
**TO ALL**: Feel free to ignore the following, as I'm just fooling around
here and don't want to waste your time with my stupid stuff.
Anyway, the complication is motivated by the use of formals() or otherwise
that *programmatically* generates a character representation of the
arguments I want to select. So, for example:
> z <- "a"
## Then:
f1 <- function(...){
...elt(match(z, ...names())) ## since z gets evaluated in the call
}
## still works.
> f1(b =2, a=1, c=3)
[1] 1
But I haven't figured out how to modify your suggestion -- at least in a
simple way -- to do the same. Likely I've missed something, though.
Cheers,
Bert
On Wed, Jan 8, 2025 at 12:51 PM Hadley Wickham <h.wickham using gmail.com> wrote:
> I'd propose an alternative that I think is superior: rely on the semantics
> of ... to do the work for you:
>
> f1 <- function(...){
> one <- list(...)[['a']]
> two <- ...elt(match('a', ...names()))
> c(one, two, three(...))
> }
>
> three <- function(a, ...) {
> a
> }
>
> f1(a = 1, b = 2, c = 3)
> #> [1] 1 1 1
>
>
> On Sun, Jan 5, 2025 at 12:00 PM Bert Gunter <bgunter.4567 using 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 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
>> https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
> --
> http://hadley.nz
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list