R/assertions.R

#' @importFrom assertthat on_failure<-

is_string <- function(x) {
  is.character(x) && length(x) == 1 && !is.na(x)
}

on_failure(is_string) <- function(call, env) {
  paste0(sQuote(deparse(call$x)), " is not a string (length 1 character)")
}

is_string_or_null <- function(x) {
  is.null(x) || is_string(x)
}

on_failure(is_string_or_null) <- function(call, env) {
  paste0(
    sQuote(deparse(call$x)),
    " must be NULL or a string (length 1 character)"
  )
}

is_flag <- function(x) {
  is.logical(x) && length(x) == 1 && !is.na(x)
}

on_failure(is_flag) <- function(call, env) {
  paste0(sQuote(deparse(call$x)), " is not a flag (length 1 logical)")
}
r-lib/conf documentation built on May 14, 2019, 2:31 p.m.