From m@rtin@morg@n @ending from ro@wellp@rk@org  Thu May  3 17:04:02 2018
From: m@rtin@morg@n @ending from ro@wellp@rk@org (Martin Morgan)
Date: Thu, 3 May 2018 11:04:02 -0400
Subject: [Rd] length of `...`
In-Reply-To: <CAF8bMcaRW88VdyXw-kmCa9dw-awBiNiU+j+nktLRACCj4grPdQ@mail.gmail.com>
References: <d0b68216-4922-2249-e5e6-1589345dc6d7@kogentum.hu>
 <CAF8bMcaRW88VdyXw-kmCa9dw-awBiNiU+j+nktLRACCj4grPdQ@mail.gmail.com>
Message-ID: <464bef71-68a2-65ba-045f-0576b2722834@roswellpark.org>

nargs() provides the number of arguments without evaluating them

 > f = function(x, ..., y) nargs()
 > f()
[1] 0
 > f(a=1, b=2)
[1] 2
 > f(1, a=1, b=2)
[1] 3
 > f(x=1, a=1, b=2)
[1] 3
 > f(stop())
[1] 1


On 05/03/2018 11:01 AM, William Dunlap via R-devel wrote:
> In R-3.5.0 you can use ...length():
>    > f <- function(..., n) ...length()
>    > f(stop("one"), stop("two"), stop("three"), n=7)
>    [1] 3
> 
> Prior to that substitute() is the way to go
>    > g <- function(..., n) length(substitute(...()))
>    > g(stop("one"), stop("two"), stop("three"), n=7)
>    [1] 3
> 
> R-3.5.0 also has the ...elt(n) function, which returns
> the evaluated n'th entry in ... , without evaluating the
> other ... entries.
>    > fn <- function(..., n) ...elt(n)
>    > fn(stop("one"), 3*5, stop("three"), n=2)
>    [1] 15
> 
> Prior to 3.5.0, eval the appropriate component of the output
> of substitute() in the appropriate environment:
>    > gn <- function(..., n) {
>    +   nthExpr <- substitute(...())[[n]]
>    +   eval(nthExpr, envir=parent.frame())
>    + }
>    > gn(stop("one"), environment(), stop("two"), n=2)
>    <environment: R_GlobalEnv>
> 
> 
> 
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> 
> On Thu, May 3, 2018 at 7:29 AM, D?nes T?th <toth.denes at kogentum.hu> wrote:
> 
>> Hi,
>>
>>
>> In some cases the number of arguments passed as ... must be determined
>> inside a function, without evaluating the arguments themselves. I use the
>> following construct:
>>
>> dotlength <- function(...) length(substitute(expression(...))) - 1L
>>
>> # Usage (returns 3):
>> dotlength(1, 4, something = undefined)
>>
>> How can I define a method for length() which could be called directly on
>> `...`? Or is it an intention to extend the base length() function to accept
>> ellipses?
>>
>>
>> Regards,
>> Denes
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 


This email message may contain legally privileged and/or...{{dropped:2}}




[Rd] length of `...`
Martin Morgan