assert_that.Rdassert_that is a drop-in replacement for stopifnot but
is designed to give informative error messages.
assert_that(..., env = parent.frame(), msg = NULL)
see_if(..., env = parent.frame(), msg = NULL)unnamed expressions that describe the conditions to be tested.
Rather than combining expressions with &&, separate them by commas
so that better error messages can be generated.
(advanced use only) the environment in which to evaluate the assertions.
a custom error message to be printed if one of the conditions is false.
Assertion functions should return a single TRUE or FALSE:
any other result is an error, and assert_that will complain about
it. This will always be the case for the assertions provided by
assertthat, but you may need be a more careful for
base R functions.
To make your own assertions that work with assert_that,
see the help for on_failure. Alternatively, a custom message
can be specified for each call.
validate_that, which returns a message (not an error)
if the condition is false.
x <- 1
# assert_that() generates errors, so can't be usefully run in
# examples
if (FALSE) { # \dontrun{
assert_that(is.character(x))
assert_that(length(x) == 3)
assert_that(is.dir("asdf"))
y <- tempfile()
writeLines("", y)
assert_that(is.dir(y))
assert_that(FALSE, msg = "Custom error message")
} # }
# But see_if just returns the values, so you'll see that a lot
# in the examples: but remember to use assert_that in your code.
see_if(is.character(x))
#> [1] FALSE
#> attr(,"msg")
#> [1] "x is not a character vector"
see_if(length(x) == 3)
#> [1] FALSE
#> attr(,"msg")
#> [1] "length(x) not equal to 3"
see_if(is.dir(17))
#> [1] FALSE
#> attr(,"msg")
#> [1] "path is not a string (a length one character vector)."
see_if(is.dir("asdf"))
#> [1] FALSE
#> attr(,"msg")
#> [1] "Path 'asdf' does not exist"
see_if(5 < 3, msg = "Five is not smaller than three")
#> [1] FALSE
#> attr(,"msg")
#> [1] "Five is not smaller than three"