[R] missing() fails to detect missing

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Apr 18 06:32:06 CEST 2024


Fascinating. Or, well, not.

Failing to use a method signature that is compatible with the generic is a no-no. So your bug seems to me to be outside the bounds of how R is supposed to be used. So don't do that.

On April 17, 2024 4:25:38 PM PDT, "Boylan, Ross via R-help" <r-help using r-project.org> wrote:
>When a generic (S4) has an argument with a default followed by ..., missing() doesn't seem to work if the method omits the ...
>--------------------Sample-------------------------------
>foo <- function(x, y=0, ...){
>  "you are very generic"
>}
>
># no ... in function arguments
>setMethod("foo", signature="character", function(x, y=0){
>  if (missing(y))
>    return("must give y for character")
>  y
>})
>
>setMethod("foo", signature="numeric", function(x, y=0, ...){
>  if (missing(y))
>    return("must give y for numeric")
>  y
>})
>
>print(foo("a"))  #[1] 0
>print(foo(0))    #[1] "must give y for numeric"
>------------------------------------------------------------------
>It's the result for foo("a") I'm puzzled by, since missing(y) does not evaluate to TRUE.
>
>Background
>==========
>The methods documentation has 2 points on which the above definitions may fail.
>
>1. The generic has regular arguments and ... arguments.  But dotsMethods docs say
>        > either the signature of the generic function is "..." only, or it
>        > does not contain "..."
>Since the arguments in ... are not part of the signatures I think I'm OK, but another reading is that
>one just shouldn't mix ... and other arguments.
>
>2. setMethod docs say
>    > The definition must be a function with the same formal arguments as the generic; however, setMethod() will handle methods that add arguments, if ... is a formal argument to the generic.
>Since the initial definition has arguments x, y, ... and the first method definition has only x, y, the arguments don't match.  So maybe that's the problem.
>
>I don't know if the fact that y has a default value matters.
>
>The real code has a function f that ordinarily requires an additional piece of information, y, to compute a result.  But for one class, the result doesn't depend on y and so that argument may be omitted.
>
>Context
>======
>R 4.3.3 on MS-Windows under RStudio 2023.12.1 build 402.
>
>Thanks for any insights.
>Ross
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list