R/utils.R

Defines functions dput.str str.match `%||%`

Documented in str.match

#' Return a default value if a variable is null
#'
#' @param a The possibly null value
#' @param b The default value to return if `a` is null
#'
#' @return `a` if `a` is non-null, `b` if `a` is null
#'
#' @examples
#' x <- list(a = 1)
#' y <- x$b %||% 2 # y = 2
#' z <- x$a %||% 10 # z = 1
`%||%` <- function(a, b) {
  if (is.null(a)) b else a
}


#' Returns the group matches from a regular expression on a vector
#' @param x the vector we want to match on
#' @param pattern regular expression with groups
str.match <- function(x, pattern) {
  regmatches(x, regexec(pattern, x));
}

#' Return a string representation of an object
dput.str <- function(x) {
  paste0(capture.output(dput(x)), collapse = " ")
}
helen-zhu/ConsensusPeaks documentation built on Dec. 25, 2021, 9:39 a.m.