From 43e337ea11d07cdb20d530a06a4bd204cb005be4 Mon Sep 17 00:00:00 2001 From: Nick-Eagles Date: Tue, 17 Oct 2023 14:33:40 -0400 Subject: [PATCH] Add a function for getting unique initials of a character vector. Preparing for using getopt with short flags --- R/utils.R | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/R/utils.R b/R/utils.R index 9df9d9f..d73f24d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -10,3 +10,22 @@ get_list_indexing <- function(this_list, index) { return(list("divisor" = divisor, "modulus" = modulus)) } + +# Given a character vector, return an equal-length character vector of +# unique one-letter initials +get_short_flags = function(loops) { + # Try just taking the first letter of the names + short_flags = substr(names(loops), 1, 1) + + # Get unused letters + remaining_letters = setdiff(letters, short_flags) + if (length(remaining_letters) == 0) { + stop("Can't handle more than 26 loops.") + } + + # Overwrite duplicates with unused letters + dup_flags = duplicated(short_flags) + short_flags[dup_flags] = remaining_letters[1:length(which(dup_flags))] + + return(short_flags) +}