[Rd] length of `...`

Tóth Dénes toth@dene@ @ending from kogentum@hu
Thu May 3 17:10:41 CEST 2018


Thank you Bill (and the R-Core Team), this is even more than what I 
thought of. I somehow missed this in the NEWS.

BTW, substitue(...()) is beautiful.


On 05/03/2018 05:01 PM, William Dunlap 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 <http://tibco.com>
> 
> On Thu, May 3, 2018 at 7:29 AM, Dénes Tóth <toth.denes at kogentum.hu 
> <mailto: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 <mailto:R-devel at r-project.org> mailing list
>     https://stat.ethz.ch/mailman/listinfo/r-devel
>     <https://stat.ethz.ch/mailman/listinfo/r-devel>
> 
> 

-- 
Dr. Tóth Dénes ügyvezető
Kogentum Kft.
Tel.: 06-30-2583723
Web: www.kogentum.hu




More information about the R-devel mailing list