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.
The tsg package provides functions for generating publication-ready
frequency tables and cross-tabulations from data frames. Tables can be
exported to Excel (XLSX), HTML, PDF, and Word (DOCX)
formats with extensive styling options via a unified facade system.
install.packages("tsg")
tsg requires the following packages: gt (HTML/PDF), officer and
flextable (Word). These are installed automatically. For PDF output,
webshot2 is also needed:
install.packages("webshot2") # optional, for PDF output
library(tsg)
# Frequency table
generate_frequency(person_record, sex)
# Cross-tabulation
generate_crosstab(person_record, marital_status, sex)
tbl <- person_record |>
generate_crosstab(marital_status, sex)
# Excel
write_xlsx(tbl, "table.xlsx")
# HTML
write_html(tbl, "table.html")
# PDF
write_pdf(tbl, "table.pdf") # requires webshot2
# Word
write_docx(tbl, "table.docx")
The facade system controls table appearance uniformly across all output formats:
person_record |>
generate_frequency(sex) |>
add_facade(
header.bgFill = "#003366",
header.fontColour = "#FFFFFF",
header.textDecoration = "bold",
body.fontSize = 11,
table.lastRowBold = TRUE
) |>
write_xlsx("styled.xlsx")
More examples in the vignettes.