fmt_fraction | R Documentation |
With numeric values in a gt table, we can perform mixed-fraction-based formatting. There are several options for setting the accuracy of the fractions. Furthermore, there is an option for choosing a layout (i.e., typesetting style) for the mixed-fraction output.
The following options are available for controlling this type of formatting:
accuracy: how to express the fractional part of the mixed fractions; there are three keyword options for this and an allowance for arbitrary denominator settings
simplification: an option to simplify fractions whenever possible
layout: We can choose to output values with diagonal or inline fractions
digit grouping separators: options to enable/disable digit separators and provide a choice of separator symbol for the whole number portion
pattern: option to use a text pattern for decoration of the formatted mixed fractions
locale-based formatting: providing a locale ID will result in number formatting specific to the chosen locale
fmt_fraction( data, columns, rows = everything(), accuracy = NULL, simplify = TRUE, layout = c("diagonal", "inline"), use_seps = TRUE, pattern = "{x}", sep_mark = ",", system = c("intl", "ind"), locale = NULL )
data |
A table object that is created using the |
columns |
The columns to format. Can either be a series of column names
provided in |
rows |
Optional rows to format. Providing |
accuracy |
The type of fractions to generate. This can either be one of
the keywords |
simplify |
If choosing to provide a numeric value for |
layout |
For HTML output, the |
use_seps |
An option to use digit group separators. The type of digit
group separator is set by |
pattern |
A formatting pattern that allows for decoration of the
formatted value. The value itself is represented by |
sep_mark |
The mark to use as a separator between groups of digits
(e.g., using |
system |
The numbering system to use. By default, this is the
international numbering system ( |
locale |
An optional locale ID that can be used for formatting the value
according the locale's rules. Examples include |
Targeting of values is done through columns
and additionally by rows
(if
nothing is provided for rows
then entire columns are selected). A number of
helper functions exist to make targeting more effective. Conditional
formatting is possible by providing a conditional expression to the rows
argument. See the Arguments section for more information on this.
An object of class gt_tbl
.
Use pizzaplace
to create a gt table. Format the f_sold
and
f_income
columns to display fractions.
pizzaplace %>% dplyr::group_by(type, size) %>% dplyr::summarize( sold = dplyr::n(), income = sum(price), .groups = "drop_last" ) %>% dplyr::group_by(type) %>% dplyr::mutate( f_sold = sold / sum(sold), f_income = income / sum(income), ) %>% dplyr::arrange(type, dplyr::desc(income)) %>% gt(rowname_col = "size") %>% tab_header( title = "Pizzas Sold in 2015", subtitle = "Fraction of Sell Count and Revenue by Size per Type" ) %>% fmt_integer(columns = sold) %>% fmt_currency(columns = income) %>% fmt_fraction( columns = starts_with("f_"), accuracy = 10, simplify = FALSE ) %>% sub_missing(missing_text = "") %>% tab_spanner( label = "Sold", columns = contains("sold") ) %>% tab_spanner( label = "Revenue", columns = contains("income") ) %>% text_transform( locations = cells_body(), fn = function(x) { dplyr::case_when( x == 0 ~ "<em>nil</em>", x != 0 ~ x ) } ) %>% cols_label( sold = "Amount", income = "Amount", f_sold = md("_f_"), f_income = md("_f_") ) %>% cols_align(align = "center", columns = starts_with("f")) %>% tab_options( table.width = px(400), row_group.as_column = TRUE )
3-7
Other Format Data:
data_color()
,
fmt_bytes()
,
fmt_currency()
,
fmt_datetime()
,
fmt_date()
,
fmt_engineering()
,
fmt_integer()
,
fmt_markdown()
,
fmt_number()
,
fmt_partsper()
,
fmt_passthrough()
,
fmt_percent()
,
fmt_scientific()
,
fmt_time()
,
fmt()
,
sub_large_vals()
,
sub_missing()
,
sub_small_vals()
,
sub_zero()
,
text_transform()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.