Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

Truth

Is the input TRUE/FALSE/NA?


Description

Checks to see if the input is TRUE, FALSE or NA.

Usage

assert_is_identical_to_false(
  x,
  allow_attributes = FALSE,
  severity = getOption("assertive.severity", "stop")
)

assert_is_identical_to_na(
  x,
  allow_attributes = FALSE,
  severity = getOption("assertive.severity", "stop")
)

assert_is_identical_to_true(
  x,
  allow_attributes = FALSE,
  severity = getOption("assertive.severity", "stop")
)

assert_all_are_false(x, severity = getOption("assertive.severity", "stop"))

assert_any_are_false(x, severity = getOption("assertive.severity", "stop"))

assert_all_are_na(x, severity = getOption("assertive.severity", "stop"))

assert_any_are_na(x, severity = getOption("assertive.severity", "stop"))

assert_all_are_true(x, severity = getOption("assertive.severity", "stop"))

assert_any_are_true(x, severity = getOption("assertive.severity", "stop"))

assert_all_are_not_false(x, severity = getOption("assertive.severity", "stop"))

assert_any_are_not_false(x, severity = getOption("assertive.severity", "stop"))

assert_all_are_not_na(x, severity = getOption("assertive.severity", "stop"))

assert_any_are_not_na(x, severity = getOption("assertive.severity", "stop"))

assert_all_are_not_true(x, severity = getOption("assertive.severity", "stop"))

assert_any_are_not_true(x, severity = getOption("assertive.severity", "stop"))

is_identical_to_false(
  x,
  allow_attributes = FALSE,
  .xname = get_name_in_parent(x)
)

is_identical_to_na(x, allow_attributes = FALSE, .xname = get_name_in_parent(x))

is_identical_to_true(
  x,
  allow_attributes = FALSE,
  .xname = get_name_in_parent(x)
)

is_false(x, .xname = get_name_in_parent(x))

is_na(x, coerce_to_logical = FALSE, .xname = get_name_in_parent(x))

is_not_na(x, coerce_to_logical = FALSE, .xname = get_name_in_parent(x))

is_not_false(x, .xname = get_name_in_parent(x))

is_not_true(x, .xname = get_name_in_parent(x))

is_true(x, .xname = get_name_in_parent(x))

Arguments

x

Input to check. See note.

allow_attributes

If TRUE, a scalar value of TRUE with attributes is allowed.

severity

How severe should the consequences of the assertion be? Either "stop", "warning", "message", or "none".

.xname

Not intended to be used directly.

coerce_to_logical

Logical: should the input be coerced to logical before checking? See note.

Value

The is* functions return TRUE if the input is TRUE/FALSE. The assert_* functions return nothing but throw an error if the corresponding is_* function returns FALSE.

Note

is_identical_to_true wraps the base function isTRUE, providing more information on failure. Likewise, is_identical_to_false checks that the input is identical to FALSE. If allow_attributes is TRUE, a scalar value of TRUE with attributes is allowed. is_true and is_false are vectorized, returning TRUE when the inputs are TRUE and FALSE respectively.

The for is_true, is_false, is_not_true and is_not_false, x argument will be coerced to be a logical vector if it isn't already.

Coercion to logical is optional for is_na and is_not_na. If you do coerce, it means that is_na differs in behaviour from base::is.na for character vector, list and data frame inputs. To replicate the behaviour of is.na, ensure the argument coerce_to_logical is FALSE (this is the default).

Note that in assertive version 0.1-4 and prior, is_identical_to_true/false were named is_true/false and the vectorized versions were not present.

See Also

Examples

# 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 (package name explicitly given to prevent
# problems with testthat name clash)
x <- c(TRUE, FALSE, NA)
assertive.base::is_true(x)
assertive.base::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 checks should fail:
dont_stop({
  assert_is_identical_to_true(c(truth = TRUE))
  assert_is_identical_to_true(1)
  assert_is_identical_to_true(c(TRUE, TRUE))
  assert_is_identical_to_false(matrix(FALSE))
  assert_is_identical_to_na(structure(NA, class = "nanana"))
  assert_all_are_true(x)
  assert_all_are_false(x)
  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))

assertive.base

A Lightweight Core of the 'assertive' Package

v0.0-9
GPL (>= 3)
Authors
Richard Cotton [aut, cre], Sunkyu Choi [trl], Ivanka Skakun [trl], Gergely Dar<c3><b3>czi [trl], Anton Antonov [trl], Hisham Ben Hamidane [trl], Anja Billing [trl], Aditya Bhagwat [trl], Rasmus B<c3><a5><c3><a5>th [trl], Mine Cetinkaya-Rundel [trl], Aspasia Chatziefthymiou [trl]
Initial release
2021-02-07

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.