R/stringify_vec.R

Defines functions stringify_vec

Documented in stringify_vec

# ---------------------------------------------------------------------------- #
#' Stringify vector
#'
#' Converts a vector (named or unnamed) into a single string representation.
#'
#' @param vec The vector to be converted into a character string.
#' @param item_sep The character separator to use between vector elements when
#'   converting the vector into a string.
#' @param name_sep The character separator to use between names and values when
#'   converting the vector into a string (if names exist).
#' @param fmt The format specification for the vector values (as a string), or
#'   NULL to use defaults (which would be as returned by \code{\link{format}}).
#'
#' @return A single character string representation of the vector.
#'
#' @examples
#' stringify_vec(c(1, 2, 3))
#'
#' stringify_vec(c(one = 1, two = 2, three = 3))
#'
#' stringify_vec(c(alpha = 1, bravo = 2, charlie = 3), fmt = "%3.1f")
#'
#' @export
#'
stringify_vec <- function(vec, item_sep = ", ", name_sep = " = ", fmt = NULL) {
  elements <- sprintf(fmt %||% "%s", vec)
  if (!is.null(names(vec))) {
    elements <- sprintf("%s%s%s", names(vec), name_sep, elements)
  }
  paste(elements, collapse = item_sep)
}

# ---------------------------------------------------------------------------- #
toniprice/jute documentation built on Jan. 11, 2023, 8:23 a.m.