|
|
|
|
|
|
|
|
validate_value <- function(value = NULL, choices, several.ok = TRUE, value_name = "") { |
|
|
validate_value <- function(value = NULL, choices, several.ok = TRUE, value_name = "") { |
|
|
if (!is.null(value) && length(value)) { |
|
|
if (!is.null(value) && length(value)) { |
|
|
value_name <- if (nchar(value_name) > 0) glue("`{value_name}` - ") else "" |
|
|
value_name <- if (nchar(value_name) > 0) glue("`{value_name}` - ") else "" |
|
|
if (!several.ok && length(value) > 1) { |
|
|
|
|
|
msg <- glue("{value_name}Using the first of {length(value)} values: {value[1]}") |
|
|
|
|
|
rlang::warn(msg) |
|
|
|
|
|
value <- value[1] |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
warnings <- c() |
|
|
not_in_choices <- setdiff(value, choices) |
|
|
not_in_choices <- setdiff(value, choices) |
|
|
if (length(not_in_choices)) { |
|
|
if (length(not_in_choices)) { |
|
|
msg <- glue("{value_name}Ignoring invalid choices: ", |
|
|
|
|
|
|
|
|
warnings <- glue("{value_name}Ignoring invalid choices: ", |
|
|
"\"{paste(not_in_choices, collapse = '\", \"')}\"") |
|
|
"\"{paste(not_in_choices, collapse = '\", \"')}\"") |
|
|
rlang::warn(msg) |
|
|
|
|
|
value <- intersect(value, choices) |
|
|
value <- intersect(value, choices) |
|
|
} |
|
|
} |
|
|
|
|
|
if (!several.ok && length(value) > 1) { |
|
|
|
|
|
warnings <- c( |
|
|
|
|
|
warnings, |
|
|
|
|
|
glue("{value_name}Using the first of {length(value)} values: {value[1]}") |
|
|
|
|
|
) |
|
|
|
|
|
value <- value[1] |
|
|
|
|
|
} |
|
|
if (length(value)) { |
|
|
if (length(value)) { |
|
|
|
|
|
if (length(warnings)) { |
|
|
|
|
|
rlang::warn(paste(warnings, collapse = "\n")) |
|
|
|
|
|
} |
|
|
value |
|
|
value |
|
|
} else { |
|
|
} else { |
|
|
rlang::abort(glue("{value_name}Must be one of the following valid choices: ", |
|
|
rlang::abort(glue("{value_name}Must be one of the following valid choices: ", |