text_case_match: Perform whole or partial text replacements with a...

View source: R/text_transform.R

text_case_matchR Documentation

Perform whole or partial text replacements with a 'switch'-like approach


The text_case_match() function provides a useful interface for a approach to replacing table cells that behaves much like a switch statement. The targeting of cells for transformation happens with the .locations argument. Once overall targeting is handled, you need to supply a sequence of two-sided formulas matching of the general form: ⁠<vector_old_text> ~ <new_text>⁠. In the left hand side (LHS) there should be a character vector containing strings to match on. The right hand side (RHS) should contain a single string (or something coercible to a length one character vector). There's also the .replace argument that changes the matching and replacing behavior. By default, text_case_match() will try to match on entire strings and replace those strings. This can be changed to a partial matching and replacement strategy with the alternate option.


  .default = NULL,
  .replace = c("all", "partial"),
  .locations = cells_body()



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.


Matching expressions

⁠<multiple expressions>⁠ // required

A sequence of two-sided formulas matching this general construction: ⁠<old_text> ~ <new_text>⁠. The left hand side (LHS) determines which values to match on and it can be any length (allowing for new_text to replace different values of old_text). The right hand side (RHS) provides the replacement text (it must resolve to a single value of the character class).


Default replacement text

⁠scalar<character>⁠ // default: NULL (optional)

The replacement text to use when cell values aren't matched by any of the LHS inputs. If NULL, the default, no replacement text will be used.


Method for text replacement

⁠singl-kw:[all|partial]⁠ // default: "all"

A choice in how the matching is to be done. The default "all" means that the old_text (on the LHS of formulas given in ...) must match the cell text completely. With that option, the replacement will completely replace that matched text. With "partial", the match will occur in all substrings of old_text. In this way, the replacements will act on those matched substrings.


Locations to target

⁠<locations expressions>⁠ // default: cells_body()

The cell or set of cells to be associated with the text transformation. Only the cells_body(), cells_stub(), cells_row_groups(), cells_column_labels(), and cells_column_spanners() 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.


An object of class gt_tbl.


Let's use the exibble dataset to create a simple, two-column gt table (keeping only the char and fctr columns). In the char column, we'll transform the NA value to "elderberry" using the text_case_match() function. Over in the fctr column, some more sophisticated matches will be performed using text_case_match(). That column has spelled out numbers and we can produce these on the LHS with help from the vec_fmt_spelled_num() function. The replacements will contain descriptive text. In this last call of text_case_match(), we use a .default to replace text for any of those non-matched cases.

exibble |>
  dplyr::select(char, fctr) |>
  gt() |>
    NA ~ "elderberry",
    .locations = cells_body(columns = char)
  ) |>
    vec_fmt_spelled_num(1:4) ~ "one to four",
    vec_fmt_spelled_num(5:6) ~ "five or six",
    .default = "seven or more",
    .locations = cells_body(columns = fctr)
This image of a table was generated from the first code example in the `text_case_match()` help file.

Next, let's use a transformed version of the towny dataset to create a gt table. Transform the text in the csd_type column using two-sided formulas supplied to text_case_match(). We can replace matches on the LHS with Fontawesome icons furnished by the fontawesome R package.

towny |>
  dplyr::select(name, csd_type, population_2021) |>
  dplyr::filter(csd_type %in% c("city", "town")) |>
  dplyr::group_by(csd_type) |>
  dplyr::arrange(desc(population_2021)) |>
  dplyr::slice_head(n = 5) |>
  dplyr::ungroup() |>
  gt() |>
  fmt_integer() |>
    "city" ~ fontawesome::fa("city"),
    "town" ~ fontawesome::fa("house-chimney")
  ) |>
    name = "City/Town",
    csd_type = "",
    population_2021 = "Population"
This image of a table was generated from the second code example in the `text_case_match()` help file.

Function ID


Function Introduced

v0.9.0 (Mar 31, 2023)

See Also

Other text transforming functions: text_case_when(), text_replace(), text_transform()

gt documentation built on June 22, 2024, 11:11 a.m.