R/assertions.R

Defines functions is_string is_string_or_null is_flag

#' @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)")
}
gaborcsardi/conf documentation built on May 24, 2019, 4:04 a.m.