[Rd] length of `...`

Martin Morgan m@rtin@morg@n @ending from ro@wellp@rk@org
Thu May 3 17:04:02 CEST 2018


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




More information about the R-devel mailing list