function(…) – function ellipsis

function(…)
Pass arguments to another function, which is especially useful for creating customized versions of existing functions or in providing additional options to end-users.
  • – Include three consecutive periods at the end of a function declaration. Also include three periods in at the end of the argument list for the function to pass arguments to.
The ellipsis is just a special argument, so be sure to include a comma between any other arguments and the ellipsis.

Example. The myhist function below closely recreates the hist function, except that there is a blue border for the histogram boxes instead of the default (black). Extra arguments in myhist are passed to the hist function.
> myhist <- function(x, border="blue", ...){
+ 	hist(x, border=border, ...)
+ }
> set.seed(5)
> myhist(rnorm(1000), breaks=30)
> set.seed(5)
> myhist(rnorm(1000))  
In the first call of myhist, the additional breaks argument is passed to the hist function by means of the ellipsis (...), which is why there are about twice as many bins in the first histogram than when using the defaults in the second histogram.
Tip. It is a good idea to specify the argument names when passing variables using the ellipsis. You can pass them without specifying their names, but this assumes there will be no changes to arguments in either function in the future, which is often unrealistic.

Leave a Reply