19.5 … (dot-dot-dot)

  • !!! is also useful in other places where you have a list of expressions you want to insert into a call.

  • Two motivating examples:

List of dataframes you want to rbind (a list of arbitrary length)

dfs <- list(
  a = data.frame(x = 1, y = 2),
  b = data.frame(x = 3, y = 4)
)

How to supply an argument name indirectly?

var <- "x"
val <- c(4, 3, 9)
  • For the first one, we can use unquote (splice) in `dplyr::bind_rows``
dplyr::bind_rows(!!!dfs)
#>   x y
#> 1 1 2
#> 2 3 4

This is known ‘splatting’ in some other langauges (Ruby, Go, Julia). Python calls this argument unpacking (**kwarg)

  • For the second we need to unquote the left side of an =. Tidy eval lets us do this with a special :=
tibble::tibble(!!var := val)
#> # A tibble: 3 × 1
#>       x
#>   <dbl>
#> 1     4
#> 2     3
#> 3     9
  • Functions that have these capabilities are said to have tidy dots (or apparently now it is called dynamic dots). To get this capability in your own functions, use list2!