Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

dyn-dots

Dynamic dots


Description

The ... syntax of base R allows you to:

  • Forward arguments from function to function, matching them along the way to function parameters.

  • Collect arguments inside data structures, e.g. with c() or list().

Dynamic dots offer a few additional features:

  1. You can splice arguments saved in a list with the big bang operator !!!.

  2. You can unquote names by using the glue syntax or the bang bang operator !! on the left-hand side of :=.

  3. Trailing commas are ignored, making it easier to copy and paste lines of arguments.

Add dynamic dots support in your functions

If your function takes dots, adding support for dynamic features is as easy as collecting the dots with list2() instead of list().

Other dynamic dots collectors are dots_list(), which is more configurable than list2(), vars() which doesn't force its arguments, and call2() for creating calls.

Document dynamic docs using this standard tag:

@param ... <[`dynamic-dots`][rlang::dyn-dots]> What these dots do.

Examples

f <- function(...) {
  out <- list2(...)
  rev(out)
}

# Splice
x <- list(alpha = "first", omega = "last")
f(!!!x)

# Unquote a name, showing both the `!!` bang bang and `{}` glue style
nm <- "key"
f(!!nm := "value")
f("{nm}" := "value")
f("prefix_{nm}" := "value")

# Tolerate a trailing comma
f(this = "that", )

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.