Improved reporting of AssertCollections
created with the checkmate::makeAssertCollection()
package
using cli::cli_abort()
instead of checkmate::reportAssertions()
in order to provide a more
informative error message.
The function is intended to be used inside a function that performs assertions on its input arguments. See below for an example.
Usage
report_checkmate_assertions(
collection,
msg = "Invalid input(s):",
env = parent.frame()
)
Arguments
- collection
A collection of assertions created with
checkmate::makeAssertCollection()
- msg
character()
Header of the error message if any assertions failed- env
environment()
Environment to use for the error message
Examples
add_numbers <- function(a, b) {
collection <- checkmate::makeAssertCollection()
checkmate::assert_numeric(x = a, add = collection)
checkmate::assert_numeric(x = b, add = collection)
report_checkmate_assertions(collection)
return(a + b)
}
add_numbers(1, 2)
#> [1] 3
try(add_numbers(1, "b"))
#> Error in add_numbers(1, "b") : Invalid input(s):
#> ✖ Variable 'b': Must be of type 'numeric', not 'character'.
try(add_numbers("a", "b"))
#> Error in add_numbers("a", "b") : Invalid input(s):
#> ✖ Variable 'a': Must be of type 'numeric', not 'character'.
#> ✖ Variable 'b': Must be of type 'numeric', not 'character'.