| Title: | Generate Publication-Ready Statistical Tables |
|---|---|
| Description: | A collection of functions for generating frequency tables and cross-tabulations of categorical variables. The resulting tables can be exported to various formats (Excel, PDF, HTML, etc.) with extensive formatting and layout customization options. |
| Authors: | Bhas Abdulsamad [aut, cre, cph]
|
| Maintainer: | Bhas Abdulsamad <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.4 |
| Built: | 2026-05-14 07:51:21 UTC |
| Source: | https://github.com/yng-me/tsg |
Add a column total
add_column_total(data, name = "total", label_total = "Total", ...)add_column_total(data, name = "total", label_total = "Total", ...)
data |
A data frame, tibble, or |
name |
Column name for total. Default value is |
label_total |
Label for the total column. Default is "Total". |
... |
Additional named arguments to be added as columns alongside the total column. |
The input data frame with an additional column representing the total of each row.
# Example data frame df <- data.frame( category = c("A", "B", "C"), value1 = c(10, 20, 30), value2 = c(5, 15, 25) ) add_column_total(df)# Example data frame df <- data.frame( category = c("A", "B", "C"), value1 = c(10, 20, 30), value2 = c(5, 15, 25) ) add_column_total(df)
This function adds a facade to a tsg table object. A facade is a set of styling options that can be applied to the table to customize its appearance. For Excel output, see openxlsx::createStyle() for all valid values.
add_facade( data, table.offsetRow = 0, table.offsetCol = 0, table.gridLines = NULL, table.tabColour = NULL, table.fontName = NULL, table.fontSize = NULL, table.fontColour = NULL, table.bgFill = NULL, table.fgFill = NULL, table.halign = NULL, table.valign = NULL, table.wrapText = FALSE, table.indent = NULL, table.locked = NULL, table.hidden = NULL, table.decimalPrecision = NULL, table.decimalCols = NULL, table.lastRowBold = NULL, table.width = NULL, table.widthOffset = NULL, title.fontName = NULL, title.fontSize = NULL, title.fontColour = NULL, title.border = NULL, title.borderColour = NULL, title.borderStyle = NULL, title.bgFill = NULL, title.fgFill = NULL, title.halign = NULL, title.valign = NULL, title.textDecoration = NULL, title.wrapText = NULL, title.indent = NULL, title.height = NULL, subtitle.fontName = NULL, subtitle.fontSize = NULL, subtitle.fontColour = NULL, subtitle.border = NULL, subtitle.borderColour = NULL, subtitle.borderStyle = NULL, subtitle.bgFill = NULL, subtitle.fgFill = NULL, subtitle.halign = NULL, subtitle.valign = NULL, subtitle.textDecoration = NULL, subtitle.wrapText = NULL, subtitle.indent = NULL, subtitle.height = NULL, header.fontName = NULL, header.fontSize = NULL, header.fontColour = NULL, header.border = NULL, header.borderColour = NULL, header.borderStyle = NULL, header.bgFill = NULL, header.fgFill = NULL, header.halign = NULL, header.valign = NULL, header.textDecoration = NULL, header.wrapText = NULL, header.indent = NULL, header.height = NULL, spanner.fontName = NULL, spanner.fontSize = NULL, spanner.fontColour = NULL, spanner.border = NULL, spanner.borderColour = NULL, spanner.borderStyle = NULL, spanner.bgFill = NULL, spanner.fgFill = NULL, spanner.halign = NULL, spanner.valign = NULL, spanner.textDecoration = NULL, spanner.wrapText = NULL, spanner.indent = NULL, spanner.height = NULL, body.fontName = NULL, body.fontSize = NULL, body.fontColour = NULL, body.numFmt = NULL, body.border = NULL, body.borderColour = NULL, body.borderStyle = NULL, body.bgFill = NULL, body.fgFill = NULL, body.halign = NULL, body.valign = NULL, body.textDecoration = NULL, body.wrapText = NULL, body.indent = NULL, body.height = NULL, col_first.fontName = NULL, col_first.fontSize = NULL, col_first.fontColour = NULL, col_first.numFmt = NULL, col_first.border = NULL, col_first.borderColour = NULL, col_first.borderStyle = NULL, col_first.bgFill = NULL, col_first.fgFill = NULL, col_first.halign = NULL, col_first.valign = NULL, col_first.textDecoration = NULL, col_first.wrapText = NULL, col_first.indent = NULL, col_first.width = NULL, col_last.fontName = NULL, col_last.fontSize = NULL, col_last.fontColour = NULL, col_last.numFmt = NULL, col_last.border = NULL, col_last.borderColour = NULL, col_last.borderStyle = NULL, col_last.bgFill = NULL, col_last.fgFill = NULL, col_last.halign = NULL, col_last.valign = NULL, col_last.textDecoration = NULL, col_last.wrapText = NULL, col_last.indent = NULL, col_last.width = NULL, row_group.fontName = NULL, row_group.fontSize = NULL, row_group.fontColour = NULL, row_group.border = NULL, row_group.borderColour = NULL, row_group.borderStyle = NULL, row_group.bgFill = NULL, row_group.fgFill = NULL, row_group.halign = NULL, row_group.valign = NULL, row_group.textDecoration = NULL, row_group.wrapText = NULL, row_group.indent = NULL, row_group.width = NULL, row_group.height = NULL, source_note.fontName = NULL, source_note.fontSize = NULL, source_note.fontColour = NULL, source_note.border = NULL, source_note.borderColour = NULL, source_note.borderStyle = NULL, source_note.bgFill = NULL, source_note.fgFill = NULL, source_note.halign = NULL, source_note.valign = NULL, source_note.textDecoration = NULL, source_note.wrapText = NULL, source_note.indent = NULL, source_note.height = NULL, footnotes.fontName = NULL, footnotes.fontSize = NULL, footnotes.fontColour = NULL, footnotes.border = NULL, footnotes.borderColour = NULL, footnotes.borderStyle = NULL, footnotes.bgFill = NULL, footnotes.fgFill = NULL, footnotes.halign = NULL, footnotes.valign = NULL, footnotes.textDecoration = NULL, footnotes.wrapText = NULL, footnotes.indent = NULL, footnotes.height = NULL, border_header.border = NULL, border_header.borderColour = NULL, border_header.borderStyle = NULL, border_outer.borderColour = NULL, border_bottom.height = NULL )add_facade( data, table.offsetRow = 0, table.offsetCol = 0, table.gridLines = NULL, table.tabColour = NULL, table.fontName = NULL, table.fontSize = NULL, table.fontColour = NULL, table.bgFill = NULL, table.fgFill = NULL, table.halign = NULL, table.valign = NULL, table.wrapText = FALSE, table.indent = NULL, table.locked = NULL, table.hidden = NULL, table.decimalPrecision = NULL, table.decimalCols = NULL, table.lastRowBold = NULL, table.width = NULL, table.widthOffset = NULL, title.fontName = NULL, title.fontSize = NULL, title.fontColour = NULL, title.border = NULL, title.borderColour = NULL, title.borderStyle = NULL, title.bgFill = NULL, title.fgFill = NULL, title.halign = NULL, title.valign = NULL, title.textDecoration = NULL, title.wrapText = NULL, title.indent = NULL, title.height = NULL, subtitle.fontName = NULL, subtitle.fontSize = NULL, subtitle.fontColour = NULL, subtitle.border = NULL, subtitle.borderColour = NULL, subtitle.borderStyle = NULL, subtitle.bgFill = NULL, subtitle.fgFill = NULL, subtitle.halign = NULL, subtitle.valign = NULL, subtitle.textDecoration = NULL, subtitle.wrapText = NULL, subtitle.indent = NULL, subtitle.height = NULL, header.fontName = NULL, header.fontSize = NULL, header.fontColour = NULL, header.border = NULL, header.borderColour = NULL, header.borderStyle = NULL, header.bgFill = NULL, header.fgFill = NULL, header.halign = NULL, header.valign = NULL, header.textDecoration = NULL, header.wrapText = NULL, header.indent = NULL, header.height = NULL, spanner.fontName = NULL, spanner.fontSize = NULL, spanner.fontColour = NULL, spanner.border = NULL, spanner.borderColour = NULL, spanner.borderStyle = NULL, spanner.bgFill = NULL, spanner.fgFill = NULL, spanner.halign = NULL, spanner.valign = NULL, spanner.textDecoration = NULL, spanner.wrapText = NULL, spanner.indent = NULL, spanner.height = NULL, body.fontName = NULL, body.fontSize = NULL, body.fontColour = NULL, body.numFmt = NULL, body.border = NULL, body.borderColour = NULL, body.borderStyle = NULL, body.bgFill = NULL, body.fgFill = NULL, body.halign = NULL, body.valign = NULL, body.textDecoration = NULL, body.wrapText = NULL, body.indent = NULL, body.height = NULL, col_first.fontName = NULL, col_first.fontSize = NULL, col_first.fontColour = NULL, col_first.numFmt = NULL, col_first.border = NULL, col_first.borderColour = NULL, col_first.borderStyle = NULL, col_first.bgFill = NULL, col_first.fgFill = NULL, col_first.halign = NULL, col_first.valign = NULL, col_first.textDecoration = NULL, col_first.wrapText = NULL, col_first.indent = NULL, col_first.width = NULL, col_last.fontName = NULL, col_last.fontSize = NULL, col_last.fontColour = NULL, col_last.numFmt = NULL, col_last.border = NULL, col_last.borderColour = NULL, col_last.borderStyle = NULL, col_last.bgFill = NULL, col_last.fgFill = NULL, col_last.halign = NULL, col_last.valign = NULL, col_last.textDecoration = NULL, col_last.wrapText = NULL, col_last.indent = NULL, col_last.width = NULL, row_group.fontName = NULL, row_group.fontSize = NULL, row_group.fontColour = NULL, row_group.border = NULL, row_group.borderColour = NULL, row_group.borderStyle = NULL, row_group.bgFill = NULL, row_group.fgFill = NULL, row_group.halign = NULL, row_group.valign = NULL, row_group.textDecoration = NULL, row_group.wrapText = NULL, row_group.indent = NULL, row_group.width = NULL, row_group.height = NULL, source_note.fontName = NULL, source_note.fontSize = NULL, source_note.fontColour = NULL, source_note.border = NULL, source_note.borderColour = NULL, source_note.borderStyle = NULL, source_note.bgFill = NULL, source_note.fgFill = NULL, source_note.halign = NULL, source_note.valign = NULL, source_note.textDecoration = NULL, source_note.wrapText = NULL, source_note.indent = NULL, source_note.height = NULL, footnotes.fontName = NULL, footnotes.fontSize = NULL, footnotes.fontColour = NULL, footnotes.border = NULL, footnotes.borderColour = NULL, footnotes.borderStyle = NULL, footnotes.bgFill = NULL, footnotes.fgFill = NULL, footnotes.halign = NULL, footnotes.valign = NULL, footnotes.textDecoration = NULL, footnotes.wrapText = NULL, footnotes.indent = NULL, footnotes.height = NULL, border_header.border = NULL, border_header.borderColour = NULL, border_header.borderStyle = NULL, border_outer.borderColour = NULL, border_bottom.height = NULL )
data |
A tsg table object to which the facade will be added. This is typically a data frame or tibble that has been processed using tsg functions. |
table.offsetRow |
Row offset of the table |
table.offsetCol |
Column offset of the table |
table.gridLines |
Boolean indicating whether to show grid lines in the table |
table.tabColour |
Color of the table tab (Excel worksheet) in the output file. Can be a hexadecimal color code (e.g., "#FF0000" for red) or a named color (e.g., "red"). |
table.fontName, title.fontName, subtitle.fontName, header.fontName, spanner.fontName, body.fontName, row_group.fontName, col_first.fontName, col_last.fontName, source_note.fontName, footnotes.fontName
|
Font name or font family for the table, title, subtitle, header, spanner, body, row group header, source note, and footnotes respectively. |
table.fontSize, title.fontSize, subtitle.fontSize, header.fontSize, spanner.fontSize, body.fontSize, col_first.fontSize, col_last.fontSize, row_group.fontSize, source_note.fontSize, footnotes.fontSize
|
Font size for the table, title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. |
table.fontColour, title.fontColour, subtitle.fontColour, header.fontColour, spanner.fontColour, body.fontColour, col_first.fontColour, col_last.fontColour, row_group.fontColour, source_note.fontColour, footnotes.fontColour
|
Font color for the table, title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. Can be a hexadecimal color code (e.g., "#FF0000" for red) or a named color (e.g., "red"). |
table.bgFill, title.bgFill, subtitle.bgFill, header.bgFill, spanner.bgFill, body.bgFill, col_first.bgFill, col_last.bgFill, row_group.bgFill, source_note.bgFill, footnotes.bgFill
|
Background fill color for the table, title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. Can be a hexadecimal color code (e.g., "#FF0000" for red) or a named color (e.g., "red"). |
table.fgFill, title.fgFill, subtitle.fgFill, header.fgFill, spanner.fgFill, body.fgFill, col_first.fgFill, col_last.fgFill, row_group.fgFill, source_note.fgFill, footnotes.fgFill
|
Foreground fill color for the table, title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. Can be a hexadecimal color code (e.g., "#FF0000" for red) or a named color (e.g., "red"). |
table.halign, title.halign, subtitle.halign, header.halign, spanner.halign, body.halign, col_first.halign, col_last.halign, row_group.halign, source_note.halign, footnotes.halign
|
Horizontal alignment for the table, title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. Can be "left", "center", or "right". |
table.valign, title.valign, subtitle.valign, header.valign, spanner.valign, body.valign, col_first.valign, col_last.valign, row_group.valign, source_note.valign, footnotes.valign
|
Vertical alignment for the table, title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. Can be "top", "middle", or "bottom". |
table.wrapText, title.wrapText, subtitle.wrapText, header.wrapText, spanner.wrapText, body.wrapText, col_first.wrapText, col_last.wrapText, row_group.wrapText, source_note.wrapText, footnotes.wrapText
|
Logical indicating whether to wrap text in the table, title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. |
table.indent, title.indent, subtitle.indent, header.indent, spanner.indent, body.indent, col_first.indent, col_last.indent, row_group.indent, source_note.indent, footnotes.indent
|
Indentation for the table, title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. Can be a numeric value indicating the number of spaces to indent. Defaults to NULL. |
table.locked |
Logical indicating whether the table is locked. |
|
Logical indicating whether the table (Excel worksheet) is hidden. |
|
table.decimalPrecision |
Numeric value indicating the number of decimal places to display in numeric columns. |
table.decimalCols |
Character vector of column names that should have decimal formatting applied. |
table.lastRowBold |
Logical indicating whether the last row of the table should be bold. |
table.width, col_first.width, col_last.width, row_group.width
|
Column widths for the table, first column, last column, and row group header respectively. Can be a numeric value indicating the width in points. |
table.widthOffset |
Numeric value indicating the width offset for the table. |
title.border, subtitle.border, header.border, spanner.border, body.border, col_first.border, col_last.border, row_group.border, source_note.border, footnotes.border, border_header.border
|
Border style for the title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. Can be a string representing the border style. The |
title.borderColour, subtitle.borderColour, header.borderColour, spanner.borderColour, body.borderColour, col_first.borderColour, col_last.borderColour, row_group.borderColour, source_note.borderColour, footnotes.borderColour, border_header.borderColour, border_outer.borderColour
|
Border color for the title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. Can be a hexadecimal color code (e.g., "#FF0000" for red) or a named color (e.g., "red"). The |
title.borderStyle, subtitle.borderStyle, header.borderStyle, spanner.borderStyle, body.borderStyle, col_first.borderStyle, col_last.borderStyle, row_group.borderStyle, source_note.borderStyle, footnotes.borderStyle, border_header.borderStyle
|
Border style for the title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. The |
title.textDecoration, subtitle.textDecoration, header.textDecoration, spanner.textDecoration, body.textDecoration, col_first.textDecoration, col_last.textDecoration, row_group.textDecoration, source_note.textDecoration, footnotes.textDecoration
|
Text decoration for the title, subtitle, header, spanner, body, first column, last column, row group header, source note, and footnotes respectively. |
title.height, subtitle.height, header.height, spanner.height, body.height, row_group.height, source_note.height, footnotes.height, border_bottom.height
|
Height for the title, subtitle, header, spanner, body, row group, source note, footnotes, and bottom border of the table respectively. Can be a numeric value indicating the height in points. |
body.numFmt, col_first.numFmt, col_last.numFmt
|
Numeric format for the body, first column, and last column respectively. Can be a string representing the numeric format. |
A tsg object with the specified facade settings applied as attributes.
person_record |> generate_frequency(sex) |> add_facade(table.offsetRow = 2, table.offsetCol = 1)person_record |> generate_frequency(sex) |> add_facade(table.offsetRow = 2, table.offsetCol = 1)
This function adds a facade to a tsg, an alternative way to allow dynamic values and programmatic evaluation.
add_facade_alt(data, ...)add_facade_alt(data, ...)
data |
a |
... |
List of supported facade items (see |
A tsg object with the specified facade settings applied as attributes.
person_record |> generate_frequency(sex) |> add_facade_alt(table.offsetRow = 2, table.offsetCol = 1)person_record |> generate_frequency(sex) |> add_facade_alt(table.offsetRow = 2, table.offsetCol = 1)
Add a footnote attribute to a table
add_footnote( data, footnote, locations = NULL, placement = c("auto", "right", "left") )add_footnote( data, footnote, locations = NULL, placement = c("auto", "right", "left") )
data |
A data frame, tibble, or |
footnote |
The footnote text to be added (a single character string). |
locations |
Optional character vector of column names to anchor the footnote marker.
When supplied, a footnote reference symbol is placed in those column headers.
Column-level anchoring is supported in HTML and PDF output (via gt);
XLSX and Word output include the text without cell-level markers.
Default |
placement |
Horizontal alignment of the footnote in the output footer.
One of |
The input data frame with an updated footnotes attribute
(a list with elements $text, $placement, and $locations).
tbl <- person_record |> generate_frequency(sex) # Whole-table footer, left-aligned (default) tbl |> add_footnote("Source: National Survey 2023.") # Right-aligned footer note tbl |> add_footnote("Weighted estimates.", placement = "right") # Footnote anchored to a specific column header (HTML/PDF) tbl |> add_footnote("Unweighted count.", locations = "frequency")tbl <- person_record |> generate_frequency(sex) # Whole-table footer, left-aligned (default) tbl |> add_footnote("Source: National Survey 2023.") # Right-aligned footer note tbl |> add_footnote("Weighted estimates.", placement = "right") # Footnote anchored to a specific column header (HTML/PDF) tbl |> add_footnote("Unweighted count.", locations = "frequency")
Add a row total
add_row_total( data, position = c("bottom", "top"), label_total = "Total", fill = "-" )add_row_total( data, position = c("bottom", "top"), label_total = "Total", fill = "-" )
data |
A data frame, tibble, or |
position |
Position to add the total row. Either "bottom" (default) or "top". |
label_total |
Label for the total row in the category column. Default is "Total". |
fill |
Character. Value to fill in for missing numeric columns in the total row. Default is "-". |
The input data frame with an additional row representing the total of numeric columns.
# Example data frame df <- data.frame( category = c("A", "B", "C"), value1 = c(10, 20, 30), value2 = c(5, 15, 25) ) df_with_total <- add_row_total(df) df_with_total_top <- add_row_total(df, position = "top")# Example data frame df <- data.frame( category = c("A", "B", "C"), value1 = c(10, 20, 30), value2 = c(5, 15, 25) ) df_with_total <- add_row_total(df) df_with_total_top <- add_row_total(df, position = "top")
Add a source note attribute to a table
add_source_note(data, source_note)add_source_note(data, source_note)
data |
A data frame, tibble, or |
source_note |
The source note text to be added. |
The input data frame with an added source note attribute.
add_source_note( dplyr::starwars, source_note = "Source: Star Wars API (SWAPI)." )add_source_note( dplyr::starwars, source_note = "Source: Star Wars API (SWAPI)." )
Add a subtitle attribute to a table
add_table_subtitle(data, subtitle)add_table_subtitle(data, subtitle)
data |
A data frame, tibble, or |
subtitle |
The subtitle text to be added. |
The input data frame with an added subtitle attribute.
add_table_subtitle( dplyr::starwars, subtitle = "Star Wars Character Data" )add_table_subtitle( dplyr::starwars, subtitle = "Star Wars Character Data" )
Add a title attribute to a table
add_table_title(data, title)add_table_title(data, title)
data |
A data frame, tibble, or |
title |
The title text to be added. |
The input data frame with an added title attribute.
add_table_title( dplyr::starwars, title = "Star Wars Character Data" )add_table_title( dplyr::starwars, title = "Star Wars Character Data" )
Collapse a list of data frames or tibbles into a single data frame
collapse_list( data, ..., col_id = "category", label = NULL, pluck = NULL, as_proportion = FALSE, name_separator = "_", label_separator = "__" )collapse_list( data, ..., col_id = "category", label = NULL, pluck = NULL, as_proportion = FALSE, name_separator = "_", label_separator = "__" )
data |
A list of data frames or tibbles to be collapsed. |
... |
Additional arguments passed to |
col_id |
The name of the column to be created for the category. |
label |
A label for the category column. If |
pluck |
A character vector of column names to pluck from the data frames. If |
as_proportion |
If |
name_separator |
A string to separate the names of the columns in the output data frame. Default is "_". |
label_separator |
A string to separate the labels of the columns in the output data frame. Default is "__". |
A data frame with the specified category column and the frequency and percent columns for each category, along with any additional columns specified in pluck.
person_record |> generate_frequency( seeing, hearing, walking, remembering, self_caring, communicating ) |> collapse_list()person_record |> generate_frequency( seeing, hearing, walking, remembering, self_caring, communicating ) |> collapse_list()
Convert labelled factors to regular factors
convert_factor(data)convert_factor(data)
data |
A data frame, tibble, or |
A data frame with labelled factors converted to regular factors.
df <- data.frame( category = haven::labelled( c(1, 2, 3), c("One" = 1, "Two" = 2, "Three" = 3) ) ) df_converted <- convert_factor(df)df <- data.frame( category = haven::labelled( c(1, 2, 3), c("One" = 1, "Two" = 2, "Three" = 3) ) ) df_converted <- convert_factor(df)
Generate cross-tabulation
generate_crosstab( data, x, ..., add_total = TRUE, add_total_row = TRUE, add_total_column = TRUE, add_percent = TRUE, as_proportion = FALSE, percent_by_column = FALSE, name_separator = "_", label_separator = "__", label_total = "Total", label_total_column = NULL, label_total_row = NULL, label_na = "Not reported", label_as_group_name = TRUE, label_group_hierarchy = "All", include_na = TRUE, recode_na = "auto", group_separator = " - ", group_as_list = FALSE, group_as_hierarchy = FALSE, calculate_per_group = TRUE, expand_categories = TRUE, position_total = "bottom", sort_column_names = TRUE, collapse_list = FALSE, convert_factor = FALSE, multiple_columns = FALSE, multiple_columns_type = c("filtered", "stacked"), multiple_columns_filter = 1L, metadata = NULL )generate_crosstab( data, x, ..., add_total = TRUE, add_total_row = TRUE, add_total_column = TRUE, add_percent = TRUE, as_proportion = FALSE, percent_by_column = FALSE, name_separator = "_", label_separator = "__", label_total = "Total", label_total_column = NULL, label_total_row = NULL, label_na = "Not reported", label_as_group_name = TRUE, label_group_hierarchy = "All", include_na = TRUE, recode_na = "auto", group_separator = " - ", group_as_list = FALSE, group_as_hierarchy = FALSE, calculate_per_group = TRUE, expand_categories = TRUE, position_total = "bottom", sort_column_names = TRUE, collapse_list = FALSE, convert_factor = FALSE, multiple_columns = FALSE, multiple_columns_type = c("filtered", "stacked"), multiple_columns_filter = 1L, metadata = NULL )
data |
A data frame (typically |
x |
The variable to use for the rows of the cross-tabulation. |
... |
Additional variable(s) to use for the columns of the cross-tabulation. If none are provided, a frequency table for |
add_total |
Logical. If |
add_total_row |
Logical. If |
add_total_column |
Logical. If |
add_percent |
Logical. If |
as_proportion |
Logical. If |
percent_by_column |
Logical. If |
name_separator |
Character. Separator used when constructing variable names in the output. |
label_separator |
Character. Separator used when constructing labels in the output. |
label_total |
Character. Label used for the total row/category. |
label_total_column |
Character. Label used for the total column/category. |
label_total_row |
Character. Label used for the total row/category. |
label_na |
Character. Label to use for missing ( |
label_as_group_name |
Logical. If |
label_group_hierarchy |
Character. Label applied to grand-total entries when |
include_na |
Logical. If |
recode_na |
Character or |
group_separator |
Character. Separator used when concatenating group values in list output (if |
group_as_list |
Logical. If |
group_as_hierarchy |
Logical. When |
calculate_per_group |
Logical. If |
expand_categories |
Logical. If |
position_total |
Character. Position of the total row/column; either |
sort_column_names |
Logical. If |
collapse_list |
Logical (NOT YET IMPLEMENTED). If |
convert_factor |
Logical. If |
multiple_columns |
|
multiple_columns_type |
Character. Controls how |
multiple_columns_filter |
Scalar value (default |
metadata |
A named list with optional metadata to attach as attributes, e.g. |
A data frame or a list of data frames containing the cross-tabulation results. If group_as_list is TRUE, the output will be a list of data frames, one for each combination of grouping variable(s). Otherwise, a single data frame is returned. Each data frame includes counts and, if specified, percentages or proportions for each combination of x and the additional variables provided in ....
generate_frequency(), generate_output(), rename_label(), remove_label()
# Using built-in dataset `person_record` # Basic usage person_record |> generate_crosstab(marital_status, sex) # Multiple variables person_record |> generate_crosstab( sex, seeing, hearing, walking, remembering, self_caring, communicating ) # Grouping person_record |> dplyr::group_by(sex) |> generate_crosstab(marital_status, employed, group_as_list = TRUE) # Nested list with totals at each level (group_as_list + group_as_hierarchy) person_record |> dplyr::group_by(sex) |> generate_crosstab(marital_status, employed, group_as_list = TRUE, group_as_hierarchy = TRUE) # # Percent or proportion by row or column person_record |> generate_crosstab( marital_status, sex, percent_by_column = TRUE )# Using built-in dataset `person_record` # Basic usage person_record |> generate_crosstab(marital_status, sex) # Multiple variables person_record |> generate_crosstab( sex, seeing, hearing, walking, remembering, self_caring, communicating ) # Grouping person_record |> dplyr::group_by(sex) |> generate_crosstab(marital_status, employed, group_as_list = TRUE) # Nested list with totals at each level (group_as_list + group_as_hierarchy) person_record |> dplyr::group_by(sex) |> generate_crosstab(marital_status, employed, group_as_list = TRUE, group_as_hierarchy = TRUE) # # Percent or proportion by row or column person_record |> generate_crosstab( marital_status, sex, percent_by_column = TRUE )
Creates frequency tables for one or more categorical variables, optionally grouped by other variables. The function supports various enhancements such as sorting, totals, percentages, cumulative statistics, handling of missing values, and label customization. It returns a single table or a list of frequency tables.
generate_frequency( data, ..., sort_value = TRUE, sort_desc = TRUE, sort_except = NULL, add_total = TRUE, add_percent = TRUE, add_cumulative = FALSE, add_cumulative_percent = FALSE, as_proportion = FALSE, include_na = TRUE, recode_na = "auto", position_total = c("bottom", "top"), calculate_per_group = TRUE, group_separator = " - ", group_as_list = FALSE, group_as_hierarchy = FALSE, label_group_hierarchy = "All", label_as_group_name = TRUE, label_stub = NULL, label_na = "Not reported", label_total = "Total", expand_categories = TRUE, convert_factor = FALSE, collapse_list = FALSE, top_n = NULL, top_n_only = FALSE, metadata = NULL )generate_frequency( data, ..., sort_value = TRUE, sort_desc = TRUE, sort_except = NULL, add_total = TRUE, add_percent = TRUE, add_cumulative = FALSE, add_cumulative_percent = FALSE, as_proportion = FALSE, include_na = TRUE, recode_na = "auto", position_total = c("bottom", "top"), calculate_per_group = TRUE, group_separator = " - ", group_as_list = FALSE, group_as_hierarchy = FALSE, label_group_hierarchy = "All", label_as_group_name = TRUE, label_stub = NULL, label_na = "Not reported", label_total = "Total", expand_categories = TRUE, convert_factor = FALSE, collapse_list = FALSE, top_n = NULL, top_n_only = FALSE, metadata = NULL )
data |
A data frame (typically |
... |
One or more unquoted variable names (passed via tidy evaluation) for which to compute frequency tables. |
sort_value |
Logical. If |
sort_desc |
Logical. If |
sort_except |
Optional character vector. Variables to exclude from sorting. |
add_total |
Logical. If |
add_percent |
Logical. If |
add_cumulative |
Logical. If |
add_cumulative_percent |
Logical. If |
as_proportion |
Logical. If |
include_na |
Logical. If |
recode_na |
Character or |
position_total |
Character. Where to place the total row: |
calculate_per_group |
Logical. If |
group_separator |
Character. Separator used when concatenating group values in list output (if |
group_as_list |
Logical. If |
group_as_hierarchy |
Logical. When |
label_group_hierarchy |
Character. Label applied to grand-total entries when |
label_as_group_name |
Logical. If |
label_stub |
Optional character vector used for labeling output tables (e.g., for export or display). |
label_na |
Character. Label to use for missing ( |
label_total |
Character. Label used for the total row/category. |
expand_categories |
Logical. If |
convert_factor |
Logical. If |
collapse_list |
Logical. If |
top_n |
Integer or |
top_n_only |
Logical. If |
metadata |
A named list with optional metadata to attach as attributes, e.g. |
A frequency table (tibble, possibly nested) or a list of such tables. Additional attributes such as labels, metadata, and grouping information may be attached. The returned object is of class "tsg".
generate_crosstab(), generate_output(), rename_label(), remove_label()
# Using built-in dataset `person_record` # Basic usage person_record |> generate_frequency(sex) # Multiple variables person_record |> generate_frequency(sex, age, marital_status) # Grouping person_record |> dplyr::group_by(sex) |> generate_frequency(marital_status) # Output group as list person_record |> dplyr::group_by(sex) |> generate_frequency(marital_status, group_as_list = TRUE) # Nested list with totals at each level (group_as_list + group_as_hierarchy) person_record |> dplyr::group_by(sex) |> generate_frequency(marital_status, group_as_list = TRUE, group_as_hierarchy = TRUE) # Sorting # default is TRUE person_record |> generate_frequency(age, sort_value = TRUE) # If FALSE, the output will be sorted by the variable values in ascending order. person_record |> generate_frequency(age, sort_value = FALSE) # See vignettes for more examples.# Using built-in dataset `person_record` # Basic usage person_record |> generate_frequency(sex) # Multiple variables person_record |> generate_frequency(sex, age, marital_status) # Grouping person_record |> dplyr::group_by(sex) |> generate_frequency(marital_status) # Output group as list person_record |> dplyr::group_by(sex) |> generate_frequency(marital_status, group_as_list = TRUE) # Nested list with totals at each level (group_as_list + group_as_hierarchy) person_record |> dplyr::group_by(sex) |> generate_frequency(marital_status, group_as_list = TRUE, group_as_hierarchy = TRUE) # Sorting # default is TRUE person_record |> generate_frequency(age, sort_value = TRUE) # If FALSE, the output will be sorted by the variable values in ascending order. person_record |> generate_frequency(age, sort_value = FALSE) # See vignettes for more examples.
Generate output in specified format (e.g., xlsx, html, pdf, word)
generate_output(data, path, ..., format = c("xlsx", "html", "pdf", "word"))generate_output(data, path, ..., format = c("xlsx", "html", "pdf", "word"))
data |
Preferably a |
path |
File path to save the output. For HTML/PDF with a list and |
... |
Additional arguments passed to specific format functions:
|
format |
Output format. One of |
Invisibly returns NULL. Called for its side-effect of writing output file(s).
# Generate an xlsx file from a tsg object data <- generate_frequency(dplyr::starwars, sex) dir_to <- tempdir() generate_output( data, file.path(dir_to, "starwars_frequency.xlsx"), format = "xlsx" ) unlink(file.path(dir_to, "starwars_frequency.xlsx"))# Generate an xlsx file from a tsg object data <- generate_frequency(dplyr::starwars, sex) dir_to <- tempdir() generate_output( data, file.path(dir_to, "starwars_frequency.xlsx"), format = "xlsx" ) unlink(file.path(dir_to, "starwars_frequency.xlsx"))
Generate a template
generate_template(path, template = c("facade", "table-list"))generate_template(path, template = c("facade", "table-list"))
path |
A character string specifying the path where the template should be saved. If a directory is provided, the template will be saved with a default name based on the template type. |
template |
A character string specifying the type of template to generate. Options are "facade" for a YAML facade template or "table-list" for an Excel table list template. |
Void. A file path where the template has been saved.
template_path_facade <- tempfile(fileext = ".yaml") generate_template(template_path_facade, template = "facade") template_path_table_list <- tempfile(fileext = ".xlsx") generate_template(template_path_table_list, template = "table-list") unlink(template_path_facade) unlink(template_path_table_list)template_path_facade <- tempfile(fileext = ".yaml") generate_template(template_path_facade, template = "facade") template_path_table_list <- tempfile(fileext = ".xlsx") generate_template(template_path_table_list, template = "table-list") unlink(template_path_facade) unlink(template_path_table_list)
Get a facade from the package or a file
get_tsg_facade(facade = "default", which = c("xlsx", "html", "docx", "pdf"))get_tsg_facade(facade = "default", which = c("xlsx", "html", "docx", "pdf"))
facade |
A character string specifying the name of the facade to retrieve. Defaults to "default". The facade is a YAML file that defines the styling and layout of the table |
which |
A character string specifying the format of the facade to retrieve. Options are "xlsx", "pdf", or "html". Defaults to "xlsx". |
A list containing the facade settings for the specified format. The facade includes styling attributes such as font size, color, border styles, and background fills for different parts of the table.
# Default facade get_tsg_facade() # Other built-in facade get_tsg_facade("yolo")# Default facade get_tsg_facade() # Other built-in facade get_tsg_facade("yolo")
This is a synthetic dataset containing person information for demonstration purposes.
person_recordperson_record
A labelled data frame with 2918 rows and 11 variables:
Numeric identifier for each person
Factor indicating the sex of the person
Numeric age of the person
Factor indicating marital status
Employment status
Functional difficulty in seeing
Functional difficulty in hearing
Functional difficulty in walking
Functional difficulty in remembering
Functional difficulty in self-caring
Functional difficulty in communicating
person_recordperson_record
Remove data labels
remove_label(data, ...)remove_label(data, ...)
data |
A data frame or tibble from which to remove labels. |
... |
A character vector of column names from which to remove labels. If no columns are specified, all labels will be removed. |
A data frame or tibble with the specified labels removed. If no columns are specified, all labels will be removed.
person_record |> generate_frequency( seeing, hearing, walking, remembering, self_caring, communicating ) |> collapse_list() |> remove_label()person_record |> generate_frequency( seeing, hearing, walking, remembering, self_caring, communicating ) |> collapse_list() |> remove_label()
Remove all labels
remove_labels(data, ...)remove_labels(data, ...)
data |
A data frame or tibble from which to remove all labels. |
... |
A character vector of column names from which to remove labels. If no columns are specified, all labels will be removed. |
A data frame or tibble with all labels removed. If no columns are specified, all labels will be removed.
person_record |> generate_frequency( seeing, hearing, walking, remembering, self_caring, communicating ) |> collapse_list() |> remove_labels()person_record |> generate_frequency( seeing, hearing, walking, remembering, self_caring, communicating ) |> collapse_list() |> remove_labels()
Rename data labels
rename_label(data, ...)rename_label(data, ...)
data |
A data frame or tibble to rename labels in. |
... |
A named list of labels to rename. The names should match the column names in the data, and the values should be the new labels. |
A data frame or tibble with the specified labels renamed.
person_record |> generate_frequency( seeing, hearing, walking, remembering, self_caring, communicating ) |> collapse_list() |> rename_label(category = "Functional difficulty")person_record |> generate_frequency( seeing, hearing, walking, remembering, self_caring, communicating ) |> collapse_list() |> rename_label(category = "Functional difficulty")
Saves one or more tsg tables as a Word document using the officer and
flextable packages (both must be installed).
write_docx( data, path, ..., title = NULL, subtitle = NULL, source_note = NULL, footnotes = NULL, separate_files = FALSE, names_separator = "__", facade = get_tsg_facade(which = "docx") )write_docx( data, path, ..., title = NULL, subtitle = NULL, source_note = NULL, footnotes = NULL, separate_files = FALSE, names_separator = "__", facade = get_tsg_facade(which = "docx") )
data |
A |
path |
File path for the Word output. A |
... |
Currently unused; reserved for future arguments. |
title |
Optional title string (overrides data attribute). |
subtitle |
Optional subtitle string. |
source_note |
Optional source note string. |
footnotes |
Optional character vector of footnotes. |
separate_files |
Logical. When |
names_separator |
Column name separator for detecting cross-tab spanners. Default
|
facade |
Styling options. Defaults to the global tsg facade. |
When data is a named list and separate_files = FALSE (default), all
tables are written into a single .docx document, one per page. Set
separate_files = TRUE to write one .docx file per table inside a
subdirectory derived from path.
Invisibly returns NULL.
Saves one or more tsg tables as an HTML file using the gt package
(which is already a hard dependency of tsg).
write_html( data, path, ..., title = NULL, subtitle = NULL, source_note = NULL, footnotes = NULL, separate_files = FALSE, include_table_list = FALSE, names_separator = "__", facade = get_tsg_facade(which = "html") )write_html( data, path, ..., title = NULL, subtitle = NULL, source_note = NULL, footnotes = NULL, separate_files = FALSE, include_table_list = FALSE, names_separator = "__", facade = get_tsg_facade(which = "html") )
data |
A |
path |
File path for the HTML output. A |
... |
Additional arguments passed to |
title |
Optional title string (overrides data attribute). |
subtitle |
Optional subtitle string. |
source_note |
Optional source note string. |
footnotes |
Optional character vector of footnotes. |
separate_files |
Logical. When |
include_table_list |
Logical. When |
names_separator |
Column name separator for spanners. Default |
facade |
Styling options. Defaults to the global tsg facade. |
When data is a named list and separate_files = FALSE (default), all
tables are written into a single self-contained HTML document. Set
include_table_list = TRUE to prepend a clickable table-of-contents. Set
separate_files = TRUE to write one HTML file per table into a subdirectory.
Invisibly returns NULL.
Saves tables as PDF using gt::gtsave(), which requires the webshot2
package (and a Chromium installation reachable by chromote).
write_pdf( data, path, ..., title = NULL, subtitle = NULL, source_note = NULL, footnotes = NULL, separate_files = TRUE, names_separator = "__", facade = get_tsg_facade(which = "html") )write_pdf( data, path, ..., title = NULL, subtitle = NULL, source_note = NULL, footnotes = NULL, separate_files = TRUE, names_separator = "__", facade = get_tsg_facade(which = "html") )
data |
A |
path |
File path for the PDF output. A |
... |
Additional arguments passed to |
title |
Optional title string (overrides data attribute). |
subtitle |
Optional subtitle string. |
source_note |
Optional source note string. |
footnotes |
Optional character vector of footnotes. |
separate_files |
Logical. When |
names_separator |
Column name separator for spanners. Default |
facade |
Styling options. Defaults to the global tsg facade. |
When data is a named list, the default (separate_files = TRUE) writes
each table to its own .pdf file inside a directory. Set
separate_files = FALSE to merge all tables into a single PDF (requires the
qpdf package).
Invisibly returns NULL.
Exports a data frame or a list of data frames to one or multiple Excel files,
with support for titles, subtitles, source notes, footnotes, grouping, and custom styles.
It leverages the openxlsx package to create styled Excel reports suitable for presentation.
write_xlsx( data, path, ..., sheet_name = NULL, title = NULL, subtitle = NULL, source_note = NULL, footnotes = NULL, separate_files = FALSE, collapse_list = FALSE, row_group_as_column = FALSE, names_separator = "__", include_table_list = FALSE, table_list_reference = NULL, facade = get_tsg_facade() )write_xlsx( data, path, ..., sheet_name = NULL, title = NULL, subtitle = NULL, source_note = NULL, footnotes = NULL, separate_files = FALSE, collapse_list = FALSE, row_group_as_column = FALSE, names_separator = "__", include_table_list = FALSE, table_list_reference = NULL, facade = get_tsg_facade() )
data |
A
|
path |
A file path (if |
... |
Additional arguments passed to |
sheet_name |
Optional name for the Excel sheet. Ignored if |
title |
Optional title displayed above the data in each sheet or file. |
subtitle |
Optional subtitle displayed under the title. |
source_note |
Optional source note displayed below the data. |
footnotes |
Optional character vector of footnotes to display below the source note. |
separate_files |
Logical. If |
collapse_list |
Logical. If |
row_group_as_column |
Logical. If |
names_separator |
Character used to separate column names when dealing with nested or grouped headers. |
include_table_list |
Logical. If |
table_list_reference |
A data frame containing the table list reference. If |
facade |
A list of styling options (colors, fonts, sizes, border styles, etc.). Defaults to the global option |
This function supports advanced Excel formatting including:
Grouped headers
Dynamic column widths
Styled titles, subtitles, source notes, and footnotes
Border styling (inner, outer, header)
The function is designed to handle export needs in professional and reporting contexts.
Invisibly returns NULL. The function is called for its side-effect of writing Excel file(s).
data <- tsg::generate_frequency(dplyr::starwars, sex) dir_to <- tempfile() write_xlsx( data, file.path(dir_to, "starwars_frequency.xlsx") )data <- tsg::generate_frequency(dplyr::starwars, sex) dir_to <- tempfile() write_xlsx( data, file.path(dir_to, "starwars_frequency.xlsx") )