random_names: Random Names

View source: R/utils.R

random_namesR Documentation

Random Names

Description

Create unique names for objects, files, etc. that are guaranteed to not already exist.

Usage

random_names(
  n_random_names = 1L,
  exclude_names = character(0L),
  name_length = 10L,
  character_pool = letters,
  transform = identity,
  n_max_tries = 1000L
)

Arguments

n_random_names

⁠[integer]⁠ (mandatory, default 1L)

generate this many random names

exclude_names

⁠[character]⁠ (optional, default character(0L)) names are generated until none of them is one the names supplied here.

name_length

⁠[integer]⁠ (mandatory, default 10L)

length of each random name

character_pool

⁠[character]⁠ (mandatory, default letters)

vector of characters (each must be a string of length one) to sample from when generating random names; the first character in any random name is always generated from letters despite what is supplied here

transform

⁠[function]⁠ (mandatory, default identity)

this transformation is applied first before checking whether they already exist in exclude_names; the function must have the argument x to accept the current random names and it must output them after any transformation (such as adding a file extension; see Examples)

n_max_tries

⁠[integer]⁠ (mandatory, default 1000L)

sampling of an individual random name is attempted this many times before giving up (raising an error); one may need to give if for some reason there are so many names to avoid or the pool of characters is so small that no random name can be generated

Examples


# avoiding writing over a pre-existing file
random_names(exclude_names = "my_file.csv",
             transform = function(x) paste0(x, ".csv"))

# avoiding writing over a pre-existing file path
random_names(exclude_names = "path/to/my_file.csv",
             transform = function(x) paste0("path/to/", x, ".csv"))

# avoiding writing over a pre-existing file path for multiple files
random_names(n_random_names = 10L,
             exclude_names = "path/to/my_file.csv",
             transform = function(x) paste0("path/to/", x, ".csv"))


CancerRegistryOfNorway/nordcancore documentation built on April 29, 2024, 4:40 p.m.