Skip to contents

Checks whether an argument does not contain any NA values (arg_no_NA()), contains only NA values (arg_all_NA()), or is a scalar NA (arg_is_NA()).

Usage

arg_no_NA(x, .arg = rlang::caller_arg(x), .msg = NULL, .call)

arg_is_NA(x, .arg = rlang::caller_arg(x), .msg = NULL, .call)

arg_all_NA(x, .arg = rlang::caller_arg(x), .msg = NULL, .call)

Arguments

x

the argument to be checked

.arg

the name of the argument supplied to x to appear in error messages. The default is to extract the argument's name using rlang::caller_arg(). Ignored if .msg is 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 to err(). Set to NULL to omit call information. The default is to search along the call stack for the first user-facing function in another package, if any.

Value

Returns NULL invisibly if an error is not thrown.

Details

arg_no_NA() throws an error when anyNA(x) is 0. arg_all_NA() throws an error when all(is.na(x)) is not FALSE. arg_is_NA() throws an error when length(x) is not 1 or anyNA(x) is FALSE.

arg_no_NA() is useful for checking that a meaningful argument was supplied. arg_all_NA() and arg_is_NA() are primarily used for in arg_or() to denote that NA is an allowed argument.

Examples

f <- function(x) {
  arg_no_NA(x)  ## x must not be NA
}

try(f(1))           ## No error
try(f(NA))          ## Error: x is NA
#> Error : `x` must not be NA.
try(f(c(1, NA, 3))) ## Error: x contains NA
#> Error : `x` must not contain NA values.

f2 <- function(y) {
  arg_all_NA(y) ## y must be NA
}

try(f2(NA))          ## No error
try(f2(c(NA, NA)))   ## No error
try(f2(1))           ## Error: y is not NA
#> Error : `y` must be NA.
try(f2(c(1, NA, 3))) ## Error: y is not all NA
#> Error : `y` must only contain NA values.