Search code examples
rellipsis

R ellipsis - further arguments passed to or from other methods


I understand what dot-dot-dot means in general. I understand how to use it when I want to create my own function with unknown number of parameters.

I do not understand how it works for example at function variable.names(). When I execute ?variable.names, there is written the following:

... further arguments passed to or from other methods.

What does it really mean? I do not know what I can pass there. How and where will be these passed arguments used.


Solution

  • The ellipsis argument allows one to pass arguments to a downstream function. We'll illustrate with a simple R function as follows.

    testfunc <- function(aFunction,x,...) {
         aFunction(x,...)
    }
    aVector <- c(1,3,5,NA,7,9,11,32)
    
    # returns NA because aVector contains NA values
    testfunc(mean,aVector)
    
    # use ellipsis in testfunc to pass na.rm=TRUE to mean()
    testfunc(mean,aVector,na.rm=TRUE)
    

    ...and the output:

    > testfunc <- function(aFunction,x,...) {
    +      aFunction(x,...)
    + }
    > aVector <- c(1,3,5,NA,7,9,11,32)
    > 
    > # returns NA because aVector contains NA values
    > testfunc(mean,aVector)
    [1] NA
    > # use ellipsis in testfunc to pass na.rm=TRUE to mean()
    > testfunc(mean,aVector,na.rm=TRUE)
    [1] 9.714286