[R] Construct a function with argments specified by an R expression

Rolf Turner ro||turner @end|ng |rom po@teo@net
Sat Oct 25 22:05:51 CEST 2025


On Sat, 25 Oct 2025 06:24:48 -0400
Duncan Murdoch <murdoch.duncan using gmail.com> wrote:

> 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

That saves my having to worry about quote() and expr.  Ta.

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



More information about the R-help mailing list