R/repeat_symbol.R

Defines functions make_spaces repeat_symbol_2 repeat_symbol

Documented in make_spaces repeat_symbol repeat_symbol_2

#' Repeat sequence of symbols.
#'
#' @param text (character) The symbol (or sequence of symbols) to be repeated
#'             until desired length is achieved.
#' @param length.out (integer) The total length (in characters) of the sequence.
#' @param times (integer) Times to repeat the sequence.
#'
#' @return Sting of defined length.
#' @export
#'
#' @importFrom magrittr "%>%"
#'
#' @examples
#'
#' repeat_symbol(".", 10)
#'
#' repeat_symbol("..+", 10)
repeat_symbol <- function(text, length.out) {
  rep_len(text, length.out) %>%
    paste0(collapse = "") %>%
    substr(1, length.out)
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname repeat_symbol
#' @export
repeat_symbol_2 <- function(text = " ", times) {
  checkmate::assert_character(text)
  checkmate::assert_integerish(times, any.missing = TRUE, all.missing = TRUE)
  times <- if (is.na(times)) 0 else times
  paste0(rep(text, times), collapse = "")
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#' Generate spaces of certain length
#'
#' @param n (integer) A vector of integers indicating number of spaces to generate.
#'
#' @return A character vector of length `length(n)` with spaces.
#' @export
#'
#' @examples
#' make_spaces(c(2, NA))
make_spaces <- function(n) {
  purrr::map_chr(n, ~ repeat_symbol_2(" ", .))
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GegznaV/addin.tools documentation built on Aug. 24, 2023, 5:35 p.m.