0
mean(x, ...)
...
further arguments passed to or from other methods.

?"..."

The “...” argument in R functions is treated specially, in that it matches zero, one or more actual arguments (and so, objects). A mechanism has been added to R to allow “...” as the signature of a generic function. Methods defined for such functions will be selected and called when all the arguments matching “...” are from the specified class or from some subclass of that class.

can you give me a meaningful and simple example to help me understand the effect of ... here?

showkey
  • 482
  • 42
  • 140
  • 295
  • http://rfunction.com/archives/73 – Roland Nov 29 '13 at 13:09
  • @CarlWitthoft The question that you linked to is about `.`, not `...`. – Richie Cotton Nov 29 '13 at 13:12
  • Read these questions: http://stackoverflow.com/questions/5890576/usage-of-three-dots-or-dot-dot-dot-in-functions?lq=1 and http://stackoverflow.com/questions/3057341/how-to-use-rs-ellipsis-feature-when-writing-your-own-function and http://stackoverflow.com/questions/17688938/what-does-passing-an-ellipsis-as-an-argument-mean-in-r?lq=1 and see if you still need more help. – Richie Cotton Nov 29 '13 at 13:18
  • @RichieCotton Sorry-- I plead pre-caffeination. – Carl Witthoft Nov 29 '13 at 13:58

2 Answers2

1

You can use ... to pass addition configuration arguments to methods that might need them.

 print2 <- function (x, ...) {
     UseMethod('print2', x)
 }
 print2.integer <- function (x, ...) {
     cat(x)
 }

 print2.character <- function (x, ...) {
     config <- list(...)

     # an option to convert to uppercase before printing
     if (isTRUE(config$uppercase)) {
         cat(toupper(x))
     } else {
         cat(x)
     }
 }

# no config options for integers
print2(1L)
  1

# the string method does have configuration
print2(c('a', 'b'))
  a b
print2(c('a', 'b'), uppercase = TRUE)
  A B

Adding ... to generic functions is not essential, but it can make individual methods more configurable. If you need clarification, leave a comment below.

Róisín Grannell
  • 2,048
  • 1
  • 19
  • 31
  • I need a clarification for `?variable.names`. What do here "..." means? Further arguments passed to or from other methods? I do not know what is meant by this sentence and I do not know what can be passed there. Can you help me? – scarface Jan 27 '18 at 20:58
0

These have a few uses. The first is that in R functions have named arguments, so in a function like sum the argument are '...' and 'na.rm'. This means were a function would normally take the first input to be the first argument and the second to be the second you can give it something different. You can saysum(1, 2, c(1, 2, 3)). Without that you would have to package everything up before the function call. The second and more valuable use for the ellipsis is in the construction of functions. I have created a function in the pass that adds some checks around reading data. It would basically rewrite the call to read.csv in a manner that would not fail, the file was always being changed, so my code would not have to change. It would read the first line and look for the fields I said I wanted, and would place them in the order it found from reading just the header and NULL all other fields. I wanted to use it more places and it never seemed to work because other aspects would change, the delimiter, number of rows to skip, etc. I would either have to put in my function definition every possible argument to read.csv that may not be the default or give it the '...'.

This means that if I had a file test.txt and I wanted the fields 'a', 'b' and 'c' I would say safe.read.csv('test.txt', c('a', 'b', 'c')) and this would pass rewrite to read.csv('test.txt', col.classes = c(NA, NA, numeric, NA, integer, integer)) or something to that extent. That all assumed the default sep argument. If that changed I had to redefine my function safe.read.csv('test.txt', c('a', 'b', 'c'), x = '|') and read.csv('test.txt', col.classes = c(NA, NA, numeric, NA, integer, integer), sep = x). If you define it with '...' though you can pass it any number of things and each function inside will look over the contents of '...' and apply them if they are used. SO my function would have no defined x argument it would instead have '...' and I would say function safe.read.csv('test.txt', c('a', 'b', 'c'), sep = '|') and when it came to read.csv it would pick that up. So then you could pass any argument to any nested function without explicitly adding it to your function arg list.

darrelkj
  • 142
  • 2