. . .

“dots”

dplyr::select(.data, ...)

One or more unquoted expressions separated by commas. Variable names can be used as if they were positions in the data frame, so expressions like x:y can be used to select a range of variables.

dplyr::mutate(.data, ...)

Name-value pairs. The name gives the name of the column in the output.

paste(...)

one or more R objects, to be converted to character vectors.

lapply(x, FUN, ...)

optional arguments to FUN

what is ... actually doing?

dots pass arguments

2 primary uses

  • passing arguments from one function to another
  • with S3 methods

pass args with ...

print_dots <- function(...) {
}

print_dots <- function(...) {
  paste(...)
}

print_dots <- function(...) {
  paste(...)
}

print_dots("hello", "world")
[1] "hello world"

print_dots("my favorite letter is", letters[1:10])
 [1] "my favorite letter is a" "my favorite letter is b"
 [3] "my favorite letter is c" "my favorite letter is d"
 [5] "my favorite letter is e" "my favorite letter is f"
 [7] "my favorite letter is g" "my favorite letter is h"
 [9] "my favorite letter is i" "my favorite letter is j"

a real example

sfdep::local_g
function (x, nb, wt, alternative = "two.sided", ...) 
{
    if (!is.null(attr(nb, "self.included"))) {
        cli::cli_alert_warning("attr `self.include` is `TRUE`. Reporting Gi*.")
    }
    listw <- recreate_listw(nb, wt)
    res <- spdep::localG(x, listw, alternative = alternative, 
        ...)
    unclass(res)
}
<bytecode: 0x15555c720>
<environment: namespace:sfdep>

function(x, nb, wt, alternative = "two.sided", ...) {
  listw <- recreate_listw(nb, wt)
  res <- spdep::localG(x, listw, alternative = alternative, ...)
  unclass(res)
}

spdep::localG(x, listw)
 [1]  0.68466144 -0.40605202  0.07383123  2.38196237  3.49570413 -0.75967568
 [7] -1.47075897 -0.80197945  0.63984614  3.68140093  2.71055652 -1.42476463
[13] -1.52840421 -1.18797787 -0.40025085 -0.94197599 -1.42699800 -1.27442420
[19]  0.95260051 -1.11613385 -0.87305053  1.53226882 -1.30140672 -0.48257276
[25] -0.99299807  1.19613531  3.43442369  3.00599480 -0.13401838 -0.62625063
[31]  0.10448258  1.96469389  1.24368535  1.24089584  0.63585914  0.59413290
[37] -0.11850431  0.01405564 -0.02251821 -1.39782829 -0.06004508  0.44914259
attr(,"internals")
              Gi      E(Gi)        V(Gi)       Z(Gi) Pr(z != E(Gi))
 [1,] 0.03410220 0.02439024 2.012155e-04  0.68466144   0.4935575875
 [2,] 0.02123678 0.02439024 6.031328e-05 -0.40605202   0.6847043809
 [3,] 0.02589994 0.02439024 4.181159e-04  0.07383123   0.9411446778
 [4,] 0.04301926 0.02439024 6.116597e-05  2.38196237   0.0172206570
 [5,] 0.06901709 0.02439024 1.629758e-04  3.49570413   0.0004728126
 [6,] 0.01581567 0.02439024 1.273997e-04 -0.75967568   0.4474484718
 [7,] 0.01428647 0.02439024 4.719369e-05 -1.47075897   0.1413563133
 [8,] 0.01886835 0.02439024 4.740770e-05 -0.80197945   0.4225648455
 [9,] 0.02930272 0.02439024 5.894531e-05  0.63984614   0.5222726301
