R … three dots ellipsis

[This article was first published on ipub » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

print(x, ...)

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:

print.data.frame <- function (x, ..., digits = NULL, quote = FALSE, right = TRUE,         
    row.names = TRUE)                                                 
{                                                                     
    n <- length(row.names(x))                                         
    if (length(x) == 0L) {                                            
        #some other code...
    }                                                                 
    else {                                                            
        m <- as.matrix(format.data.frame(x, digits = digits,          
            na.encode = FALSE))                                       
        if (!isTRUE(row.names))                                       
            dimnames(m)[[1L]] <- if (identical(row.names, FALSE))     
                rep.int("", n)                                        
            else row.names                                            
        print(m, ..., quote = quote, right = right)                   
    }                                                                 
    invisible(x)                                                      
}

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:

HelloWorld <- function(...) {
  arguments <- list(...)
  paste(arguments)
}

HelloWorld("Hello", "World", "!")

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:

apply <- function (X, MARGIN, FUN, ...)                                        
{
  #...lots of code

      for (i in 1L:d2) {                                            
            tmp <- FUN(newX[, i], ...)                                
            if (!is.null(tmp))                                        
                ans[[i]] <- tmp                                       
        }       
  # ...more code
}

 Advanced Stuff

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

do.call
 method.

The post R … three dots ellipsis appeared first on ipub.

To leave a comment for the author, please follow the link and comment on their blog: ipub » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)