# ---------------------------------------------------------------------------- #
#' Construct sequence of incremental names
#'
#' Constructs a sequence of names, each of which has an increasing integer
#' appended to it. Digits are padded so that the names are of equal length.
#'
#' @param n_items The number of names to generate.
#'
#' @param prefix An optional character name prefix to use. E.g. if
#' \code{prefix} is "xyz" and \code{n_items} is 3 this will result in names
#' \code{xyz_1}, \code{xyz_2} and \code{xyz_3}. If NULL the returned names
#' will simply be a sequence of integers which are formatted as strings.
#'
#' @param sep The character separator to use between \code{prefix} and the
#' incremental numbers (default is an underscore). Ignored if \code{prefix =
#' NULL}.
#'
#' @param start Starting value for the integer sequence.
#'
#' @return A character vector of equal-length names with successively
#' incremented integer suffixes.
#'
#' @examples
#' get_incremental_names(2, "item")
#' get_incremental_names(3, "item", sep = "")
#' get_incremental_names(11, "z")
#' get_incremental_names(12)
#' get_incremental_names(3, "a", start = 0)
#'
#' @export
#'
get_incremental_names <- function(n_items, prefix = NULL, sep = "_",
start = 1) {
end <- n_items + start - 1
len <- nchar(sprintf("%d", end))
fmt <- sprintf("%%0%dd", len)
if (!is.null(prefix)) fmt <- sprintf("%s%s%s", prefix, sep, fmt)
sprintf(fmt, seq(start, end))
}
# ---------------------------------------------------------------------------- #
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.