R/autonames.R

Defines functions autonames

Documented in autonames

#' Automatically generate names for vectors
#'
#' @param x A vector
#' @param ... Additional arguments passed to [format()]
#' @param trimws Whether to trim whitespace surrounding automatically formatted
#'   names.
#'   Defaults to `TRUE`.
#'
#' @return Returns the names of a named vector and the elements of an unnamed
#' vector formatted as characters.
#'
#' @export
#'
#' @example examples/autonames.R

autonames <- function(x, ..., trimws = TRUE) {
  if (is.null(names(x))) {
    names(x) <- format(x, ...)
    if (trimws) {names(x) <- trimws(names(x))}
  }

  names(x)
}

Try the crossmap package in your browser

Any scripts or data that you put into this service are public.

crossmap documentation built on Jan. 13, 2023, 1:13 a.m.