Skip to content

Commit 6148a35

Browse files
authored
Merge pull request #1647 from rstudio/fmt-icon-multiple-fills
Allow `fill_color` arg in `fmt_icon()` to use of named vector/list
2 parents 5de03a5 + f014ab4 commit 6148a35

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
lines changed

R/format_data.R

+60-5
Original file line numberDiff line numberDiff line change
@@ -11135,13 +11135,16 @@ fmt_country <- function(
1113511135
#'
1113611136
#' @param fill_color *Color of the icon fill*
1113711137
#'
11138-
#' `scalar<character>` // *default:* `NULL` (`optional`)
11138+
#' `scalar<character>|vector<character>` // *default:* `NULL` (`optional`)
1113911139
#'
1114011140
#' The fill color of the icon can be set with `fill_color`; providing a single
1114111141
#' color here will change the color of the fill but not of the icon's 'stroke'
11142-
#' or outline (use `stroke_color` to modify that). If not provided then the
11143-
#' default value of `"currentColor"` is applied so that the fill matches the
11144-
#' color of the parent HTML element's color attribute.
11142+
#' or outline (use `stroke_color` to modify that). A named vector or named
11143+
#' list comprising the icon names with corresponding fill colors can
11144+
#' alternatively be used here (e.g.,
11145+
#' `list("circle-check" = "green", "circle-xmark" = "red"`). If nothing is
11146+
#' provided then the default value of `"currentColor"` is applied so that the
11147+
#' fill matches the color of the parent HTML element's color attribute.
1114511148
#'
1114611149
#' @param fill_alpha *Transparency value for icon fill*
1114711150
#'
@@ -11416,6 +11419,45 @@ fmt_country <- function(
1141611419
#' `r man_get_image_tag(file = "man_fmt_icon_4.png")`
1141711420
#' }}
1141811421
#'
11422+
#' A fairly common thing to do with icons in tables is to indicate whether
11423+
#' a quantity is either higher or lower than another. Up and down arrow symbols
11424+
#' can serve as good visual indicators for this purpose. We can make use of the
11425+
#' `"up-arrow"` and `"down-arrow"` icons here. The `fmt_icon()` function has to
11426+
#' find those text values in cells to generate the icons, so, lets generate the
11427+
#' text within a new column via the [cols_add()] function (an expression is used
11428+
#' therein to generate the correct text given the `close` and `open` values).
11429+
#' Following that, `fmt_icon()` is used and its `fill_color` argument is
11430+
#' provided with a named vector that indicates which color should be used for
11431+
#' each icon.
11432+
#'
11433+
#' ```r
11434+
#' sp500 |>
11435+
#' dplyr::slice_head(n = 10) |>
11436+
#' dplyr::select(date, open, close) |>
11437+
#' dplyr::arrange(-dplyr::row_number()) |>
11438+
#' gt(rowname_col = "date") |>
11439+
#' cols_add(week = date, .after = date) |>
11440+
#' cols_add(dir = ifelse(close > open, "arrow-up", "arrow-down")) |>
11441+
#' cols_merge(columns = c(date, week), pattern = "{1} ({2})") |>
11442+
#' fmt_date(columns = date, date_style = "m_day_year") |>
11443+
#' fmt_datetime(columns = week, format = "w", pattern = "W{x}") |>
11444+
#' fmt_currency() |>
11445+
#' fmt_icon(
11446+
#' columns = dir,
11447+
#' fill_color = c("arrow-up" = "green", "arrow-down" = "red")
11448+
#' ) |>
11449+
#' cols_label(
11450+
#' open = "Opening Value",
11451+
#' close = "Closing Value",
11452+
#' dir = ""
11453+
#' ) |>
11454+
#' opt_stylize(style = 1, color = "gray")
11455+
#' ```
11456+
#'
11457+
#' \if{html}{\out{
11458+
#' `r man_get_image_tag(file = "man_fmt_icon_5.png")`
11459+
#' }}
11460+
#'
1141911461
#' @family data formatting functions
1142011462
#' @section Function ID:
1142111463
#' 3-24
@@ -11567,6 +11609,8 @@ fmt_icon <- function(
1156711609

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

11612+
fill_color_named <- rlang::is_named(fill_color)
11613+
1157011614
x_str_non_missing <-
1157111615
vapply(
1157211616
seq_along(x_str_non_missing),
@@ -11593,11 +11637,22 @@ fmt_icon <- function(
1159311637

1159411638
for (y in seq_along(icons)) {
1159511639

11640+
icon_name_i <- icons[y]
11641+
11642+
if (
11643+
fill_color_named &&
11644+
rlang::has_name(fill_color, name = icon_name_i)
11645+
) {
11646+
fill_color_i <- fill_color[[icon_name_i]]
11647+
} else {
11648+
fill_color_i <- fill_color
11649+
}
11650+
1159611651
out_y <-
1159711652
as.character(
1159811653
fontawesome::fa(
1159911654
name = icons[y],
11600-
fill = fill_color,
11655+
fill = fill_color_i,
1160111656
fill_opacity = fill_alpha,
1160211657
stroke = stroke_color,
1160311658
stroke_width = stroke_width,

images/man_fmt_icon_5.png

241 KB
Loading

man/fmt_icon.Rd

+45-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)