@@ -11135,13 +11135,16 @@ fmt_country <- function(
11135
11135
# '
11136
11136
# ' @param fill_color *Color of the icon fill*
11137
11137
# '
11138
- # ' `scalar<character>` // *default:* `NULL` (`optional`)
11138
+ # ' `scalar<character>|vector<character> ` // *default:* `NULL` (`optional`)
11139
11139
# '
11140
11140
# ' The fill color of the icon can be set with `fill_color`; providing a single
11141
11141
# ' 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.
11145
11148
# '
11146
11149
# ' @param fill_alpha *Transparency value for icon fill*
11147
11150
# '
@@ -11416,6 +11419,45 @@ fmt_country <- function(
11416
11419
# ' `r man_get_image_tag(file = "man_fmt_icon_4.png")`
11417
11420
# ' }}
11418
11421
# '
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
+ # '
11419
11461
# ' @family data formatting functions
11420
11462
# ' @section Function ID:
11421
11463
# ' 3-24
@@ -11567,6 +11609,8 @@ fmt_icon <- function(
11567
11609
11568
11610
x_str_non_missing <- x [! is.na(x )]
11569
11611
11612
+ fill_color_named <- rlang :: is_named(fill_color )
11613
+
11570
11614
x_str_non_missing <-
11571
11615
vapply(
11572
11616
seq_along(x_str_non_missing ),
@@ -11593,11 +11637,22 @@ fmt_icon <- function(
11593
11637
11594
11638
for (y in seq_along(icons )) {
11595
11639
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
+
11596
11651
out_y <-
11597
11652
as.character(
11598
11653
fontawesome :: fa(
11599
11654
name = icons [y ],
11600
- fill = fill_color ,
11655
+ fill = fill_color_i ,
11601
11656
fill_opacity = fill_alpha ,
11602
11657
stroke = stroke_color ,
11603
11658
stroke_width = stroke_width ,
0 commit comments