Skip to content

Commit

Permalink
Merge pull request #1748 from rstudio/info-currencies-improve
Browse files Browse the repository at this point in the history
Improve the appearance of the tables produced by `info_currencies()`
rich-iannone authored Jul 5, 2024
2 parents ea3031f + ec77561 commit a3434f4
Showing 6 changed files with 185 additions and 98 deletions.
2 changes: 1 addition & 1 deletion R/format_data.R
Original file line number Diff line number Diff line change
@@ -3621,7 +3621,7 @@ round_gt <- function(x, digits = 0) {
#' supplied as a 3-letter currency code (e.g., `"USD"` for U.S. Dollars,
#' `"EUR"` for the Euro currency). Use [info_currencies()] to get an
#' information table with all of the valid currency codes and examples of
#' each. Alternatively, we can provide a common currency name (e.g.,
#' each. Alternatively, we can provide a general currency type (e.g.,
#' `"dollar"`, `"pound"`, `"yen"`, etc.) to simplify the process. Use
#' [info_currencies()] with the `type == "symbol"` option to view an
#' information table with all of the supported currency symbol names along
261 changes: 174 additions & 87 deletions R/info_tables.R
Original file line number Diff line number Diff line change
@@ -245,13 +245,14 @@ info_time_style <- function(locale = NULL) {
#'
#' [fmt_currency()] lets us format numeric values as currencies. The table
#' generated by `info_currencies()` provides a quick reference to all the
#' available currencies. The currency identifiers are provided
#' (name, 3-letter currency code, and 3-digit currency code) along with
#' the each currency's exponent value (number of digits of the currency
#' subunits). A formatted example is provided (based on the value of `49.95`) to
#' demonstrate the default formatting of each currency.
#' available currencies. The currency identifiers are provided (name, 3-letter
#' currency code, and 3-digit currency code) along with the each currency's
#' exponent value (number of digits of the currency subunits). A formatted
#' example is provided (based on the value of `49.95`) to demonstrate the
#' default formatting of each currency.
#'
#' @details
#'
#' There are 172 currencies, which can lead to a verbose display table. To make
#' this presentation more focused on retrieval, we can provide an initial letter
#' corresponding to the 3-letter currency code to `begins_with`. This will
@@ -262,10 +263,10 @@ info_time_style <- function(locale = NULL) {
#'
#' `singl-kw:[code|symbol]` // *default:* `"code"`
#'
#' The type of currency information provided. Can either be `code` where
#' currency information corresponding to 3-letter currency codes is provided,
#' or `symbol` where currency info for common currency names (e.g., dollar,
#' pound, yen, etc.) is returned.
#' The type of currency information provided. Can either be `"code"` where
#' currency information corresponding to 3-letter/3-number currency codes is
#' provided, or `"symbol"` where currency info for common currency
#' names/symbols (e.g., dollar, pound, yen, etc.) is returned.
#'
#' @param begins_with *Show currencies beginning with a specific letter*
#'
@@ -315,117 +316,203 @@ info_currencies <- function(
begins_with = NULL
) {

if (type[1] == "code") {
# Get the correct `type` value
type <- rlang::arg_match(type)

if (type == "code") {

if (!is.null(begins_with)) {

starting <-
substr(begins_with, 1, 1) %>%
toupper()
starting <- toupper(substr(begins_with, 1, 1))

curr <-
currencies %>%
dplyr::filter(grepl(paste0("^", starting, ".*"), curr_code))
dplyr::filter(currencies, grepl(paste0("^", starting, ".*"), curr_code))

} else {
curr <- currencies
}
tab_1 <- curr
tab_1$symbol <- NULL
tab_1$value <- 49.95
tab_1 <- dplyr::relocate(tab_1, "curr_name")
tab_1 <- gt(tab_1)

for (i in seq_len(nrow(curr))) {

tab_1 <-
fmt_currency(
tab_1,
columns = "value",
rows = i,
currency = curr[[i, "curr_code"]]
)
}

tab_1 <-
tab_spanner(
tab_1,
label = "Identifiers",
columns = c("curr_name", "curr_code", "curr_number")
curr_df <- curr[, c(4, 1, 2, 3)]
curr_df$value <- 49.95

gt_tbl <- gt(curr_df, id = "currencies")

gt_tbl <-
fmt_currency(
gt_tbl,
columns = "value",
currency = from_column(column = "curr_code")
)
tab_1 <-

gt_tbl <-
cols_label(
tab_1,
curr_name = html("Currency\nName"),
curr_code = html("Currency\nCode"),
curr_number = html("Currency\nNumber"),
exponent = "Exp",
value = html("Formatted\nCurrency"),
gt_tbl,
curr_name = md("Currency<br>Name"),
curr_code = md("Alpha<br>Code"),
curr_number = md("Numeric<br>Code"),
exponent = md("No. of<br>Subunits"),
value = md("Formatted<br>Currency"),
)
tab_1 <-

gt_tbl <-
cols_width(
gt_tbl,
curr_name ~ px(325),
curr_code ~ px(75),
curr_number ~ px(85),
exponent ~ px(85),
value ~ px(125)
)

gt_tbl <-
tab_style(
gt_tbl,
style = cell_text(
font = system_fonts(name = "monospace-code"),
size = px(12)
),
locations = cells_body(columns = c(curr_code, curr_number))
)

gt_tbl <-
tab_header(
tab_1,
gt_tbl,
title = md("Currencies Supported in **gt**"),
subtitle = md("Currency codes are used in the `fmt_currency()` function")
subtitle = md(
"Currency codes are used in the `fmt_currency()` function<br><br>"
)
)
tab_1 <-

gt_tbl <- opt_all_caps(gt_tbl)

gt_tbl <- opt_stylize(gt_tbl, style = 6)

gt_tbl <-
tab_style(
tab_1,
style = cell_text(align = "left"),
locations = list(
cells_title(groups = "title"),
cells_title(groups = "subtitle")
)
gt_tbl,
style = cell_text(size = px(24)),
locations = cells_title(groups = "title")
)

return(tab_1)
}
gt_tbl <-
tab_style(
gt_tbl,
style = cell_text(size = px(18)),
locations = cells_title(groups = "subtitle")
)

if (type[1] == "symbol") {
gt_tbl <- opt_align_table_header(gt_tbl, align = "left")

curr <- currency_symbols
gt_tbl <- opt_table_lines(gt_tbl, extent = "none")

# Prepare gt for example.
tab_1 <- currency_symbols
tab_1$symbol <- NULL
tab_1$value <- 49.95
tab_1 <- gt(tab_1)
gt_tbl <- opt_horizontal_padding(gt_tbl, scale = 2)

for (i in seq_len(nrow(curr))) {
gt_tbl <- opt_vertical_padding(gt_tbl, scale = 0.8)

tab_1 <-
fmt_currency(
tab_1,
columns = "value",
rows = i,
currency = curr[[i, "curr_symbol"]]
)
}
gt_tbl <-
tab_options(
gt_tbl,
table.border.top.style = "hidden",
column_labels.border.bottom.style = "hidden",
container.height = px(620)
)

tab_1 <-
gt_tbl <-
tab_style(
gt_tbl,
style = css(position = "sticky", top = "-1em", `z-index` = 10),
locations = cells_column_labels()
)

} else {

curr_df <- currency_symbols
curr_df$symbol <- NULL
curr_df$value <- 49.95

gt_tbl <- gt(curr_df, id = "currency_symbols")

gt_tbl <-
fmt_currency(
gt_tbl,
columns = "value",
currency = from_column(column = "curr_symbol")
)

gt_tbl <-
cols_label(
tab_1,
curr_symbol = html("Currency\nSymbol"),
value = html("Formatted\nCurrency"),
gt_tbl,
curr_symbol = "Currency Symbol Keyword",
value = "Formatted Currency"
)
tab_1 <-
tab_header(
tab_1,
title = md("Currencies Supported in **gt**"),
subtitle = md("Currency symbols are used in the `fmt_currency()` function")

gt_tbl <-
cols_width(
gt_tbl,
curr_symbol ~ px(300),
value ~ px(300)
)

gt_tbl <-
tab_style(
gt_tbl,
style = list(
cell_text(
font = system_fonts(name = "monospace-code"),
size = px(12)
),
cell_borders(
sides = "r",
color = "lightblue",
weight = px(1.5))
),
locations = cells_body(columns = curr_symbol)
)
tab_1 <-

gt_tbl <- opt_all_caps(gt_tbl)

gt_tbl <- opt_stylize(gt_tbl, style = 6)

gt_tbl <-
tab_style(
tab_1,
style = cell_text(align = "left"),
locations = list(
cells_title(groups = "title"),
cells_title(groups = "subtitle")
gt_tbl,
style = cell_text(size = px(24)),
locations = cells_title(groups = "title")
)

gt_tbl <-
tab_style(
gt_tbl,
style = cell_text(size = px(18)),
locations = cells_title(groups = "subtitle")
)

gt_tbl <-
tab_options(
gt_tbl,
table.border.top.style = "hidden",
column_labels.border.bottom.style = "hidden"
)

gt_tbl <-
tab_header(
gt_tbl,
title = md("Currencies Supported in **gt**"),
subtitle = md(
"Currency symbols are used in the `fmt_currency()` function.<br><br>"
)
)

return(tab_1)
gt_tbl <- opt_align_table_header(gt_tbl, align = "left")

gt_tbl <- opt_table_lines(gt_tbl, extent = "none")

gt_tbl <- opt_horizontal_padding(gt_tbl, scale = 2)

gt_tbl <- opt_vertical_padding(gt_tbl, scale = 0.7)
}

gt_tbl
}

#' View a table with info on supported locales
Binary file modified images/man_info_currencies_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/man_info_currencies_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion man/fmt_currency.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions man/info_currencies.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a3434f4

Please sign in to comment.