R/zzz.R

Defines functions is_number is_string is_strings is_boolean has_class nullable valid_path

is_number <- function(x) {
  is.numeric(x) || tryCatch(as.numeric(x), warning = function(e) FALSE)
}

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

is_strings <- function(x, allow_na = FALSE) {
  is.character(x) && length(x) > 0 && (allow_na || all(!is.na(x)))
}

is_boolean <- function(x) {
  is.logical(x) && length(x) > 0
}

has_class <- function(x, class) {
  if (isNamespaceLoaded("methods")) {
    methods::is(x, class)
  } else {
    inherits(x, class)
  }
}

nullable <- function(fun) {
  function(x, ...) {
    is.null(x) || fun(x, ...)
  }
}

valid_path <- function(path) {
  is_string(path) && dir.exists(path)
}

Try the gmailr package in your browser

Any scripts or data that you put into this service are public.

gmailr documentation built on Aug. 23, 2019, 5:06 p.m.