R:从一种含NULL区分EMPTY省略号?

问题描述 投票:2回答:2

想像:

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

如何在功能的过程中区分点无论是从myfunct('something')(没有点)或myfunct('something', NULL)衍生(点包括明确NULL)?在我的实验两种情况导致is.null(dots)等同于TRUE

r ellipsis
2个回答
4
投票

帮助?

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

0
投票

我终于想出了以下内容:

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)
}

这产生了:

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

> myfunct(NULL)
[[1]]
NULL

> myfunct()
NULL

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

[[2]]
NULL

[[3]]
[1] "A"
© www.soinside.com 2019 - 2024. All rights reserved.