Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fill_color arg in fmt_icon() to use of named vector/list #1647

Merged
merged 6 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -11135,13 +11135,16 @@ fmt_country <- function(
#'
#' @param fill_color *Color of the icon fill*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#' `scalar<character>|vector<character>` // *default:* `NULL` (`optional`)
#'
#' The fill color of the icon can be set with `fill_color`; providing a single
#' color here will change the color of the fill but not of the icon's 'stroke'
#' or outline (use `stroke_color` to modify that). If not provided then the
#' default value of `"currentColor"` is applied so that the fill matches the
#' color of the parent HTML element's color attribute.
#' or outline (use `stroke_color` to modify that). A named vector or named
#' list comprising the icon names with corresponding fill colors can
#' alternatively be used here (e.g.,
#' `list("circle-check" = "green", "circle-xmark" = "red"`). If nothing is
#' provided then the default value of `"currentColor"` is applied so that the
#' fill matches the color of the parent HTML element's color attribute.
#'
#' @param fill_alpha *Transparency value for icon fill*
#'
Expand Down Expand Up @@ -11416,6 +11419,45 @@ fmt_country <- function(
#' `r man_get_image_tag(file = "man_fmt_icon_4.png")`
#' }}
#'
#' A fairly common thing to do with icons in tables is to indicate whether
#' a quantity is either higher or lower than another. Up and down arrow symbols
#' can serve as good visual indicators for this purpose. We can make use of the
#' `"up-arrow"` and `"down-arrow"` icons here. The `fmt_icon()` function has to
#' find those text values in cells to generate the icons, so, lets generate the
#' text within a new column via the [cols_add()] function (an expression is used
#' therein to generate the correct text given the `close` and `open` values).
#' Following that, `fmt_icon()` is used and its `fill_color` argument is
#' provided with a named vector that indicates which color should be used for
#' each icon.
#'
#' ```r
#' sp500 |>
#' dplyr::slice_head(n = 10) |>
#' dplyr::select(date, open, close) |>
#' dplyr::arrange(-dplyr::row_number()) |>
#' gt(rowname_col = "date") |>
#' cols_add(week = date, .after = date) |>
#' cols_add(dir = ifelse(close > open, "arrow-up", "arrow-down")) |>
#' cols_merge(columns = c(date, week), pattern = "{1} ({2})") |>
#' fmt_date(columns = date, date_style = "m_day_year") |>
#' fmt_datetime(columns = week, format = "w", pattern = "W{x}") |>
#' fmt_currency() |>
#' fmt_icon(
#' columns = dir,
#' fill_color = c("arrow-up" = "green", "arrow-down" = "red")
#' ) |>
#' cols_label(
#' open = "Opening Value",
#' close = "Closing Value",
#' dir = ""
#' ) |>
#' opt_stylize(style = 1, color = "gray")
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_fmt_icon_5.png")`
#' }}
#'
#' @family data formatting functions
#' @section Function ID:
#' 3-24
Expand Down Expand Up @@ -11567,6 +11609,8 @@ fmt_icon <- function(

x_str_non_missing <- x[!is.na(x)]

fill_color_named <- rlang::is_named(fill_color)

x_str_non_missing <-
vapply(
seq_along(x_str_non_missing),
Expand All @@ -11593,11 +11637,22 @@ fmt_icon <- function(

for (y in seq_along(icons)) {

icon_name_i <- icons[y]

if (
fill_color_named &&
rlang::has_name(fill_color, name = icon_name_i)
) {
fill_color_i <- fill_color[[icon_name_i]]
} else {
fill_color_i <- fill_color
}

out_y <-
as.character(
fontawesome::fa(
name = icons[y],
fill = fill_color,
fill = fill_color_i,
fill_opacity = fill_alpha,
stroke = stroke_color,
stroke_width = stroke_width,
Expand Down
Binary file added images/man_fmt_icon_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 45 additions & 4 deletions man/fmt_icon.Rd

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

Loading