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

2025-10-28 Thread Peter Dalgaard
This is kind of fun, although it gets a bit far into the realm of obfuscation…

It also seems to work to do

> arg_names <- c("x", "y", paste0("a", 1:3), paste0("b", 1:3))
> f <- function()"hello"
> formals(f) <- sapply(arg_names,\(.)unname(alist(x=)))
> f
function (x, y, a1, a2, a3, b1, b2, b3)  “hello”

- pd


> On 26 Oct 2025, at 15.47, Gabor Grothendieck  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
>  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  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
>>> 
>>> __
>>> [email protected] 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
> 
> __
> [email protected] 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.


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business SchoolSolbjerg Plads 3, 2000 
Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [email protected]  Priv: [email protected]

__
[email protected] 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.


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

2025-10-26 Thread Bert Gunter
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 
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
>  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
> >  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 
> 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
> > > >
> > > > __
> > > > [email protected] 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
>
> __
> [email protected] 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]]

__
[email protected] 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.


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

2025-10-26 Thread Gabor Grothendieck
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
 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
>  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  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
> > >
> > > __
> > > [email protected] 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

__
[email protected] 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.


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

2025-10-26 Thread Bert Gunter
I will correct myself. "Special features of the languages" is not the same
as "computing on the language." So my comment misunderstood your meaning.

Bert

On Sun, Oct 26, 2025 at 8:21 AM Bert Gunter  wrote:

> I disagree. I see "formals() <- "  as a way to compute on **functions**,
> which are first class *objects* in R, as in any functional programming
> language. This is not computing on the language  as I understand the term
> -- substitute() and friends are, i.e. anything where eval() is needed to
> get a result.
>
> Cheers,
> Bert
>
>
> On Sun, Oct 26, 2025 at 7:36 AM Richard O'Keefe  wrote:
>
>> 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  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
>> >
>> > __
>> > [email protected] 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.
>>
>> __
>> [email protected] 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]]

__
[email protected] 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.


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

2025-10-26 Thread Bert Gunter
I disagree. I see "formals() <- "  as a way to compute on **functions**,
which are first class *objects* in R, as in any functional programming
language. This is not computing on the language  as I understand the term
-- substitute() and friends are, i.e. anything where eval() is needed to
get a result.

Cheers,
Bert


On Sun, Oct 26, 2025 at 7:36 AM Richard O'Keefe  wrote:

> 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  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
> >
> > __
> > [email protected] 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.
>
> __
> [email protected] 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]]

__
[email protected] 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.


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

2025-10-26 Thread Gabor Grothendieck
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
 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  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
> >
> > __
> > [email protected] 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

__
[email protected] 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.


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

2025-10-26 Thread Gabor Grothendieck
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  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
>
> __
> [email protected] 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

__
[email protected] 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.


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

2025-10-26 Thread Richard O'Keefe
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  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
>
> __
> [email protected] 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.

__
[email protected] 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.


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

2025-10-25 Thread Rolf Turner


On Sat, 25 Oct 2025 06:24:48 -0400
Duncan Murdoch  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

__
[email protected] 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.


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

2025-10-25 Thread Duncan Murdoch
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  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



__
[email protected] 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.


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

2025-10-24 Thread Rolf Turner


On Fri, 24 Oct 2025 22:31:53 -0400
Iris Simmons  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

-- 
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

__
[email protected] 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.


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

2025-10-24 Thread Iris Simmons
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  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
>
> __
> [email protected] 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]]

__
[email protected] 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.