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.