Skip to content

Commit

Permalink
Merge pull request #1901 from rstudio/arg-match-replacements
Browse files Browse the repository at this point in the history
Arg match replacements (`arg_match()` -> `arg_match0()`)
  • Loading branch information
rich-iannone authored Oct 4, 2024
2 parents b7e83d2 + af9fbf9 commit b75dc29
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 64 deletions.
6 changes: 5 additions & 1 deletion R/cols_align.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ cols_align <- function(
stop_if_not_gt_tbl(data = data)

# Get the `align` value, this stops the function if there is no match
align <- rlang::arg_match(align)
align <-
rlang::arg_match0(
align,
values = c("auto", "left", "center", "right")
)

# Get the columns supplied in `columns` as a character vector
column_names <-
Expand Down
14 changes: 9 additions & 5 deletions R/data_color.R
Original file line number Diff line number Diff line change
Expand Up @@ -676,16 +676,20 @@ data_color <- function(
stop_if_not_gt_tbl(data = data)

# Get the correct `direction` value
direction <- rlang::arg_match(direction)
direction <- rlang::arg_match0(direction, values = c("column", "row"))

# Get the correct `method` value
method <- rlang::arg_match(method)
method <-
rlang::arg_match0(
method,
values = c("auto", "numeric", "bin", "quantile", "factor")
)

# Get the correct `apply_to` value
apply_to <- rlang::arg_match(apply_to)
apply_to <- rlang::arg_match0(apply_to, values = c("fill", "text"))

# Get the correct `contrast_algo` value
contrast_algo <- rlang::arg_match(contrast_algo)
contrast_algo <- rlang::arg_match0(contrast_algo, values = c("apca", "wcag"))

# If no color is provided to `na_color`, use gray as a default
na_color <- na_color %||% "#808080"
Expand Down Expand Up @@ -1276,7 +1280,7 @@ ideal_fgnd_color <- function(
) {

# Get the correct `algo` value
algo <- rlang::arg_match(algo)
algo <- rlang::arg_match0(algo, values = c("apca", "wcag"))

# Normalize color to hexadecimal color if it is in the 'rgba()' string format
bgnd_color <- rgba_to_hex(colors = bgnd_color)
Expand Down
6 changes: 5 additions & 1 deletion R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,11 @@ as_word <- function(
# Perform input object validation
stop_if_not_gt_tbl(data = data)

caption_location <- rlang::arg_match(caption_location)
caption_location <-
rlang::arg_match0(
caption_location,
values = c("top", "bottom", "embed")
)

# Build all table data objects through a common pipeline
value <- build_data(data = data, context = "word")
Expand Down
13 changes: 11 additions & 2 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ extract_body <- function(
}

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <-
rlang::arg_match0(
output,
values = c("html", "latex", "rtf", "word", "grid")
)

rlang::check_dots_empty()

# Generate vector of columns to include in output
Expand Down Expand Up @@ -613,7 +618,11 @@ extract_cells <- function(
stop_if_not_gt_tbl(data = data)

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <-
rlang::arg_match0(
output,
values = c("auto", "plain", "html", "latex", "rtf", "word", "grid")
)

if (output == "auto") {
output <- determine_output_format()
Expand Down
44 changes: 28 additions & 16 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ fmt_symbol <- function(
) {

# Ensure that arguments are matched
system <- rlang::arg_match(system)
system <- rlang::arg_match0(system, values = c("intl", "ind"))

# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
Expand Down Expand Up @@ -1462,7 +1462,7 @@ fmt_percent <- function(
#

# Ensure that arguments are matched
system <- rlang::arg_match(system)
system <- rlang::arg_match0(system, values = c("intl", "ind"))

# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
Expand Down Expand Up @@ -1763,8 +1763,12 @@ fmt_partsper <- function(
#

# Ensure that arguments are matched
to_units <- rlang::arg_match(to_units)
system <- rlang::arg_match(system)
to_units <-
rlang::arg_match0(
to_units,
values = c("per-mille", "per-myriad", "pcm", "ppm", "ppb", "ppt", "ppq")
)
system <- rlang::arg_match0(system, values = c("intl", "ind"))

# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
Expand Down Expand Up @@ -2119,8 +2123,8 @@ fmt_fraction <- function(
#

# Ensure that arguments are matched
system <- rlang::arg_match(system)
layout <- rlang::arg_match(layout)
system <- rlang::arg_match0(system, values = c("intl", "ind"))
layout <- rlang::arg_match0(layout, values = c("inline", "diagonal"))

# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
Expand Down Expand Up @@ -2857,7 +2861,7 @@ fmt_currency <- function(
#

# Ensure that arguments are matched
system <- rlang::arg_match(system)
system <- rlang::arg_match0(system, values = c("intl", "ind"))

# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
Expand Down Expand Up @@ -3068,7 +3072,7 @@ fmt_roman <- function(
#

# Ensure that arguments are matched
case <- rlang::arg_match(case)
case <- rlang::arg_match0(case, values = c("upper", "lower"))

valid_class <- c("numeric", "integer")
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
Expand Down Expand Up @@ -3296,8 +3300,8 @@ fmt_index <- function(
#

# Ensure that arguments are matched
case <- rlang::arg_match(case)
index_algo <- rlang::arg_match(index_algo)
case <- rlang::arg_match0(case, values = c("upper", "lower"))
index_algo <- rlang::arg_match0(index_algo, values = c("repeat", "excel"))

# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
Expand Down Expand Up @@ -3978,7 +3982,7 @@ fmt_bytes <- function(
#

# Ensure that arguments are matched
standard <- rlang::arg_match(standard)
standard <- rlang::arg_match0(standard, values = c("decimal", "binary"))

# Stop function if `locale` does not have a valid value; normalize locale
# and resolve one that might be set globally
Expand Down Expand Up @@ -4236,8 +4240,12 @@ fmt_duration <- function(
stop_if_not_gt_tbl(data = data)

# Ensure that arguments are matched
duration_style <- rlang::arg_match(duration_style)
system <- rlang::arg_match(system)
duration_style <-
rlang::arg_match0(
duration_style,
values = c("narrow", "wide", "colon-sep", "iso")
)
system <- rlang::arg_match0(system, values = c("intl", "ind"))

check_chr_has_length(output_units, allow_null = TRUE, allow_0 = FALSE)
check_chr_has_length(input_units, allow_null = TRUE, allow_0 = FALSE)
Expand Down Expand Up @@ -9301,7 +9309,7 @@ fmt_icon <- function(
#

# Ensure that arguments are matched
a11y <- rlang::arg_match(a11y)
a11y <- rlang::arg_match0(a11y, values = c("semantic", "decorative", "none"))

if (a11y == "semantic") {
a11y <- "sem"
Expand Down Expand Up @@ -9657,7 +9665,11 @@ fmt_markdown <- function(
#

# Ensure that arguments are matched
md_engine <- rlang::arg_match(md_engine)
md_engine <-
rlang::arg_match0(
md_engine,
values = c("markdown", "commonmark")
)

# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
Expand Down Expand Up @@ -10036,7 +10048,7 @@ fmt_auto <- function(
stop_if_not_gt_tbl(data = data)

# Ensure that arguments are matched
lg_num_pref <- rlang::arg_match(lg_num_pref)
lg_num_pref <- rlang::arg_match0(lg_num_pref, values = c("sci", "suf"))

# Resolve the `locale` value here with the global locale value
locale <- resolve_locale(data = data, locale = locale)
Expand Down
61 changes: 37 additions & 24 deletions R/format_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#
#------------------------------------------------------------------------------#

output_types <- c("auto", "plain", "html", "latex", "rtf", "word")

# vec_fmt_number() -------------------------------------------------------------
#' Format a vector as numeric values
Expand Down Expand Up @@ -177,7 +178,7 @@ vec_fmt_number <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -469,7 +470,7 @@ vec_fmt_scientific <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -626,7 +627,7 @@ vec_fmt_engineering <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -797,7 +798,7 @@ vec_fmt_percent <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -976,10 +977,14 @@ vec_fmt_partsper <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `to_units` is matched correctly to one option
to_units <- rlang::arg_match(to_units)
to_units <-
rlang::arg_match0(
to_units,
values = c("per-mille", "per-myriad", "pcm", "ppm", "ppb", "ppt", "ppq")
)

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -1115,10 +1120,10 @@ vec_fmt_fraction <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `layout` is matched correctly to one option
layout <- rlang::arg_match(layout)
layout <- rlang::arg_match0(layout, values = c("inline", "diagonal"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -1309,7 +1314,7 @@ vec_fmt_currency <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -1421,8 +1426,8 @@ vec_fmt_roman <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `case` and `output` are matched correctly to one option
case <- rlang::arg_match(case)
output <- rlang::arg_match(output)
case <- rlang::arg_match0(case, values = c("upper", "lower"))
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -1543,9 +1548,9 @@ vec_fmt_index <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `case`, `index_algo` and `output` are matched correctly to one option
case <- rlang::arg_match(case)
index_algo <- rlang::arg_match(index_algo)
output <- rlang::arg_match(output)
case <- rlang::arg_match0(case, values = c("upper", "lower"))
index_algo <- rlang::arg_match0(index_algo, values = c("repeat", "excel"))
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -1682,7 +1687,7 @@ vec_fmt_spelled_num <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -1832,8 +1837,8 @@ vec_fmt_bytes <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer"))

# Ensure that `standard` and `output` are matched correctly to one option
standard <- rlang::arg_match(standard)
output <- rlang::arg_match(output)
standard <- rlang::arg_match0(standard, values = c("decimal", "binary"))
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -2025,7 +2030,7 @@ vec_fmt_date <- function(
check_vector_valid(x, valid_classes = c("Date", "POSIXt", "character"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -2197,7 +2202,7 @@ vec_fmt_time <- function(
check_vector_valid(x, valid_classes = c("Date", "POSIXt", "character"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -3018,7 +3023,7 @@ vec_fmt_datetime <- function(
check_vector_valid(x, valid_classes = c("Date", "POSIXct", "character"))

# Ensure that `output` is matched correctly to one option
output <- rlang::arg_match(output)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -3215,8 +3220,12 @@ vec_fmt_duration <- function(
check_vector_valid(x, valid_classes = c("numeric", "integer", "difftime"))

# Ensure that `duration_style` and `ouput` are matched correctly to one option
duration_style <- rlang::arg_match(duration_style)
output <- rlang::arg_match(output)
duration_style <-
rlang::arg_match0(
duration_style,
values = c("narrow", "wide", "colon-sep", "iso")
)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down Expand Up @@ -3310,8 +3319,12 @@ vec_fmt_markdown <- function(
check_vector_valid(x)

# Ensure that arguments are matched
md_engine <- rlang::arg_match(md_engine)
output <- rlang::arg_match(output)
md_engine <-
rlang::arg_match0(
md_engine,
values = c("markdown", "commonmark")
)
output <- rlang::arg_match0(output, values = output_types)

if (output == "auto") {
output <- determine_output_format()
Expand Down
Loading

0 comments on commit b75dc29

Please sign in to comment.