fmt_tf | R Documentation |
TRUE
and FALSE
valuesThere can be times where logical values are useful in a gt table. You
might want to express a 'yes' or 'no', a 'true' or 'false', or, perhaps use
pairings of complementary symbols that make sense in a table. The fmt_tf()
function has a set of tf_style
presets that can be used to quickly map
TRUE
/FALSE
values to strings (which are automatically translated
according to a given locale
value), or, symbols like up/down or left/right
arrows and open/closed shapes.
While the presets are nice, you can provide your own mappings through the
true_val
and false_val
arguments. With those you could provide text
(perhaps a Unicode symbol?) or even a fontawesome icon by using
fontawesome::fa("<icon name>")
. The function will automatically handle
alignment when auto_align = TRUE
and try to give you the best look
depending on the options chosen. For extra customization, you can also apply
color to the individual TRUE
, FALSE
, and NA
mappings. Just supply
a vector of colors (up to a length of 3) to the colors
argument.
fmt_tf(
data,
columns = everything(),
rows = everything(),
tf_style = "true-false",
pattern = "{x}",
true_val = NULL,
false_val = NULL,
na_val = NULL,
colors = NULL,
auto_align = TRUE,
locale = NULL
)
data |
The gt table data object
This is the gt table object that is commonly created through use of the
|
columns |
Columns to target
Can either be a series of column names provided in |
rows |
Rows to target
In conjunction with |
tf_style |
Predefined style for
The |
pattern |
Specification of the formatting pattern
A formatting pattern that allows for decoration of the formatted value. The
formatted value is represented by the |
true_val |
Text to use for
While the choice of a |
false_val |
Text to use for
While the choice of a |
na_val |
Text to use for
None of the |
colors |
Colors to use for the resulting strings or symbols
Providing a vector of color values to |
auto_align |
Automatic alignment of the formatted column
The input values may have resulted in an alignment that is not as suitable
once formatting has occurred. With |
locale |
Locale identifier
An optional locale identifier that can be used for formatting values
according the locale's rules. Examples include |
An object of class gt_tbl
.
fmt_tf()
is compatible with body cells that are of the "logical"
(preferred) or "numeric"
types. Any other types of body cells are ignored
during formatting. This is to say that cells of incompatible data types may
be targeted, but there will be no attempt to format them.
There is a special caveat when attempting to format numerical values: the
values must either be exactly 1
(the analogue for TRUE
) or exactly 0
(the analogue for FALSE
). Any other numerical values will be disregarded
and left as is. Because of these restrictions, it is recommended that only
logical values undergo formatting.
from_column()
helper functionfrom_column()
can be used with certain arguments of fmt_tf()
to obtain
varying parameter values from a specified column within the table. This means
that each row could be formatted a little bit differently. These arguments
provide support for from_column()
:
tf_style
pattern
true_val
false_val
na_val
locale
Please note that for each of the aforementioned arguments, a from_column()
call needs to reference a column that has data of the correct type (this is
different for each argument). Additional columns for parameter values can be
generated with cols_add()
(if not already present). Columns that contain
parameter data can also be hidden from final display with cols_hide()
.
Finally, there is no limitation to how many arguments the from_column()
helper is applied so long as the arguments belong to this closed set.
tf_style
argumentWe can supply a preset TRUE
/FALSE
style to the tf_style
argument to
handle the formatting of logical values. There are several such styles and
the first three of them can handle localization to any supported locale
(i.e., the pairs of words for each style will be translated to the language
of the locale
) value.
The following table provides a listing of all valid tf_style
values and a
description of their output values. The output from styles 4
to 10
are
described in terms of the Unicode character names used for the TRUE
and
FALSE
values.
TF Style | Output (for TRUE and FALSE ) |
|
1 | "true-false" | "true" , "false" (locale -aware) |
2 | "yes-no" | "yes" , "no" (locale -aware) |
3 | "up-down" | "up" , "down" (locale -aware) |
4 | "check-mark" | <Heavy Check Mark> , <Heavy Ballot X> |
5 | "circles" | <Black Circle> , <Heavy Circle> |
6 | "squares" | <Black Square> , <White Square> |
7 | "diamonds" | <Black Diamond> , <White Diamond> |
8 | "arrows" | <Upwards Arrow> , <Downwards Arrow> |
9 | "triangles" | <Black Up-Pointing Triangle> , <Black Down-Pointing Triangle> |
10 | "triangles-lr" | <Heavy Check Mark> , <Heavy Ballot X> |
locale
This formatting function can adapt outputs according to a provided locale
value. Examples include "en"
for English (United States) and "fr"
for
French (France). Note that a locale
value provided here will override any
global locale setting performed in gt()
's own locale
argument (it is
settable there as a value received by all other functions that have a
locale
argument). As a useful reference on which locales are supported, we
can call info_locales()
to view an info table.
Let's use a subset of the sp500
dataset to create a small gt table
containing opening and closing price data for a week in 2013. We can add
a logical column (dir
) with cols_add()
; the expression used determines
whether the close
value is greater than the open
value. That new column
is inserted between open
and close
. Then, we use fmt_tf()
to generate
up and down arrows in the dir
column. We elect to use green upward arrows
and red downward arrows (through the colors
option). With a little numeric
formatting and changes to the column labels, the table becomes more presentable.
sp500 |> dplyr::filter(date >= "2013-01-07" & date <= "2013-01-12") |> dplyr::arrange(date) |> dplyr::select(-c(adj_close, volume, high, low)) |> gt(rowname_col = "date") |> cols_add(dir = close > open, .after = open) |> fmt_tf( columns = dir, tf_style = "arrows", colors = c("green", "red") ) |> fmt_currency(columns = c(open, close)) |> cols_label( open = "Opening", close = "Closing", dir = "" )
The reactions
dataset contains chemical kinetic information on a wide
variety of atmospherically-relevant compounds. It might be interesting to get
a summary (for a small subset of compounds) for which rate constants are
available for the selected compounds. We first start by selecting the
relevant rows and columns. Then we generate logical columns for each of the
reaction types (i.e., if a value is NA
then there's no measurement, so
that's FALSE
). Once the gt table has been created, we can use
fmt_tf()
to provide open and filled circles to indicate whether a
particular reaction has been measured and presented in the literature.
reactions |> dplyr::filter(cmpd_type %in% c("carboxylic acid", "alkyne", "allene")) |> dplyr::select(cmpd_name, cmpd_type, ends_with("k298")) |> dplyr::mutate(across(ends_with("k298"), is.na)) |> gt(rowname_col = "cmpd_name", groupname_col = "cmpd_type") |> tab_spanner( label = "Has a measured rate constant", columns = ends_with("k298") ) |> tab_stub_indent( rows = everything(), indent = 2 ) |> fmt_tf( columns = ends_with("k298"), tf_style = "circles" ) |> cols_label( OH_k298 = "OH", O3_k298 = "Ozone", NO3_k298 = "Nitrate", Cl_k298 = "Chlorine" ) |> cols_width( stub() ~ px(200), ends_with("k298") ~ px(80) ) |> opt_vertical_padding(scale = 0.35)
There are census-based population values in the towny
dataset and quite a
few small towns within it. Let's look at the ten smallest towns (according
to the 2021 figures) and work out whether their populations have increased or
declined since 1996. Also, let's determine which of these towns even have a
website. After that data preparation, the data is made into a gt table
and fmt_tf()
can be used in the website
and pop_dir
columns (which both
have TRUE
/FALSE
values). Each of these fmt_tf()
calls will either
produce "yes"
/"no"
or "up"
/"down"
strings (set via the tf_style
option).
towny |> dplyr::arrange(population_2021) |> dplyr::mutate(website = !is.na(website)) |> dplyr::mutate(pop_dir = population_2021 > population_1996) |> dplyr::select(name, website, population_1996, population_2021, pop_dir) |> dplyr::slice_head(n = 10) |> gt(rowname_col = "name") |> tab_spanner( label = "Population", columns = starts_with("pop") ) |> tab_stubhead(label = "Town") |> fmt_tf( columns = website, tf_style = "yes-no", auto_align = FALSE ) |> fmt_tf( columns = pop_dir, tf_style = "up-down", pattern = "It's {x}." ) |> cols_label_with( columns = starts_with("population"), fn = function(x) sub("population_", "", x) ) |> cols_label( website = md("Has a \n website?"), pop_dir = "Pop. direction?" ) |> opt_horizontal_padding(scale = 2)
If formatting to words instead of symbols (with the hyphenated tf_style
keywords), the words themselves can be translated to different languages
if providing a locale
value. In this next example, we're manually creating
a tibble with locale codes and their associated languages. The yes
and up
columns all receive TRUE
whereas no
and down
will all be FALSE
.
With two calls of fmt_tf()
for each of these pairings, we get the columns'
namesake words. To have these words translated, the locale
argument is
pointed toward values in the code
column by using from_column()
.
dplyr::tibble( code = c("de", "fr", "is", "tr", "ka", "lt", "ca", "bg", "lv"), lang = c( "German", "French", "Icelandic", "Turkish", "Georgian", "Lithuanian", "Catalan", "Bulgarian", "Latvian" ), yes = TRUE, no = FALSE, up = TRUE, down = FALSE ) |> gt(rowname_col = "lang") |> tab_header(title = "Common words in a few languages") |> fmt_tf( columns = c(yes, no), tf_style = "yes-no", locale = from_column("code") ) |> fmt_tf( columns = c(up, down), tf_style = "up-down", locale = from_column("code") ) |> cols_merge( columns = c(lang, code), pattern = "{1} ({2})" ) |> cols_width( stub() ~ px(150), everything() ~ px(80) )
3-18
v0.11.0
(July 9, 2024)
Other data formatting functions:
data_color()
,
fmt()
,
fmt_auto()
,
fmt_bins()
,
fmt_bytes()
,
fmt_chem()
,
fmt_country()
,
fmt_currency()
,
fmt_date()
,
fmt_datetime()
,
fmt_duration()
,
fmt_email()
,
fmt_engineering()
,
fmt_flag()
,
fmt_fraction()
,
fmt_icon()
,
fmt_image()
,
fmt_index()
,
fmt_integer()
,
fmt_markdown()
,
fmt_number()
,
fmt_partsper()
,
fmt_passthrough()
,
fmt_percent()
,
fmt_roman()
,
fmt_scientific()
,
fmt_spelled_num()
,
fmt_time()
,
fmt_units()
,
fmt_url()
,
sub_large_vals()
,
sub_missing()
,
sub_small_vals()
,
sub_values()
,
sub_zero()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.