Checks whether an argument is a logical vector (arg_logical()) or a logical scalar (arg_flag()), i.e., a single logical value. Logical values include TRUE and FALSE.
Usage
arg_logical(x, .arg = rlang::caller_arg(x), .msg = NULL, .call)
arg_flag(x, .arg = rlang::caller_arg(x), .msg = NULL, .call)Arguments
- x
the argument to be checked
- .arg
the name of the argument supplied to
xto appear in error messages. The default is to extract the argument's name usingrlang::caller_arg(). Ignored if.msgis supplied.- .msg
an optional alternative message to display if an error is thrown instead of the default message.
- .call
the execution environment of a currently running function, e.g.
.call = rlang::current_env(). The corresponding function call is retrieved and mentioned in error messages as the source of the error. Passed toerr(). Set toNULLto omit call information. The default is to search along the call stack for the first user-facing function in another package, if any.
Examples
obj <- TRUE
try(arg_flag(obj)) # No error
try(arg_logical(obj)) # No error
obj <- c(TRUE, FALSE)
try(arg_flag(obj)) # Error: must be a scalar
#> Error : `obj` must be a single logical value (TRUE or FALSE).
try(arg_logical(obj)) # No error
obj <- 1L
try(arg_flag(obj)) # Error must be logical
#> Error : `obj` must be a logical value (TRUE or FALSE).
try(arg_logical(obj)) # Error must be logical
#> Error : `obj` must be a logical vector.