Rolf Turner
2025-Oct-25 02:23 UTC
[R] Construct a function with argments specified by an R expression
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
Iris Simmons
2025-Oct-25 02:31 UTC
[R] Construct a function with argments specified by an R expression
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
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.
On Fri, Oct 24, 2025, 22:23 Rolf Turner <rolfturner at 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 at 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]]
Richard O'Keefe
2025-Oct-26 14:36 UTC
[R] Construct a function with argments specified by an R expression
Can I just mention the quick-and-dirty way that will get the job done? Not as elegant as use the special features of the language *meant* for this kind of job, but suitable for a bear of very little brain. 1. Write a function that constructs the code you want as text, written to a file. 2. source() the file.' On Sat, 25 Oct 2025 at 15:23, Rolf Turner <rolfturner at 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 at 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.
Gabor Grothendieck
2025-Oct-26 14:41 UTC
[R] Construct a function with argments specified by an R expression
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 at 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 at 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