inspect_true_or_false checks if an object is a non-missing logical vector of length 1. This can be useful to validate inputs in user-defined functions.

inspect_true_or_false(x)

Arguments

x

An arbitrary object.

Value

inspect_true_or_false does not return any output. There are two possible scenarios:

  • The call is silent if x is a non-missing logical vector of length 1.

  • An informative error message is thrown otherwise.

Details

inspect_true_or_false conducts a series of tests to check if x is a non-missing logical vector of length 1. Namely, inspect_true_or_false checks if:

  • x is NULL or empty.

  • x is an atomic vector of length 1.

  • The typeof x is logical.

  • x is NA or NaN.

See also

Examples

# Calls that pass silently:
x <- TRUE
y <- FALSE
inspect_true_or_false(x)
inspect_true_or_false(y)

# Calls that throw informative error messages:
mylist <- list(NULL, NA, NaN, 1, 0, "TRUE")
try(inspect_true_or_false(mylist[[1]]))
#> Error in inspect_true_or_false(mylist[[1]]) : 
#>   Invalid argument: mylist[[1]] is NULL.
try(inspect_true_or_false(mylist[[2]]))
#> Error in inspect_true_or_false(mylist[[2]]) : 
#>   Invalid argument: mylist[[2]] is NA or NaN.
try(inspect_true_or_false(mylist[[3]]))
#> Error in inspect_true_or_false(mylist[[3]]) : 
#>   Invalid argument: mylist[[3]] is NA or NaN.
try(inspect_true_or_false(mylist[[4]]))
#> Error in inspect_true_or_false(mylist[[4]]) : 
#>   Invalid argument: the type of mylist[[4]] must be logical.
try(inspect_true_or_false(mylist[[5]]))
#> Error in inspect_true_or_false(mylist[[5]]) : 
#>   Invalid argument: the type of mylist[[5]] must be logical.
try(inspect_true_or_false(mylist[[6]]))
#> Error in inspect_true_or_false(mylist[[6]]) : 
#>   Invalid argument: the type of mylist[[6]] must be logical.