Skip to content
Prev 317385 / 398503 Next

an assert() function

On 12/02/2013 9:11 AM, Greg Minshall wrote:
stopifnot() might do what you want.

The tricky thing in writing this sort of function is handling vector 
inputs.  Sometimes x is a vector, and then you need to decide what to do 
with

assert(x > 0)

Yours will only test the first component and issue a warning; the base 
one will test all(x > 0).  But the base behaviour causes its own 
problems; one came up recently in that

stopifnot( ncol(x) > 1 )

will not stop if x is just a vector, because ncol(x) is NULL, so ncol(x) 
 > 1 is a length 0 vector, and all(ncol(x) > 1) is TRUE.

Duncan Murdoch