[10,] 0.04686388 0.02439024 3.726665e-05  3.68140093   0.0002319559
[11,] 0.05558392 0.02439024 1.324391e-04  2.71055652   0.0067170399
[12,] 0.01060169 0.02439024 9.365938e-05 -1.42476463   0.1542252496
[13,] 0.01286235 0.02439024 5.688833e-05 -1.52840421   0.1264122108
[14,] 0.01283815 0.02439024 9.455934e-05 -1.18797787   0.2348421270
[15,] 0.01651766 0.02439024 3.868745e-04 -0.40025085   0.6889717664
[16,] 0.01642493 0.02439024 7.150328e-05 -0.94197599   0.3462049302
[17,] 0.01218072 0.02439024 7.320670e-05 -1.42699800   0.1535804724
[18,] 0.01203614 0.02439024 9.397114e-05 -1.27442420   0.2025131206
[19,] 0.03801004 0.02439024 2.044181e-04  0.95260051   0.3407925210
[20,] 0.01170106 0.02439024 1.292513e-04 -1.11613385   0.2643648453
[21,] 0.01452569 0.02439024 1.276662e-04 -0.87305053   0.3826355348
[22,] 0.03735296 0.02439024 7.156855e-05  1.53226882   0.1254561124
[23,] 0.01333262 0.02439024 7.219342e-05 -1.30140672   0.1931192740
[24,] 0.01771664 0.02439024 1.912473e-04 -0.48257276   0.6293991246
[25,] 0.01313099 0.02439024 1.285650e-04 -0.99299807   0.3207108935
[26,] 0.03220138 0.02439024 4.264500e-05  1.19613531   0.2316437614
[27,] 0.05423393 0.02439024 7.550872e-05  3.43442369   0.0005938152
[28,] 0.05030228 0.02439024 7.430649e-05  3.00599480   0.0026471353
[29,] 0.02308668 0.02439024 9.460981e-05 -0.13401838   0.8933880369
[30,] 0.01561661 0.02439024 1.962737e-04 -0.62625063   0.5311505602
[31,] 0.02513299 0.02439024 5.053475e-05  0.10448258   0.9167863921
[32,] 0.04314622 0.02439024 9.113587e-05  1.96469389   0.0494496780
[33,] 0.03833909 0.02439024 1.257928e-04  1.24368535   0.2136153893
[34,] 0.04203957 0.02439024 2.022951e-04  1.24089584   0.2146442303
[35,] 0.02933855 0.02439024 6.056084e-05  0.63585914   0.5248682387
[36,] 0.03105930 0.02439024 1.259972e-04  0.59413290   0.5524232308
[37,] 0.02348303 0.02439024 5.860676e-05 -0.11850431   0.9056680825
[38,] 0.02452575 0.02439024 9.294972e-05  0.01405564   0.9887855902
[39,] 0.02407140 0.02439024 2.004846e-04 -0.02251821   0.9820345879
[40,] 0.01226231 0.02439024 7.527763e-05 -1.39782829   0.1621646378
[41,] 0.02356882 0.02439024 1.871478e-04 -0.06004508   0.9521197274
[42,] 0.03326650 0.02439024 3.905635e-04  0.44914259   0.6533288014
attr(,"cluster")
 [1] High High High High High Low  Low  Low  Low  High High Low  Low  Low  Low 
[16] Low  Low  Low  High Low  Low  Low  Low  Low  Low  High High High Low  Low 
[31] High Low  Low  High High Low  Low  Low  Low  High Low  Low 
Levels: Low High
attr(,"gstari")
[1] FALSE
attr(,"call")
spdep::localG(x = x, listw = listw)
attr(,"class")
[1] "localG"

sfdep::local_g(x, nb, wt)
 [1]  0.68466144 -0.40605202  0.07383123  2.38196237  3.49570413 -0.75967568
 [7] -1.47075897 -0.80197945  0.63984614  3.68140093  2.71055652 -1.42476463
[13] -1.52840421 -1.18797787 -0.40025085 -0.94197599 -1.42699800 -1.27442420
[19]  0.95260051 -1.11613385 -0.87305053  1.53226882 -1.30140672 -0.48257276
[25] -0.99299807  1.19613531  3.43442369  3.00599480 -0.13401838 -0.62625063
[31]  0.10448258  1.96469389  1.24368535  1.24089584  0.63585914  0.59413290
[37] -0.11850431  0.01405564 -0.02251821 -1.39782829 -0.06004508  0.44914259
attr(,"internals")
              Gi      E(Gi)        V(Gi)       Z(Gi) Pr(z != E(Gi))
 [1,] 0.03410220 0.02439024 2.012155e-04  0.68466144   0.4935575875
 [2,] 0.02123678 0.02439024 6.031328e-05 -0.40605202   0.6847043809
 [3,] 0.02589994 0.02439024 4.181159e-04  0.07383123   0.9411446778
 [4,] 0.04301926 0.02439024 6.116597e-05  2.38196237   0.0172206570
 [5,] 0.06901709 0.02439024 1.629758e-04  3.49570413   0.0004728126
 [6,] 0.01581567 0.02439024 1.273997e-04 -0.75967568   0.4474484718
 [7,] 0.01428647 0.02439024 4.719369e-05 -1.47075897   0.1413563133
 [8,] 0.01886835 0.02439024 4.740770e-05 -0.80197945   0.4225648455
 [9,] 0.02930272 0.02439024 5.894531e-05  0.63984614   0.5222726301
