R … three dots ellipsis

If you have used R before, then you surely have come across the three dots, e.g.

In technical language, this is called an ellipsis. And it means that the function is designed to take any number of named or unnamed arguments.

By the way, the ellipsis is not a specialty of R. Other programming languages have the same or a similar concept. For example, C# has the params key-word, which is a type-safe variation of the three dots.

In the example of the print function, any argument passed after the initial x will simply be passed along to a subclass’ print function. For example, if you look at the generic print.data.frame , it has more arguments than the print.default  function. And it passes the ...  when it calls print function on the matrix:

The interesting question is: How do you write functions that make use of ellipsis? The answer is very simple: you simply convert the … to a list, like so:

So, when should I use the ellipsis construct?

That, again, is very simple: there are essentially two situations when you can use the three dots:

  1. When it makes sense to call the function with a variable number of arguments. See the HellowWorld  example above. Another very prominent example is the paste function.
  2. When, within your function, you call other functions, and these functions can have a variable number of arguments, either because
    1. the called function is generic (like in the above print example), or
    2. the called function can be passed into the function as an argument, as for example with the FUN argument in apply:

 Advanced Stuff

When you want to manipulate the … , and pass part of it to another recursive call, you can use the  do.call method.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.