Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions R/standalone-types-check.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---
# repo: r-lib/rlang
# file: standalone-types-check.R
# last-updated: 2023-03-13
# last-updated: 2025-09-19
# license: https://unlicense.org
# dependencies: standalone-obj-type.R
# imports: rlang (>= 1.1.0)
Expand All @@ -11,6 +11,9 @@
#
# 2025-09-19:
# - `check_logical()` gains an `allow_na` argument (@jonthegeek, #1724)
# - Rename `check_number_decimal()` to `check_number()` (@khusmann, #1714)
# - Add `check_numeric()` and `check_numeric_whole()`, vectorized versions
# of `check_number()` and `check_number_whole()` (@khusmann, #1714)
#
# 2024-08-15:
# - `check_character()` gains an `allow_na` argument (@martaalcalde, #1724)
Expand Down Expand Up @@ -178,7 +181,7 @@ IS_NUMBER_true <- 0
IS_NUMBER_false <- 1
IS_NUMBER_oob <- 2

check_number_decimal <- function(
check_number <- function(
x,
...,
min = NULL,
Expand Down Expand Up @@ -502,6 +505,78 @@ check_formula <- function(

# TODO: Figure out what to do with logical `NA` and `allow_na = TRUE`

check_numeric <- function(
x,
...,
allow_na = TRUE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
) {
if (!missing(x)) {
if (is.numeric(x)) {
if (!allow_na && any(is.na(x))) {
abort(
sprintf("`%s` can't contain NA values.", arg),
arg = arg,
call = call
)
}

return(invisible(NULL))
}

if (allow_null && is_null(x)) {
return(invisible(NULL))
}
}

stop_input_type(
x,
"a numeric vector",
...,
allow_null = allow_null,
arg = arg,
call = call
)
}

check_numeric_whole <- function(
x,
...,
allow_na = TRUE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
) {
if (!missing(x)) {
if (is_integerish(x)) {
if (!allow_na && any(is.na(x))) {
abort(
sprintf("`%s` can't contain NA values.", arg),
arg = arg,
call = call
)
}

return(invisible(NULL))
}

if (allow_null && is_null(x)) {
return(invisible(NULL))
}
}

stop_input_type(
x,
"a numeric vector with whole numbers",
...,
allow_null = allow_null,
arg = arg,
call = call
)
}

check_character <- function(
x,
...,
Expand Down
112 changes: 98 additions & 14 deletions tests/testthat/_snaps/standalone-types-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,76 +244,76 @@
Error in `check()`:
! `max` must be a number, not missing.

# `check_number_decimal()` checks
# `check_number()` checks

Code
err(checker(, check_number_decimal))
err(checker(, check_number))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a number, not absent.
Code
err(checker(NA, check_number_decimal))
err(checker(NA, check_number))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a number, not `NA`.
Code
err(checker(NULL, check_number_decimal))
err(checker(NULL, check_number))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a number, not `NULL`.
Code
err(checker(int(), check_number_decimal, allow_na = TRUE))
err(checker(int(), check_number, allow_na = TRUE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a number or `NA`, not an empty integer vector.
Code
err(checker(na_dbl, check_number_decimal))
err(checker(na_dbl, check_number))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a number, not a numeric `NA`.
Code
err(checker(na_int, check_number_decimal))
err(checker(na_int, check_number))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a number, not an integer `NA`.
Code
err(checker(10:11, check_number_decimal, allow_na = TRUE, allow_null = TRUE))
err(checker(10:11, check_number, allow_na = TRUE, allow_null = TRUE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a number, `NA`, or `NULL`, not an integer vector.
Code
err(checker(Inf, check_number_decimal, allow_infinite = FALSE))
err(checker(Inf, check_number, allow_infinite = FALSE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a number, not `Inf`.
Code
err(checker(-Inf, check_number_decimal, allow_infinite = FALSE))
err(checker(-Inf, check_number, allow_infinite = FALSE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a number, not `-Inf`.
Code
err(checker(10, min = NA, check_number_decimal))
err(checker(10, min = NA, check_number))
Output
<error/rlang_error>
Error in `check()`:
! `min` must be a single double value.
Code
err(checker(10, min = NaN, check_number_decimal))
err(checker(10, min = NaN, check_number))
Output
<error/rlang_error>
Error in `check()`:
! `min` must be a number, not missing.
Code
err(checker(10, max = NaN, check_number_decimal))
err(checker(10, max = NaN, check_number))
Output
<error/rlang_error>
Error in `check()`:
Expand Down Expand Up @@ -418,6 +418,90 @@
Error in `checker()`:
! `foo` must be an environment or `NULL`, not a list.

# `check_numeric()` checks

Code
err(checker(, check_numeric))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector, not absent.
Code
err(checker(NULL, check_numeric))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector, not `NULL`.
Code
err(checker(NA, check_numeric))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector, not `NA`.
Code
err(checker("foo", check_numeric))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector, not the string "foo".
Code
err(checker(list(1, 2), check_numeric, allow_null = TRUE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector or `NULL`, not a list.
Code
err(checker(c(1, NA), check_numeric, allow_na = FALSE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` can't contain NA values.

# `check_numeric_whole()` checks

Code
err(checker(, check_numeric_whole))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector with whole numbers, not absent.
Code
err(checker(1.1, check_numeric_whole))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector with whole numbers, not the number 1.1.
Code
err(checker(NULL, check_numeric_whole))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector with whole numbers, not `NULL`.
Code
err(checker(NA, check_numeric_whole))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector with whole numbers, not `NA`.
Code
err(checker("foo", check_numeric_whole))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector with whole numbers, not the string "foo".
Code
err(checker(list(1, 2), check_numeric_whole, allow_null = TRUE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must be a numeric vector with whole numbers or `NULL`, not a list.
Code
err(checker(c(1, NA), check_numeric_whole, allow_na = FALSE))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` can't contain NA values.

# `check_character()` checks

Code
Expand Down Expand Up @@ -505,7 +589,7 @@
Error:
! `factor("a")` must be a whole number, not a <factor> object.
Code
(expect_error(check_number_decimal(as.Date("2000-01-01"))))
(expect_error(check_number(as.Date("2000-01-01"))))
Output
<error/rlang_error>
Error:
Expand Down
Loading
Loading