From 6c541c328818c73b25f817e5849a542c0ed1fa79 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Mon, 10 Aug 2026 16:43:43 -0700 Subject: [PATCH 1/2] updating check_scalar to error on list input --- R/standalone-checks.R | 25 ++++++++++++++++------ tests/testthat/_snaps/standalone-checks.md | 2 +- tests/testthat/test-standalone-checks.R | 8 ++++++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/R/standalone-checks.R b/R/standalone-checks.R index 0d19387..23c7109 100644 --- a/R/standalone-checks.R +++ b/R/standalone-checks.R @@ -12,6 +12,9 @@ # # ## Changelog # +# 2026-08-10 +# - Updated `check_scalar()` to return an error for lists of length 1. +# # 2026-07-01 # - `check_*()` functions now error on empty input when `allow_empty = FALSE` # (previously empty input silently passed class/type checks) (#30) @@ -20,6 +23,7 @@ # # 2025-05-08 # - Added `check_identical()` and `check_identical_length()` +# # 2025-04-27 # - Added `check_named()` @@ -256,18 +260,25 @@ check_scalar <- function(x, message = ifelse( allow_empty, - "The {.arg {arg_name}} argument must be length {.val {1}} or empty.", - "The {.arg {arg_name}} argument must be length {.val {1}}." + "The {.arg {arg_name}} argument must be a vector of length {.val {1}} or empty.", + "The {.arg {arg_name}} argument must be a vector of length {.val {1}}." ), arg_name = rlang::caller_arg(x), class = "check_scalar", call = get_cli_abort_call(), envir = rlang::current_env()) { - check_length( - x = x, length = 1L, message = message, - allow_empty = allow_empty, arg_name = arg_name, - class = class, call = call, envir = envir - ) + # if empty: return invisibly when allowed, otherwise error + if (rlang::is_empty(x)) { + if (isTRUE(allow_empty)) { + return(invisible(x)) + } + cli::cli_abort(message, class = c(class, "standalone-checks"), call = call, .envir = envir) + } + + # input must be a vector of length 1 (not a list) + if (length(x) != 1L || is.list(x)) { + cli::cli_abort(message, class = c(class, "standalone-checks"), call = call, .envir = envir) + } } #' Check Number of Levels diff --git a/tests/testthat/_snaps/standalone-checks.md b/tests/testthat/_snaps/standalone-checks.md index 3893753..a17d484 100644 --- a/tests/testthat/_snaps/standalone-checks.md +++ b/tests/testthat/_snaps/standalone-checks.md @@ -60,7 +60,7 @@ myfunc(c(TRUE, FALSE)) Condition Error in `myfunc()`: - ! The `x` argument must be length 1. + ! The `x` argument must be a vector of length 1. --- diff --git a/tests/testthat/test-standalone-checks.R b/tests/testthat/test-standalone-checks.R index e5ec198..6f666be 100644 --- a/tests/testthat/test-standalone-checks.R +++ b/tests/testthat/test-standalone-checks.R @@ -106,7 +106,13 @@ test_that("check functions work", { expect_error( check_scalar(c(1, 10)), - "The `c(1, 10)` argument must be length 1.", + "The `c(1, 10)` argument must be a vector of length 1.", + fixed = TRUE + ) + + expect_error( + check_scalar(list(1)), + "The `list(1)` argument must be a vector of length 1.", fixed = TRUE ) From 0b42ecf52eb896461d05f720d03e9a31634d8e86 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Mon, 10 Aug 2026 18:06:02 -0700 Subject: [PATCH 2/2] Update standalone-checks.R --- R/standalone-checks.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/standalone-checks.R b/R/standalone-checks.R index 23c7109..ce1ab36 100644 --- a/R/standalone-checks.R +++ b/R/standalone-checks.R @@ -279,6 +279,8 @@ check_scalar <- function(x, if (length(x) != 1L || is.list(x)) { cli::cli_abort(message, class = c(class, "standalone-checks"), call = call, .envir = envir) } + + invisible(x) } #' Check Number of Levels