R/utils.r

Defines functions str_match camel_to_underscore delay_by msf_date

`%||%` <- function(x, y) {
  if(is.null(x) || length(x) == 0) y else x
}

msf_date <- function(date) {
  stopifnot(methods::is(date, "Date"))
  format(date, "%Y%m%d")
}

delay_by <- function(delay, f) {
  function(...) {
    res <- f(...)
    Sys.sleep(delay)
    res
  }
}

camel_to_underscore <- function(x) {
  gsub(" ", "_", trimws(tolower(gsub("([[:upper:]])", " \\1", x))))
}

str_match <- function(x, pattern) {
  locations <- regexec(pattern, x)
  matches <- regmatches(x, locations)
  transpose_and_simplify(matches)
}
zamorarr/msf documentation built on June 22, 2018, 7:23 p.m.