[10,] 0.04686388 0.02439024 3.726665e-05  3.68140093   0.0002319559
[11,] 0.05558392 0.02439024 1.324391e-04  2.71055652   0.0067170399
[12,] 0.01060169 0.02439024 9.365938e-05 -1.42476463   0.1542252496
[13,] 0.01286235 0.02439024 5.688833e-05 -1.52840421   0.1264122108
[14,] 0.01283815 0.02439024 9.455934e-05 -1.18797787   0.2348421270
[15,] 0.01651766 0.02439024 3.868745e-04 -0.40025085   0.6889717664
[16,] 0.01642493 0.02439024 7.150328e-05 -0.94197599   0.3462049302
[17,] 0.01218072 0.02439024 7.320670e-05 -1.42699800   0.1535804724
[18,] 0.01203614 0.02439024 9.397114e-05 -1.27442420   0.2025131206
[19,] 0.03801004 0.02439024 2.044181e-04  0.95260051   0.3407925210
[20,] 0.01170106 0.02439024 1.292513e-04 -1.11613385   0.2643648453
[21,] 0.01452569 0.02439024 1.276662e-04 -0.87305053   0.3826355348
[22,] 0.03735296 0.02439024 7.156855e-05  1.53226882   0.1254561124
[23,] 0.01333262 0.02439024 7.219342e-05 -1.30140672   0.1931192740
[24,] 0.01771664 0.02439024 1.912473e-04 -0.48257276   0.6293991246
[25,] 0.01313099 0.02439024 1.285650e-04 -0.99299807   0.3207108935
[26,] 0.03220138 0.02439024 4.264500e-05  1.19613531   0.2316437614
[27,] 0.05423393 0.02439024 7.550872e-05  3.43442369   0.0005938152
[28,] 0.05030228 0.02439024 7.430649e-05  3.00599480   0.0026471353
[29,] 0.02308668 0.02439024 9.460981e-05 -0.13401838   0.8933880369
[30,] 0.01561661 0.02439024 1.962737e-04 -0.62625063   0.5311505602
[31,] 0.02513299 0.02439024 5.053475e-05  0.10448258   0.9167863921
[32,] 0.04314622 0.02439024 9.113587e-05  1.96469389   0.0494496780
[33,] 0.03833909 0.02439024 1.257928e-04  1.24368535   0.2136153893
[34,] 0.04203957 0.02439024 2.022951e-04  1.24089584   0.2146442303
[35,] 0.02933855 0.02439024 6.056084e-05  0.63585914   0.5248682387
[36,] 0.03105930 0.02439024 1.259972e-04  0.59413290   0.5524232308
[37,] 0.02348303 0.02439024 5.860676e-05 -0.11850431   0.9056680825
[38,] 0.02452575 0.02439024 9.294972e-05  0.01405564   0.9887855902
[39,] 0.02407140 0.02439024 2.004846e-04 -0.02251821   0.9820345879
[40,] 0.01226231 0.02439024 7.527763e-05 -1.39782829   0.1621646378
[41,] 0.02356882 0.02439024 1.871478e-04 -0.06004508   0.9521197274
[42,] 0.03326650 0.02439024 3.905635e-04  0.44914259   0.6533288014
attr(,"cluster")
 [1] High High High High High Low  Low  Low  Low  High High Low  Low  Low  Low 
[16] Low  Low  Low  High Low  Low  Low  Low  Low  Low  High High High Low  Low 
[31] High Low  Low  High High Low  Low  Low  Low  High Low  Low 
Levels: Low High
attr(,"gstari")
[1] FALSE
attr(,"call")
spdep::localG(x = x, listw = listw, alternative = alternative)

args(spdep::localG)
function (x, listw, zero.policy = NULL, spChk = NULL, GeoDa = FALSE, 
    alternative = "two.sided", return_internals = TRUE) 
NULL

