Rolf Turner
2025-Oct-25 03:17 UTC
[R] Construct a function with argments specified by an R expression
On Fri, 24 Oct 2025 22:31:53 -0400 Iris Simmons <ikwsimmo at gmail.com> wrote:> I would start by making a list of the desired length, where each > element is the missing argument: > > f <- rep(list(quote(expr = )), 2 + length(argnms)) > > then add names to the list: > > names(f) <- c("x", "y", argnms) > > and then use formals<- > > formals(foo) <- fWow! That was fast! And it seems to do exactly what I want. What I was missing, I guess, was the quote(expr = ) structure. I have never really understood quote(), nor expr() either. I'll just treat it as a black box for now.> Although, I would ask if you're certain that this is actually what > you want to do. It's usually a bad idea to have variable names that > you don't know. I'd to think it'd be better to have the default > argument: > > a = list() > > and supply a list of `a` values instead of several `a` arguments.Yes I understand that that would be the "sensible" approach but it doesn't work in the context with which I am confronted. I have more work to do before I get the approach that I've envisaged up and running. I may inflict more questions upon you later. (No good deed goes unpunished! ??) cheers, Rolf -- Honorary Research Fellow Department of Statistics University of Auckland Stats. Dep't. (secretaries) phone: +64-9-373-7599 ext. 89622 Home phone: +64-9-480-4619
Duncan Murdoch
2025-Oct-25 10:24 UTC
[R] Construct a function with argments specified by an R expression
Another way to do this is to make a prototype and then modify it. For
example,
foo <- function(x, y, others) {
# the body can be anything, we won't touch it
}
args <- formals(foo)
# Make extra copies of the 3rd argument
args[3:(2 + length(argnms))] <- args[3]
# Set the names of the new ones (or use Iris's code to set all the
names, your choice)
names(args)[3:(2 + length(argnms))] <- argnms
formals(foo) <- args
Duncan Murdoch
On 2025-10-24 11:17 p.m., Rolf Turner wrote:>
> On Fri, 24 Oct 2025 22:31:53 -0400
> Iris Simmons <ikwsimmo at gmail.com> wrote:
>
>> I would start by making a list of the desired length, where each
>> element is the missing argument:
>>
>> f <- rep(list(quote(expr = )), 2 + length(argnms))
>>
>> then add names to the list:
>>
>> names(f) <- c("x", "y", argnms)
>>
>> and then use formals<-
>>
>> formals(foo) <- f
>
> Wow! That was fast! And it seems to do exactly what I want.
>
> What I was missing, I guess, was the quote(expr = ) structure. I have
> never really understood quote(), nor expr() either. I'll just treat it
> as a black box for now.
>
>> Although, I would ask if you're certain that this is actually what
>> you want to do. It's usually a bad idea to have variable names that
>> you don't know. I'd to think it'd be better to have the
default
>> argument:
>>
>> a = list()
>>
>> and supply a list of `a` values instead of several `a` arguments.
>
> Yes I understand that that would be the "sensible" approach but
it
> doesn't work in the context with which I am confronted.
>
> I have more work to do before I get the approach that I've envisaged up
> and running.
>
> I may inflict more questions upon you later. (No good deed goes
> unpunished! ??)
>
>
> cheers,
>
> Rolf
>