Extract the table body from a gt objectSource:
We can extract the body of a gt table, even at various stages of its
rendering, from a
gt_tbl object using the
extract_body() function. By
default, the data frame returned will have gone through all of the build
stages but we can intercept the table body after a certain build stage.
Here are the eight different build stages and some notes about each:
"init": the body table is initialized here, entirely with
NAvalues. It's important to note that all columns of the are of the
charactertype in this first stage. And all columns remain in the same order as the input data table.
"fmt_applied": Any cell values that have had formatting applied to them are migrated to the body table. All other cells remain as
NAvalues. Depending on the
outputtype, the formatting may also be different.
"sub_applied": Any cell values that have had substitution functions applied to them (whether or not they were previously formatted) are migrated to the body table or modified in place (if formatted). All cells that had neither been formatted nor undergone substitution remain as
"unfmt_included": All cells that either didn't have any formatting or any substitution operations applied are migrated to the body table.
NAvalues now become the string
"NA", so, there aren't any true missing values in this body table.
"cols_merged": The result of column-merging operations (though
cols_merge()and related functions) is materialized here. Columns that were asked to be hidden will be present here (i.e., hiding columns doesn't remove them from the body table).
"body_reassembled": Though columns do not move positions rows can move to different positions, and this is usually due to migration to different row groups. At this stage, rows will be in the finalized order that is seen in the associated display table.
text_*()functions in gt can operate on body cells (now fully formatted at this stage) and return transformed character values. After this stage, the effects of those functions are apparent.
"footnotes_attached": Footnote marks are attached to body cell values (either on the left or right of the content). This stage performs said attachment.
extract_body( data, build_stage = NULL, output = c("html", "latex", "rtf", "word") )
The gt table data object
This is the gt table object that is commonly created through use of the
The build stage of the formatted R data frame
When a gt undergoes rendering, the body of the table proceeds through several build stages. Providing a single stage name will yield a data frame that has been extracted after completed that stage. Here are the build stages in order: (1)
"text_transformed", and (8)
"footnotes_attached". If not supplying a value for
build_stagethen the entire build for the table body (i.e., up to and including the
"footnotes_attached"stage) will be performed before returning the data frame.
The output format of the resulting data frame. This can either be