sfdep::local_g(x, nb, wt, return_internals = TRUE)
 [1]  0.68466144 -0.40605202  0.07383123  2.38196237  3.49570413 -0.75967568
 [7] -1.47075897 -0.80197945  0.63984614  3.68140093  2.71055652 -1.42476463
[13] -1.52840421 -1.18797787 -0.40025085 -0.94197599 -1.42699800 -1.27442420
[19]  0.95260051 -1.11613385 -0.87305053  1.53226882 -1.30140672 -0.48257276
[25] -0.99299807  1.19613531  3.43442369  3.00599480 -0.13401838 -0.62625063
[31]  0.10448258  1.96469389  1.24368535  1.24089584  0.63585914  0.59413290
[37] -0.11850431  0.01405564 -0.02251821 -1.39782829 -0.06004508  0.44914259
attr(,"internals")
              Gi      E(Gi)        V(Gi)       Z(Gi) Pr(z != E(Gi))
 [1,] 0.03410220 0.02439024 2.012155e-04  0.68466144   0.4935575875
 [2,] 0.02123678 0.02439024 6.031328e-05 -0.40605202   0.6847043809
 [3,] 0.02589994 0.02439024 4.181159e-04  0.07383123   0.9411446778
 [4,] 0.04301926 0.02439024 6.116597e-05  2.38196237   0.0172206570
 [5,] 0.06901709 0.02439024 1.629758e-04  3.49570413   0.0004728126
 [6,] 0.01581567 0.02439024 1.273997e-04 -0.75967568   0.4474484718
 [7,] 0.01428647 0.02439024 4.719369e-05 -1.47075897   0.1413563133
 [8,] 0.01886835 0.02439024 4.740770e-05 -0.80197945   0.4225648455
 [9,] 0.02930272 0.02439024 5.894531e-05  0.63984614   0.5222726301
[10,] 0.04686388 0.02439024 3.726665e-05  3.68140093   0.0002319559
[11,] 0.05558392 0.02439024 1.324391e-04  2.71055652   0.0067170399
[12,] 0.01060169 0.02439024 9.365938e-05 -1.42476463   0.1542252496
[13,] 0.01286235 0.02439024 5.688833e-05 -1.52840421   0.1264122108
[14,] 0.01283815 0.02439024 9.455934e-05 -1.18797787   0.2348421270
[15,] 0.01651766 0.02439024 3.868745e-04 -0.40025085   0.6889717664
[16,] 0.01642493 0.02439024 7.150328e-05 -0.94197599   0.3462049302
[17,] 0.01218072 0.02439024 7.320670e-05 -1.42699800   0.1535804724
[18,] 0.01203614 0.02439024 9.397114e-05 -1.27442420   0.2025131206
[19,] 0.03801004 0.02439024 2.044181e-04  0.95260051   0.3407925210
[20,] 0.01170106 0.02439024 1.292513e-04 -1.11613385   0.2643648453
[21,] 0.01452569 0.02439024 1.276662e-04 -0.87305053   0.3826355348
[22,] 0.03735296 0.02439024 7.156855e-05  1.53226882   0.1254561124
[23,] 0.01333262 0.02439024 7.219342e-05 -1.30140672   0.1931192740
[24,] 0.01771664 0.02439024 1.912473e-04 -0.48257276   0.6293991246
[25,] 0.01313099 0.02439024 1.285650e-04 -0.99299807   0.3207108935
[26,] 0.03220138 0.02439024 4.264500e-05  1.19613531   0.2316437614
[27,] 0.05423393 0.02439024 7.550872e-05  3.43442369   0.0005938152
[28,] 0.05030228 0.02439024 7.430649e-05  3.00599480   0.0026471353
[29,] 0.02308668 0.02439024 9.460981e-05 -0.13401838   0.8933880369
[30,] 0.01561661 0.02439024 1.962737e-04 -0.62625063   0.5311505602
[31,] 0.02513299 0.02439024 5.053475e-05  0.10448258   0.9167863921
[32,] 0.04314622 0.02439024 9.113587e-05  1.96469389   0.0494496780
[33,] 0.03833909 0.02439024 1.257928e-04  1.24368535   0.2136153893
[34,] 0.04203957 0.02439024 2.022951e-04  1.24089584   0.2146442303
[35,] 0.02933855 0.02439024 6.056084e-05  0.63585914   0.5248682387
[36,] 0.03105930 0.02439024 1.259972e-04  0.59413290   0.5524232308
[37,] 0.02348303 0.02439024 5.860676e-05 -0.11850431   0.9056680825
[38,] 0.02452575 0.02439024 9.294972e-05  0.01405564   0.9887855902
[39,] 0.02407140 0.02439024 2.004846e-04 -0.02251821   0.9820345879
[40,] 0.01226231 0.02439024 7.527763e-05 -1.39782829   0.1621646378
[41,] 0.02356882 0.02439024 1.871478e-04 -0.06004508   0.9521197274
[42,] 0.03326650 0.02439024 3.905635e-04  0.44914259   0.6533288014
attr(,"cluster")
 [1] High High High High High Low  Low  Low  Low  High High Low  Low  Low  Low 
