Skip to contents

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)