Nick logo Credibly Curious

Nick Tierney's (mostly) rstats blog

2020-09-21

Create Alpha Numeric Strings

Nicholas Tierney

Categories: functions rstats Tags: functions rstats

3 minute read

Sometimes it is useful to create alpha numeric strings. In my case, I wanted to generate something that looked like an API key in a demo.

Here’s the code to do that, also with an additional argument to write to clipboard, which I usually want to do, and is made possible with the excellent clipr package by Matthew Lincoln.

alpha_num <- function(n, save_clip = TRUE){
  
  alpha_num_pool <- c(letters,
                      LETTERS,
                      rep(0:9, length.out = length(letters)*2))
  
  alpha_num_sample <- sample(x = alpha_num_pool,
                             size = n,
                             replace = TRUE)
  
  alpha_num_obj <- paste0(alpha_num_sample,
                          collapse = "")
  
  if (isTRUE(save_clip)) {
    message("writing key of length ", n, " to clipboard.")
    clipr::write_clip(alpha_num_obj)
  }
  
  return(alpha_num_obj)
  
}

And here is the output:

alpha_num(1)

#> writing key of length 1 to clipboard.

#> [1] "6"

alpha_num(10)

#> writing key of length 10 to clipboard.

#> [1] "ojv1jonrvs"

Let’s briefly break down each part of the function.

I want my key to be comprised of UPPERCASE, lowercase, and numbers.

So I create a vector with the R objects, LETTERS, and letters which contain the upper and lowercase alphabet. I also repeat the numbers 0 through to 9, and tell it to repeat the alphabet for twice the number of characters in the alphabet. This is so I get (somewhat) more equal representation of letters and numbers.

alpha_num_pool <- c(letters,
                   LETTERS,
                   rep(0:9, length.out = length(letters)*2))

alpha_num_pool

#>   [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r"
#>  [19] "s" "t" "u" "v" "w" "x" "y" "z" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
#>  [37] "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "0" "1"
#>  [55] "2" "3" "4" "5" "6" "7" "8" "9" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
#>  [73] "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "1" "2" "3" "4" "5" "6" "7"
#>  [91] "8" "9" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "1"

Then I sample that pool, n times - for our example here we’ll set n = 10

alpha_num_sample <- sample(x = alpha_num_pool,
                           # size = n,
                           size = 10,
                           replace = TRUE)

alpha_num_sample

#>  [1] "5" "6" "0" "h" "H" "2" "4" "u" "H" "S"

Then we need to smush that together into one string, so we use paste0 and set collapse = "", which says: “collapse with no characters”.

alpha_num_obj <- paste0(alpha_num_sample,
                        collapse = "")
alpha_num_obj

#> [1] "560hH24uHS"

Next we have this step that writes to clipboard. This asks, "is save_clip TRUE? Then do the following. This uses isTRUE, which is a robust way of checking if something is TRUE - I don’t think it is needed in this case, but I think it is good practice to use. It also reads better, I think.

if (isTRUE(save_clip)) {
    message("writing key of length ", n, " to clipboard.")
    clipr::write_clip(alpha_num_obj)
  }

Finally, we return the object:

return(alpha_num_obj)

This isn’t strictly needed - a function will return whatever the last output is, but in this case it felt kind of right to me as there was this if statement section and I wanted to make it clear that the part of the function that returned data was the last part. Generally, return is best used when you need a part of a function to return an object early.

And yeah, that’s it! Happy generating-alphabetical-numeric-strings!