Git Product home page Git Product logo

ellipsis's Introduction

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:

  • check_dots_used() errors if any components of ... are not evaluated. This allows an S3 generic to state that it expects every input to be evaluated.

  • check_dots_unnamed() errors if any components of ... are named. This allows you to collect arbitrary unnamed arguments, warning if the user misspells a named argument.

  • check_dots_empty() errors if ... is used. This allows you to use ... to force the user to supply full argument names, while still warning if an argument name is misspelled.

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?

ellipsis's People

Contributors

batpigandme avatar dkahle avatar hadley avatar jimhester avatar jyuu avatar krlmlr avatar lionel- avatar noamross avatar yutannihilation avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.