Check if the input variable is numeric
Usage
check_if_numeric(
x,
allow_na = TRUE,
arg = rlang::caller_arg(x),
call = rlang::caller_env()
)
Arguments
- x
Input variable to check if it is numeric
- allow_na
Input boolean to determine if
NA
orNaN
is allowed. Default:TRUE
- arg
An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem.
- call
The execution environment of a currently running function, e.g.
caller_env()
. The function will be mentioned in error messages as the source of the error. See thecall
argument ofabort()
for more information.
Examples
# No error
input = 1
try(check_if_numeric(input))
# Error as "5" is not numeric
input = "5"
try(check_if_numeric(input))
#> Error in eval(expr, envir) :
#> Provided input `input`, must be <numeric>, `NA` or `NaN`. It is
#> currently "5" of type <character>
# Error as NULL is not numeric
input = NULL
try(check_if_numeric(input))
#> Error in eval(expr, envir) :
#> Provided input `input`, must be <numeric>, `NA` or `NaN`. It is
#> currently of type <NULL>
# Error as NA is not numeric and allow_na is FALSE
input = NA
try(check_if_numeric(input, allow_na = FALSE))
#> Error in eval(expr, envir) :
#> Provided input `input`, must be <numeric>. It is currently NA of type
#> <logical>