# Checks against logical values using base::identical
assert_is_identical_to_true(TRUE)
assert_is_identical_to_false(FALSE)
assert_is_identical_to_na(NA)
# Other NA types match
assert_is_identical_to_na(NA_complex_)
# NaN is not NA
dont_stop(assert_is_identical_to_na(NaN))
# For a slightly less strict test, you can ignore attributes
assert_is_identical_to_true(c(truth = TRUE), allow_attributes = TRUE)
assert_is_identical_to_false(matrix(FALSE), allow_attributes = TRUE)
assert_is_identical_to_na(structure(NA, class = "nanana"), allow_attributes = TRUE)
# Vectorized predicates
x <- c(TRUE, FALSE, NA)
is_true(x)
is_false(x)
is_na(x)
# ...and their opposites
is_not_true(x)
is_not_false(x)
is_not_na(x)
# Check that at least one element fits the condition
assert_any_are_true(x)
assert_any_are_false(x)
assert_any_are_na(x)
# These tests should fail:
dont_stop(assert_is_identical_to_true(c(truth = TRUE)))
dont_stop(assert_is_identical_to_false(matrix(FALSE)))
dont_stop(assert_is_identical_to_na(structure(NA, class = "nanana")))
dont_stop(assert_all_are_true(x))
dont_stop(assert_all_are_false(x))
dont_stop(assert_all_are_na(x))
# base::is.na has non-standard behaviour for data.frames and lists.
# is_na and is_not_na coerce to logical vectors (except character input).
# unlist the input or use an apply function.
d <- data.frame(x = c(TRUE, FALSE, NA), y = c(0, NA, 2), z = c("a", "NA", NA))
is.na(d)
is_na(unlist(d))
Run the code above in your browser using DataLab