Wherever there is numerical data that are zero in value, replacement text may
be better for explanatory purposes. sub_zero()
allows for this replacement
through its zero_text
argument.
Usage
sub_zero(data, columns = everything(), rows = everything(), zero_text = "nil")
Arguments
- data
The gt table data object
obj:<gt_tbl>
// requiredThis is the gt table object that is commonly created through use of the
gt()
function.- columns
Columns to target
<column-targeting expression>
// default:everything()
The columns to which substitution operations are constrained. Can either be a series of column names provided in
c()
, a vector of column indices, or a select helper function (e.g.starts_with()
,ends_with()
,contains()
,matches()
,num_range()
, andeverything()
).- rows
Rows to target
<row-targeting expression>
// default:everything()
In conjunction with
columns
, we can specify which of their rows should form a constraint for targeting operations. The defaulteverything()
results in all rows incolumns
being formatted. Alternatively, we can supply a vector of row IDs withinc()
, a vector of row indices, or a select helper function (e.g.starts_with()
,ends_with()
,contains()
,matches()
,num_range()
, andeverything()
). We can also use expressions to filter down to the rows we need (e.g.,[colname_1] > 100 & [colname_2] < 50
).- zero_text
Replacement text for zero values
scalar<character>
// default:"nil"
The text to be used in place of zero values in the rendered table. We can optionally use
md()
orhtml()
to style the text as Markdown or to retain HTML elements in the text.
Examples
Let's generate a simple, single-column tibble that contains an assortment of values that could potentially undergo some substitution.
tbl <- dplyr::tibble(num = c(10^(-1:2), 0, 0, 10^(4:6)))
tbl
#> # A tibble: 9 x 1
#> num
#> <dbl>
#> 1 0.1
#> 2 1
#> 3 10
#> 4 100
#> 5 0
#> 6 0
#> 7 10000
#> 8 100000
#> 9 1000000
With this table, the zero values in will be given replacement text with a
single call of sub_zero()
.
tbl |>
gt() |>
fmt_number(columns = num) |>
sub_zero()
See also
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_tf()
,
fmt_time()
,
fmt_units()
,
fmt_url()
,
sub_large_vals()
,
sub_missing()
,
sub_small_vals()
,
sub_values()