check_numeric() and check_is_numeric() functions check whether
the input is a valid numeric value within a specified range. The
check_numeric() function raises an error if the input does not meet
the specified conditions, while check_is_numeric() returns TRUE
if the input is a valid numeric value within the specified range, and
FALSE otherwise.
Arguments
- num
 A numeric scalar to check.
- min
 The minimum allowed value for
num.- max
 The maximum allowed value for
num.- boundary
 Defines whether the range is inclusive or exclusive.
- is_integer
 Logical value. If
TRUE, ensuresnumis an integer. IfFALSE, ensuresnumis a double.- allow_null
 Logical value. If
TRUE, allowsnumto be NULL.- par_name
 The name of the parameter to display in error messages.
Value
check_numeric(): Returns the original value ofnumif it is valid. Raises an error message ifnumis not a valid numeric value or not within the specified range.check_is_numeric(): Logical value,TRUEif the input is a valid numeric value within the specified range, andFALSEotherwise.
Examples
# Examples for check_numeric()
# ----------------------------
check_numeric(5, min = 1, max = 10)
#> [1] 5
try(check_numeric(0, min = 1, max = 10))
#> Error in check_numeric(0, min = 1, max = 10) : 
#>   Expected 'num' to be a numeric value between 1 and 10 inclusive but got 0
check_numeric(5, min = 1, max = 10, is_integer = TRUE)
#> [1] 5
try(check_numeric(5.5, min = 1, max = 10, is_integer = TRUE))
#> Error in check_numeric(5.5, min = 1, max = 10, is_integer = TRUE) : 
#>   Expected 'num' to be an integer (whole number) but got numeric
try(check_numeric(NULL, min = 1, max = 10, allow_null = FALSE))
#> Error in check_numeric(NULL, min = 1, max = 10, allow_null = FALSE) : 
#>   Argument 'num' cannot be NULL when `allow_null = FALSE`. So either provide a numerical scalar for 'num' or set `allow_null = TRUE`
# Examples for check_is_numeric()
# -------------------------------
check_is_numeric(5, min = 1, max = 10)
#> [1] TRUE
check_is_numeric(0, min = 1, max = 10)
#> [1] FALSE
check_is_numeric(5, min = 1, max = 10, is_integer = TRUE)
#> [1] TRUE
check_is_numeric(5.5, min = 1, max = 10, is_integer = TRUE)
#> [1] FALSE
check_is_numeric(NULL, min = 1, max = 10, allow_null = FALSE)
#> [1] FALSE