File: validate-that.R

package info (click to toggle)
r-cran-assertthat 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 220 kB
  • sloc: sh: 9; makefile: 2
file content (25 lines) | stat: -rw-r--r-- 888 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#' Validate that certain conditions are true.
#'
#' \code{validate_that} is an alternative to the function
#' \code{\link{assert_that}}, that returns a \code{character} vector. This
#' makes them easier to use within S4 \code{"validate"} methods.
#'
#' @inheritParams assert_that
#' @return A \code{character} vector if the assertion is false, or \code{TRUE}
#' if the assertion is true.
#' @export
#' @seealso \code{\link{assert_that}}, which returns an error if the condition
#' is false.
#' @examples
#' x <- 1
#' # assert_that() generates errors, so can't be usefully run in
#' # examples
#' validate_that(is.numeric(x))
#' validate_that(is.character(x))
#' validate_that(length(x) == 3)
#' validate_that(is.dir("asdf"))
validate_that <- function(..., env = parent.frame(), msg = NULL) {
  res <- see_if(..., env = env, msg = msg)
  if (res) return(TRUE)
  return(attr(res, "msg"))
}