[R] stopifnot with logical(0)

Giorgio Garziano giorgio.garziano at ericsson.com
Fri Dec 11 16:38:56 CET 2015


I think the inspection of the "stopifnot()" source code may help.

> stopifnot
function (...)
{
    n <- length(ll <- list(...))
    if (n == 0L)
        return(invisible())
    mc <- match.call()
    for (i in 1L:n) if (!(is.logical(r <- ll[[i]]) && !anyNA(r) &&
        all(r))) {
        ch <- deparse(mc[[i + 1]], width.cutoff = 60L)
        if (length(ch) > 1L)
            ch <- paste(ch[1L], "....")
        stop(sprintf(ngettext(length(r), "%s is not TRUE", "%s are not all TRUE"),
            ch), call. = FALSE, domain = NA)
    }
    invisible()
}


The following code may help in understanding.

(arg <- (logical(0) == 1))
(arg <- (logical(0) == TRUE))
(arg <- (logical(0) == FALSE))

n <- length(ll <- list(arg))
n

if (n == 0L)
  return(invisible())

for (i in 1L:n) {
  print(is.logical(r <- ll[[i]]))
  print(!anyNA(r))
  print(all(r))
  if (! (is.logical(r <- ll[[i]]) && !anyNA(r) &&  all(r)) ) {
    print("stop")
  }
}

Executing such code:

> (arg <- (logical(0) == 1))
logical(0)
> (arg <- (logical(0) == TRUE))
logical(0)
> (arg <- (logical(0) == FALSE))
logical(0)
>
> n <- length(ll <- list(arg))
> n
[1] 1
>
> if (n == 0L)
+   return(invisible())
>
> for (i in 1L:n) {
+   print(is.logical(r <- ll[[i]]))
+   print(!anyNA(r))
+   print(all(r))
+   if (! (is.logical(r <- ll[[i]]) && !anyNA(r) &&  all(r)) ) {
+     print("stop")
+   }
+ }
[1] TRUE
[1] TRUE
[1] TRUE
>

See also help(all) and what its "Note" states about all(logical(0))
and consider that:

> is.logical(logical(0))
[1] TRUE


--
GG

	[[alternative HTML version deleted]]



More information about the R-help mailing list