Skip to contents

Check if arg is a flag, a scalar logical that is TRUE or FALSE.

Usage

check_flag(arg, signal = "error", msg = NULL, call. = FALSE)

Arguments

arg

(object)
The argument to check.

signal

(string: c("error", "warning", "message"))
Must be one of "error", "warning", or "message". "error" (default) calls base::stop() to signal an error message. "warning" calls base::warning() to signal a warning message. "message" calls base::message() to signal a message.

msg

(string or NULL: NULL)
A string that replaces the default message. Set as NULL to use the default message.

call.

(Scalar logical: c(FALSE, TRUE))
Passed to stop() or warning(). The default msg includes it's own formatted call, hence call. should be FALSE (default). If you insert your own msg, then you may want call. = TRUE.

Value

invisible(arg) or condition determined by argument signal.

Details

Unlike check_scalar_logical(), a flag rejects NA. This matches the common case of a boolean toggle argument that must be decidably on or off.

Examples

#----------------------------------------------------------------------------
# check_flag() examples
#----------------------------------------------------------------------------
library(bkcheck)

f <- function(x) {
  x |>
    check_flag()
}

f(TRUE)
f(FALSE)

try(f(NA))
#> Error : Argument `x` must be a scalar logical that is `TRUE` or `FALSE`.
#> 
#> `x` = NA
#> class(x) = 'logical'
#> length(x) = 1
#> 
#> Call: f(x = NA)
try(f(1L))
#> Error : Argument `x` must be a scalar logical that is `TRUE` or `FALSE`.
#> 
#> `x` = 1
#> class(x) = 'integer'
#> length(x) = 1
#> 
#> Call: f(x = 1L)
try(f(c(TRUE, FALSE)))
#> Error : Argument `x` must be a scalar logical that is `TRUE` or `FALSE`.
#> 
#> `x` = TRUE, FALSE
#> class(x) = 'logical'
#> length(x) = 2
#> 
#> Call: f(x = c(TRUE, FALSE))
try(f("TRUE"))
#> Error : Argument `x` must be a scalar logical that is `TRUE` or `FALSE`.
#> 
#> `x` = 'TRUE'
#> class(x) = 'character'
#> length(x) = 1
#> 
#> Call: f(x = "TRUE")