Skip to content

Commit 0da36b8

Browse files
authored
Add allow_na to check_logical() (#1823)
1 parent 7371bac commit 0da36b8

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

R/standalone-types-check.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#
1010
# ## Changelog
1111
#
12+
# 2025-09-19:
13+
# - `check_logical()` gains an `allow_na` argument (@jonthegeek, #1724)
14+
#
1215
# 2024-08-15:
1316
# - `check_character()` gains an `allow_na` argument (@martaalcalde, #1724)
1417
#
@@ -538,12 +541,20 @@ check_character <- function(
538541
check_logical <- function(
539542
x,
540543
...,
544+
allow_na = TRUE,
541545
allow_null = FALSE,
542546
arg = caller_arg(x),
543547
call = caller_env()
544548
) {
545549
if (!missing(x)) {
546550
if (is_logical(x)) {
551+
if (!allow_na && any(is.na(x))) {
552+
abort(
553+
sprintf("`%s` can't contain NA values.", arg),
554+
arg = arg,
555+
call = call
556+
)
557+
}
547558
return(invisible(NULL))
548559
}
549560
if (allow_null && is_null(x)) {

tests/testthat/_snaps/standalone-types-check.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@
489489
<error/rlang_error>
490490
Error in `checker()`:
491491
! `foo` must be a logical vector or `NULL`, not a list.
492+
Code
493+
err(checker(NA, check_logical, allow_na = FALSE))
494+
Output
495+
<error/rlang_error>
496+
Error in `checker()`:
497+
! `foo` can't contain NA values.
492498

493499
# non-numeric types are not numbers
494500

tests/testthat/test-standalone-types-check.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ test_that("`check_logical()` checks", {
188188
err(checker(NA_integer_, check_logical))
189189
err(checker(1, check_logical))
190190
err(checker(list("foo", "bar"), check_logical, allow_null = TRUE))
191+
err(checker(NA, check_logical, allow_na = FALSE))
191192
})
192193
})
193194

0 commit comments

Comments
 (0)