Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

text_transform

Perform targeted text transformation with a function


Description

Perform targeted text transformation with a function

Usage

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-12

See Also

Examples

# 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 = vars(num)) %>%
  fmt_currency(columns = vars(currency)) %>%
  text_transform(
    locations = cells_body(
      columns = vars(num)),
    fn = function(x) {
      paste0(
        x, " (",
        dplyr::case_when(
          x > 20   ~ "large",
          x <= 20  ~ "small"),
        ")")
    }
  )

gt

Easily Create Presentation-Ready Display Tables

v0.2.2
MIT + file LICENSE
Authors
Richard Iannone [aut, cre] (<https://orcid.org/0000-0003-3925-190X>), Joe Cheng [aut], Barret Schloerke [aut] (<https://orcid.org/0000-0001-9986-114X>), RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.