Perform targeted text transformation with a function

text_transform(data, locations, fn)



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


the cell or set of cells to be associated with the text transformation. Supplying any of the cells_*() helper functions is a useful way to target the location cells that are associated with the footnote text. These helper functions are: cells_title(), cells_column_labels(), cells_group(), cells_stub(), cells_data(), and cells_summary(). Please see the help article location_cells for more information on how these helper functions can be used. Additionally, we can enclose several cells_*() calls within a list() if we wish to link the footnote text to different types of locations (e.g., cell data values, stub group headings, the table title, etc.).


the function to use for text transformation.


an object of class gt_tbl.


See also


# 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_data( columns = vars(num)), fn = function(x) { paste0( x, " (", dplyr::case_when( x > 20 ~ "large", x <= 20 ~ "small"), ")") } ) %>% text_transform( locations = cells_data( columns = vars(currency)), fn = function(x) { ifelse( grepl(",", x), gsub("\\.\\d\\d", "", x), x) } )