sub_zero: Substitute zero values in the table body

View source: R/substitution.R

sub_zeroR Documentation

Substitute zero values in the table body

Description

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>⁠ // required

This 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(), and everything()).

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 default everything() results in all rows in columns being formatted. Alternatively, we can supply a vector of row IDs within c(), a vector of row indices, or a select helper function (e.g. starts_with(), ends_with(), contains(), matches(), num_range(), and everything()). 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() or html() to style the text as Markdown or to retain HTML elements in the text.

Value

An object of class gt_tbl.

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()
This image of a table was generated from the first code example in the `sub_zero()` help file.

Function ID

3-32

Function Introduced

v0.6.0 (May 24, 2022)

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()


rstudio/gt documentation built on Dec. 2, 2024, 11:05 a.m.