Skip to content

Commit

Permalink
Add a function for getting unique initials of a character vector. Pre…
Browse files Browse the repository at this point in the history
…paring for using getopt with short flags
  • Loading branch information
Nick-Eagles committed Oct 17, 2023
1 parent 4b44f17 commit 43e337e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 43e337e

Please sign in to comment.