How to detect missing point-to-point in an argument to a generic function

Suppose I have a function like

z <- function(x, ...) { print(x) } 

If ... missing, the function should do one thing; if indicated ... , the function should do something else with it. In this case, I can use missing(...) to detect the absence .. But I did not find an elegant way to detect using common methods such as [.Obj(x, ...) , where it is missing ... still pairlist(<emptyname>) and missing(...) = FALSE , even if I do not give values.

Here are some experiments:

 z <- function(x, ...) { cat(missing(...)) } Obj <- function() { env <- environment() class(env) <- "Obj" env } `[.Obj` <- function(x,...) { cat(missing(...),"\n") } 

With the code above, evaluate the following:

 > z() TRUE > z(a=1) FALSE > Obj()[] FALSE NULL > Obj()[a=1] FALSE NULL 

However, in debug mode for Obj()[] in RStudio, this is similar to

 Browse[1]> list(...) Error: argument is missing, with no default Browse[1]> missing(...) [1] FALSE 

Somehow, the way z() works does not work for Obj()[] . Is there an elegant way with a little overhead to determine if ... missing for the case of [.Obj(x, ...) that I really encounter?

See also: http://r.789695.n4.nabble.com/Arguments-passing-through-dot-dot-dot-lose-ability-to-check-for-missing-td4656455.html

+5
source share
2 answers

Try to check the absence ..1 :

 > `[.Obj` <- function(x, ...) missing(..1) > Obj()[a=1] [1] FALSE > Obj()[] [1] TRUE 
+3
source

What about:

 `[.Obj` <- function(x,...) { length(as.list(match.call(expand.dots=TRUE))[-c(1,2)])==1L } 

Noting that for some functions (for example, [.data.frame ) an empty argument does not match an argument that is absent (i.e. absent).

0
source

Source: https://habr.com/ru/post/1200714/


All Articles