Skip to contents

Wherever there is numerical data that are zero in value, replacement text may be better for explanatory purposes. The sub_zero() function allows for this replacement through its zero_text argument.

Usage

sub_zero(data, columns = everything(), rows = everything(), zero_text = "nil")

Arguments

data

A table object that is created using the gt() function.

columns

The columns to format. Can either be a series of column names provided in c(), a vector of column indices, or a helper function focused on selections. The select helper functions are: starts_with(), ends_with(), contains(), matches(), one_of(), num_range(), and everything().

rows

Optional rows to format. Providing everything() (the default) results in all rows in columns being formatted. Alternatively, we can supply a vector of row captions within c(), a vector of row indices, or a helper function focused on selections. The select helper functions are: starts_with(), ends_with(), contains(), matches(), one_of(), 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

The text to be used in place of zero values in the rendered table.

Value

An object of class gt_tbl.

Details

Targeting of values is done through columns and additionally by rows (if nothing is provided for rows then entire columns are selected). Conditional formatting is possible by providing a conditional expression to the rows argument. See the Arguments section for more information on this.

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

Function ID

3-17