Skip to content
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

* Improved error messages when `assert_create()` arguments are inappropriate

* Improved `assert_numeric()` error messaging for non-numeric matrices/arrays

# assertions 0.1.0

* Added a `NEWS.md` file to track changes to the package.
3 changes: 1 addition & 2 deletions R/assert_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ assert_factor_vector <- assert_create(is.factor, default_error_msg = msg_helper_
#'
#' @concept assert_type
#' @export
assert_numeric <- assert_create(is.numeric, default_error_msg = msg_helper_assert_type("numeric", a = FALSE))
assert_numeric <- assert_create(is_numeric_with_matrix_message, default_error_msg = msg_helper_assert_type("numeric", a = FALSE))

## numeric vector -----------------------------------------------------------
#' Assert input is a numeric vector
Expand Down Expand Up @@ -597,4 +597,3 @@ assert_connection <- assert_create(
func = is_connection,
default_error_msg = msg_helper_assert_type("database connection")
)

16 changes: 13 additions & 3 deletions R/is_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ is_numeric_vector <- function(x){
is.numeric(x) && is_vector(x)
}

is_numeric_with_matrix_message <- function(x){
if(is.numeric(x))
return(TRUE)

if(is.matrix(x) || is.array(x)){
structure_type <- if (is.matrix(x)) "matrix" else "array"
element_type <- typeof(x)
return(paste0("'{.strong {arg_name}}' must be numeric, not a {.strong ", element_type, "} ", structure_type))
}

return(FALSE)
}


#' Check if an object is a single number
#'
Expand Down Expand Up @@ -258,6 +271,3 @@ is_non_empty_string_advanced <- function(x){

return(invisible(TRUE))
}



2 changes: 1 addition & 1 deletion tests/testthat/test-assert_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ cli::test_that_cli("assert_numeric() works", configs = "plain", {
expect_identical(assert_numeric(matrix(1:9, 3)), TRUE)

# Aborts for non-numeric objects
expect_error(assert_numeric(matrix(LETTERS[1:5])), "'matrix(LETTERS[1:5])' must be numeric, not a character matrix", fixed = TRUE)
expect_error(assert_numeric(c("a", "b", "c")), "'c(\"a\", \"b\", \"c\")' must be numeric, not a character", fixed = TRUE)
expect_error(assert_numeric(factor(c(1, 2, 3))), "'factor(c(1, 2, 3))' must be numeric, not a factor", fixed = TRUE)
expect_error(assert_numeric(data.frame(a = 1, b = 2)), "'data.frame(a = 1, b = 2)' must be numeric, not a data.frame", fixed = TRUE)
Expand Down Expand Up @@ -550,4 +551,3 @@ cli::test_that_cli("assert_connection() works", configs = "plain", {
})