Search code examples
rellipsis

How to check if any arguments were passed via "..." (ellipsis) in R? Is missing(...) valid?


I'd like to check whether an R function's "..." (ellipsis) parameter has been fed with some values/arguments.

Currently I'm using something like:

test1 <- function(...) {
   if (missing(...)) TRUE
   else FALSE
}

test1()
## [1] TRUE
test1(something)
## [2] FALSE

It works, but ?missing doesn't indicate if that way is proper/valid.

If the above is not correct, what is THE way to do so? Or maybe there are other, faster ways? PS. I need this kind of verification for this issue.


Solution

  • Here's an alternative that will throw an error if you try to pass in a non-existent object.

    test2 <- function(...) if(length(list(...))) FALSE else TRUE
    
    test2()
    #[1] TRUE
    test2(something)
    #Error in test2(something) : object 'something' not found
    test2(1)
    #[1] FALSE