text_transform: Perform targeted text transformation with a function

Description Usage Arguments Value Figures Function ID See Also Examples

View source: R/text_transform.R

Description

Perform targeted text transformation with a function

Usage

1
text_transform(data, locations, fn)

Arguments

data

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

locations

The cell or set of cells to be associated with the text transformation. Only the cells_body(), cells_stub(), and cells_column_labels() helper functions can be used here. We can enclose several of these calls within a list() if we wish to make the transformation happen at different locations.

fn

The function to use for text transformation.

Value

An object of class gt_tbl.

Figures

Function ID

3-14

See Also

Other Format Data: data_color(), fmt_bytes(), fmt_currency(), fmt_datetime(), fmt_date(), fmt_engineering(), fmt_integer(), fmt_markdown(), fmt_missing(), fmt_number(), fmt_passthrough(), fmt_percent(), fmt_scientific(), fmt_time(), fmt()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Use `exibble` to create a gt table;
# transform the formatted text in the
# `num` and `currency` columns using
# a function within `text_transform()`,
# where `x` is a formatted vector of
# column values
tab_1 <-
  exibble %>%
  dplyr::select(num, char, currency) %>%
  dplyr::slice(1:4) %>%
  gt() %>%
  fmt_number(columns = num) %>%
  fmt_currency(columns = currency) %>%
  text_transform(
    locations = cells_body(
      columns = num
    ),
    fn = function(x) {
      paste0(
        x, " (",
        dplyr::case_when(
          x > 20   ~ "large",
          x <= 20  ~ "small"),
        ")")
    }
  )

gt documentation built on Aug. 7, 2021, 9:06 a.m.