ellipsis

Lifecycle: maturing CRAN status Travis build status Codecov test coverage

Adding ... to a function is a powerful technique because it allows you to accept any number of additional arguments. Unfortunately it comes with a big downside: any misspelled or extraneous arguments will be silently ignored. This package provides tools for making ... safer:

Thanks to Jenny Bryan for the idea, and Lionel Henry for the heart of the implementation.

Installation

Install the released version from CRAN:

install.packages("ellipsis")

Or the development version from GitHub:

devtools::install_github("r-lib/ellipsis")

Example

mean() is a little dangerous because you might expect it to work like sum():

sum(1, 2, 3, 4)
#> [1] 10
mean(1, 2, 3, 4)
#> [1] 1

This silently returns the incorrect result because mean() has arguments x and .... The ... silently swallows up the additional arguments. We can use ellipsis::check_dots_used() to check that every input to ... is actually used:

safe_mean <- function(x, ..., trim = 0, na.rm = FALSE) {
  ellipsis::check_dots_used()
  mean(x, ..., trim = trim, na.rm = na.rm)
}

safe_mean(1, 2, 3, 4)
#> Error: 3 components of `...` were not used.
#> 
#> We detected these problematic arguments:
#> * `..1`
#> * `..2`
#> * `..3`
#> 
#> Did you misspecify an argument?