Skip to contents

Checks if an argument is a logical scalar

Usage

assert_logical_scalar(arg, optional = FALSE)

Arguments

arg

A function argument to be checked

optional

Is the checked argument optional?

If set to FALSE and arg is NULL then an error is thrown. Otherwise, NULL is considered as valid value.

Value

The function throws an error if arg is neither TRUE or FALSE. Otherwise, the input is returned invisibly.

Examples

example_fun <- function(flag) {
  assert_logical_scalar(flag)
}

example_fun(FALSE)

try(example_fun(NA))
#> Error in assert_logical_scalar(flag) : 
#>   `flag` must be either `TRUE` or `FALSE` but is `NA`

try(example_fun(c(TRUE, FALSE, FALSE)))
#> Error in assert_logical_scalar(flag) : 
#>   `flag` must be either `TRUE` or `FALSE` but is a logical vector

try(example_fun(1:10))
#> Error in assert_logical_scalar(flag) : 
#>   `flag` must be either `TRUE` or `FALSE` but is an integer vector