[R] Construct a function with argments specified by an R expression
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Sun Oct 26 21:48:06 CET 2025
Well, in the spirit of Rolf's original request, a slightly different
version of Gabor's/Duncan's suggestions is:
buildFn <- function(argnms){
f <- function(){
## whatever
}
## produce a named (regular) list (of NA's here) with the desired names for
formals()
formals(f) <- setNames( as.list(c(rep(NA, length(argnms) + 2))),
nm = c('x', 'y', argnms))
f
}
Note that this requires/gives default arguments (NA's here) for everything,
which may be undesirable:
> foo <- buildFn(argnms = argnms)
> foo
function (x = NA, y = NA, a1 = NA, a2 = NA, a3 = NA, b1 = NA,
b2 = NA, b3 = NA)
{
}
Cheers,
Bert
On Sun, Oct 26, 2025 at 11:14 AM Gabor Grothendieck <ggrothendieck using gmail.com>
wrote:
> Here is an approach using as.function.formula from gsubfn:
>
> library (gsubfn)
>
> # inputs
> arg_names <- head(letters)
> f <- function() {}
>
> formals(f) <- formals(as.function(reformulate(arg_names)))
> f
>
> On Sun, Oct 26, 2025 at 10:47 AM Gabor Grothendieck
> <ggrothendieck using gmail.com> wrote:
> >
> > This can be simplified slightly to
> >
> > # inputs
> > arg_names <- c("x", "y", paste0("a", 1:3), paste0("b", 1:3))
> > f <- function() {}
> >
> > arg <- alist(x = )
> > formals(f) <- setNames(rep(arg, length(arg_names)), arg_names)
> > f
> >
> > ## function (x, y, a1, a2, a3, b1, b2, b3)
> > ## {
> > ## }
> >
> > On Sun, Oct 26, 2025 at 10:41 AM Gabor Grothendieck
> > <ggrothendieck using gmail.com> wrote:
> > >
> > > Define the names and a prototype and then use formals(f) <- ... as
> shown.
> > >
> > > # inputs
> > > arg_names <- c("x", "y", paste0("a", 1:3), paste0("b", 1:3))
> > > f <- function() {}
> > >
> > > arg <- alist(x = )
> > > formals(f) <- setNames(c(arg, rep(arg, length(arg_names)-1)),
> arg_names)
> > > f
> > >
> > > giving
> > >
> > > function (x, y, a1, a2, a3, b1, b2, b3)
> > > {
> > > }
> > >
> > > On Fri, Oct 24, 2025 at 10:23 PM Rolf Turner <rolfturner using posteo.net>
> wrote:
> > > >
> > > >
> > > > I want to build a function (say "buildFn") to *return* a function of
> the
> > > > form
> > > >
> > > > foo(x,y,a1, ... an, b1, ..., bn)
> > > >
> > > > where the arguments ai and bi are given in the form of a list created
> > > > in R. E.g. I'd like to be able to say
> > > >
> > > > argnms <- c(paste0("a",1:3),paste0("b",1:3"))
> > > > foo <- buildFn(argnms)
> > > >
> > > > with the resulting "foo" having arguments x, y, a1, a2, a3, b1, b2,
> b3.
> > > >
> > > > I thought I might be able to do this using "formals<-" but I cannot
> get
> > > > this to work. i could provide more detail of what I've tried, but
> it's
> > > > probably not worth it.
> > > >
> > > > Can anyone steer me in the right direction? Thanks.
> > > >
> > > > cheers,
> > > >
> > > > Rolf Turner
> > > >
> > > > --
> > > > 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
> > > >
> > > > ______________________________________________
> > > > 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.
> > >
> > >
> > >
> > > --
> > > Statistics & Software Consulting
> > > GKX Group, GKX Associates Inc.
> > > tel: 1-877-GKX-GROUP
> > > email: ggrothendieck at gmail.com
> >
> >
> >
> > --
> > Statistics & Software Consulting
> > GKX Group, GKX Associates Inc.
> > tel: 1-877-GKX-GROUP
> > email: ggrothendieck at gmail.com
>
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
> ______________________________________________
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list