2

Imagine:

myfunct <- function(x, ...){
  dots <- list(...)
...
}

How do I distinguish in the course of the function whether dots derived from myfunct('something') (no dots) or myfunct('something', NULL) (dots includes explicit NULL)? In my experimentation both cases lead to is.null(dots) equating to TRUE.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
balin
  • 1,554
  • 1
  • 12
  • 26

2 Answers2

5

Does it help ?

f <- function(x, ...){
  missing(...)
}
> f(2)
[1] TRUE
> f(2, NULL)
[1] FALSE

g <- function(x, ...){
  length(list(...))
}
> g(2)
[1] 0
> g(2, NULL)
[1] 1
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
1

I eventually came up with the following:

myfunct <- function(...)
{
  my_dots <- match.call(expand.dots = FALSE)[['...']]
  no_dots <- is.null(my_dots)
  # Process the dots
  if(!no_dots)
  {
     my_dots <- lapply(my_dots, eval)
  }
  # Exemplary return
  return(my_dots)
}

This yields:

> myfunct(1)
[[1]]
[1] 1

> myfunct(NULL)
[[1]]
NULL

> myfunct()
NULL

> myfunct(1, NULL, 'A')
[[1]]
[1] 1

[[2]]
NULL

[[3]]
[1] "A"
balin
  • 1,554
  • 1
  • 12
  • 26