[R] Bug report: override stopifnot() ?

Stepan Kasal kasal at ucw.cz
Mon Mar 12 18:56:40 CET 2018


Hello,
I'm sorry that I aswer to my own mail; I forgot to attach the patch.
Patch below,
   Stepan Kasal

On Mon, Mar 12, 2018 at 06:53:00PM +0100, Stepan Kasal wrote:
> When thinking about it, stopifnot() should really issue a better error
> message in this case.  Patch attached.  But I should perhaps send
> it also to R-devel.


--- stopifnot-orig.r	2018-03-12 18:49:01.439484100 +0100
+++ stopifnot.r	2018-03-12 18:48:55.721846700 +0100
@@ -1,16 +1,20 @@
-function (...)
+function (...) 
 {
   n <- length(ll <- list(...))
-  if (n == 0L)
+  if (n == 0L) 
     return(invisible())
   mc <- match.call()
-  for (i in 1L:n) if (!(is.logical(r <- ll[[i]]) && !anyNA(r) &&
+  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)
+    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)
+    if (is.logical(r)) {
+      msg <- ngettext(length(r), "%s is not TRUE", "%s are not all TRUE")
+    } else {
+      msg <- gettext("%s is not of type \"logical\"")
+    }
+    stop(sprintf(msg, ch), call. = FALSE, domain = NA)
   }
   invisible()
 }



More information about the R-help mailing list