19.6 … (dot-dot-dot) [When using … with quoting]

  • Sometimes need to supply an arbitrary list of expressions or arguments in a function (...)
  • But need a way to use these when we don’t necessarily have the names
  • Remember !! and !!! only work with functions that use rlang
  • Can use list2(...) to turn ... into “tidy dots” which can be unquoted and spliced
  • Require list2() if going to be passing or using !! or !!! in ...
  • list2() is a wrapper around dots_list() with the most common defaults

No need for list2()

d <- function(...) data.frame(list(...))
d(x = c(1:3), y = c(2, 4, 6))
#>   x y
#> 1 1 2
#> 2 2 4
#> 3 3 6

Require list2()

vars <- list(x = c(1:3), y = c(2, 4, 6))
d(!!!vars)
#> Error in !vars: invalid argument type
d2 <- function(...) data.frame(list2(...))
d2(!!!vars)
#>   x y
#> 1 1 2
#> 2 2 4
#> 3 3 6
# Same result but x and y evaluated later
vars_expr <- exprs(x = c(1:3), y = c(2, 4, 6))
d2(!!!vars_expr)  
#>   x y
#> 1 1 2
#> 2 2 4
#> 3 3 6

Getting argument names (symbols) from variables

nm <- "z"
val <- letters[1:4]
d2(x = 1:4, !!nm := val)
#>   x z
#> 1 1 a
#> 2 2 b
#> 3 3 c
#> 4 4 d