extract_cells: Extract a vector of formatted cells from a *gt* object

View source: R/extract.R

extract_cellsR Documentation

Extract a vector of formatted cells from a gt object

Description

Get a vector of cell data from a gt_tbl object. The output vector will have cell data formatted in the same way as the table.

Usage

extract_cells(
  data,
  columns,
  rows = everything(),
  output = c("auto", "plain", "html", "latex", "rtf", "word", "grid")
)

Arguments

data

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.

columns

Columns to target

<column-targeting expression> // default: everything()

Can either be a series of column names provided in c(), a vector of column indices, or a select helper function (e.g. starts_with(), ends_with(), contains(), matches(), num_range() and everything()).

rows

Rows to target

⁠<row-targeting expression>⁠ // default: everything()

In conjunction with columns, we can specify which of their rows should form a constraint for extraction. The default everything() results in all rows in columns being formatted. Alternatively, we can supply a vector of row IDs within c(), a vector of row indices, or a select helper function (e.g. starts_with(), ends_with(), contains(), matches(), 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⁠).

output

Output format

⁠singl-kw:[auto|plain|html|latex|rtf|word]⁠ // default: "auto"

The output style of the resulting character vector. This can either be "auto" (the default), "plain", "html", "latex", "rtf", or "word". In knitr rendering (i.e., Quarto or R Markdown), the "auto" option will choose the correct output value

Value

A vector of cell data extracted from a gt table.

Examples

Let's create a gt table with the exibble dataset to use in the next few examples:

gt_tbl <- gt(exibble, rowname_col = "row", groupname_col = "group")

We can extract a cell from the table with the extract_cells() function. This is done by providing a column and a row intersection:

extract_cells(gt_tbl, columns = num, row = 1)
#> [1] "1.111e-01"

Multiple cells can be extracted. Let's get the first four cells from the char column.

extract_cells(gt_tbl, columns = char, rows = 1:4)
#> [1] "apricot" "banana" "coconut" "durian"

We can format cells and expect that the formatting is fully retained after extraction.

gt_tbl |>
  fmt_number(columns = num, decimals = 2) |>
  extract_cells(columns = num, rows = 1)
#> [1] "0.11"

Function ID

13-9

Function Introduced

v0.8.0 (November 16, 2022)

See Also

Other table export functions: as_gtable(), as_latex(), as_raw_html(), as_rtf(), as_word(), extract_body(), extract_summary(), gtsave()


rstudio/gt documentation built on Dec. 2, 2024, 11:05 a.m.