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
So stopifnot()
passes even when this argument is not TRUE
.
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
上面是否有逻辑,还是应该将其视为错误?
akrun和r2evans的评论很明显。
However, to give details on why specifically this happens and why you're confused vs.
isTRUE()
behavior, note thatstopifnot()
checks for three things; the check is (wherer
is the result of the expression you pass):因此,让我们看一下:
So, the only reason why
logical()
passes whilenumeric()
fails is becausenumeric()
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.