Today I found that some of my
stopifnot() tests are failing because the passed arguments evaluate to empty logical vectors.
stopifnot(iris$nosuchcolumn == 2) # passes without error
isTRUE(logical()) > FALSE stopifnot(logical()) # passes
stopifnot() passes even when this argument is not
But furthermore, the behaviour of the above is different with different types of empty vectors.
isTRUE(numeric()) > FALSE stopifnot(numeric()) # Error: numeric() are not all TRUE
However, to give details on why specifically this happens and why you're confused vs.
isTRUE()behavior, note that
stopifnot()checks for three things; the check is (where
ris the result of the expression you pass):
So, the only reason why
numeric()fails is because
numeric()is not "logical," as suggested by akrun. For this reason, you should avoid checks that may result in logical vectors of length 0, as suggested by r2evans.