Skip to content
Prev 19656 / 63424 Next

stopifnot() suggestion

On Wed, 1 Mar 2006, Roger D. Peng wrote:

            
I was thinking of a fallible R user accidentally testing the truth of an 
expression with NAs, rather than of a situation where you remember that 
there may be missing values. For example
Error in if (x != 4) stop("x should be 4") :
 	missing value where TRUE/FALSE needed
Error in if (!(is.logical(r <- eval(ll[[i]])) && all(r))) 
stop(paste(deparse(mc[[i +  :
 	missing value where TRUE/FALSE needed

If you write the error-checking code represented by f(), you get a message 
which is very helpful in correcting your error. But someone who uses 
stopifnot() instead gets the output of g(). Even a user who knows the 
origin of the code in the error message doesn't know which of several 
stopifnot()s is responsible.

Dan