R/misc_helpers.R

Defines functions has_text have_text signc

Documented in has_text have_text signc

#' Checks if `x` is a valid character string and not empty
#'
#' @param x single character string (not vectorized)
#' @return TRUE or FALSE
#'
#' @examples
#' has_text("") # FALSE
#' has_text("some text") # TRUE
#' has_text(c("", "some text")) # FALSE, only first element checked
#'
#' @seealso [have_text()] for the vectorized version
#' @export
has_text <- function(x) {
  !missing(x) && length(x) > 0 && is.character(x) && !is.na(x[[1]]) && nchar(x[[1]]) > 0
}

#' Checks if arguments are valid, not empty, character strings
#'
#' @param ... character strings to check
#' @return logical vector
#'
#' @examples
#' have_text(c("", "some text")) # c(FALSE, TRUE)
#'
#' @seealso [has_text()] for the base non-vectorized version
#' @export
have_text <- function(...) if (length(list(...)) == 0) FALSE else
  sapply(as.list(...), has_text, simplify = TRUE, USE.NAMES = FALSE)

#' Character sign of an argument
#'
#' Returns "+" for positive arguments, "-" for negative and `zero` for 0.
#'
#' @param x a numeric vector
#' @param zero character to return for 0 argument
#'
#' @examples
#' signc(-1:1)
#' signc(-1:1, zero = "|")
#'
#' @export
signc <- function(x, zero = "0") {
  if (!is.numeric(x)) stop("x should be numeric")
  str <- ifelse(x >= 0, "+", "-")
  str[x == 0] <- zero
  str
}
avidclam/avidstart documentation built on May 17, 2019, 10:01 a.m.