[16] Low  Low  Low  High Low  Low  Low  Low  Low  Low  High High High Low  Low 
[31] High Low  Low  High High Low  Low  Low  Low  High Low  Low 
Levels: Low High
attr(,"gstari")
[1] FALSE
attr(,"call")
spdep::localG(x = x, listw = listw, alternative = alternative, 
    return_internals = TRUE)

capture

dots <- function(...) {
  list(...)
}

dots <- function(...) {
  list(...)
}

dots(hello = "world")
$hello
[1] "world"

dots("hello", "world")
[[1]]
[1] "hello"

[[2]]
[1] "world"

use case

crafting data frames

data_frame <- function(...) {
  dots <- list(...)
  list2DF(dots)
}
data_frame(x = 10, y = 20)
   x  y
1 10 20

data_frame <- function(...) {
  dots <- list(...)
  dots_names <- names(dots)

  list2DF(dots)

}

data_frame <- function(...) {
  dots <- list(...)
  dots_names <- names(dots)

  if (is.null(dots_names) || "" %in% dots_names) {
    stop("all arguments must be named")
  }

  list2DF(dots)

}

data_frame(1:10, letters[1:10])
Error in data_frame(1:10, letters[1:10]): all arguments must be named
data_frame(id = 1:10, alphabet = letters[1:10])
   id alphabet
1   1        a
2   2        b
3   3        c
4   4        d
5   5        e
6   6        f
7   7        g
8   8        h
9   9        i
10 10        j

dots helpers

. . . rarely useful, but good to know!

reference by position

each argument as ..1, ..2, ..3, ..n

add2 <- function(...) {
  ..1 + ..2
}
add2(3, .14)
[1] 3.14

...length()

how many arguments?

nargs <- function(...) {
  ...length()
}
nargs(1, 2, 3, 4, 5)
[1] 5

arg n

...elt(n)

first and last args

first_last <- function(...) {
 arg1 <- ..1 
 n <- ...length()
 last_arg <- ...elt(n)
 
 list(arg1, last_arg)
}
first_last("first argument", 2, NA, Inf)
[[1]]
[1] "first argument"

[[2]]
[1] Inf

WARNING:

unused dots are “swallowed”

add2(3, 0.14, 10)
[1] 3.14

check yer dots

r-lib/ellipsis: Tools for Working with …

ellipsis::check_dots_used()

  • error if any arguments passed to dots aren’t used

add2 <- function(...) {
  ellipsis::check_dots_used()
 
  ..1 + ..2
}

add2(3, 0.14)
[1] 3.14
add2(3, 0.14, 0.0015)
Error in `add2()`:
! Arguments in `...` must be used.
✖ Problematic argument:
• ..1 = 0.0015
ℹ Did you misspell an argument name?

{ellipsis} helpers

  • check_dots_empty()
  • check_dots_unnamed()

data_frame <- function(...) {
  dots <- list(...)
  ellipsis::check_dots_empty()
  
  list2DF(dots)
}
data_frame(y = pi, exp(1))
Error in `data_frame()`:
! `...` must be empty.
✖ Problematic arguments:
• y = pi
• ..2 = exp(1)
ℹ Did you forget to name an argument?

but wait, there’s more!

but that’s for a later time

{rlang} dynamic dots

https://rlang.r-lib.org/reference/dyn-dots.html

recap

Dots can:

  • pass arguments from one function to another
  • be collected using list()
  • be sneaky and “swallow” arguments

You should check them with {ellipsis}

what’s next?

thanks!