Learn R Programming

tsg

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.

Installation

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

Basic usage

library(tsg)

# Frequency table
generate_frequency(person_record, sex)

# Cross-tabulation
generate_crosstab(person_record, marital_status, sex)

Output formats

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")

Styling with facades

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.

Copy Link

Version

Install

install.packages('tsg')

Monthly Downloads

2,940

Version

0.1.4

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Bhas Abdulsamad

Last Published

May 5th, 2026

Functions in tsg (0.1.4)

remove_label

Remove data labels
tsg_to_flextable

Convert a tsg table to a flextable object
write_pdf

Write a tsg table (or list of tables) to a PDF file
write_xlsx

Write Data to Excel with Titles, Notes, and Styling
write_docx

Write a tsg table (or list of tables) to a Word (.docx) file
write_html

Write a tsg table (or list of tables) to an HTML file
tsg_to_gt

Convert a tsg table to a gt object
rename_label

Rename data labels
person_record

Sample dataset of persons
tsg-package

tsg: Generate Publication-Ready Statistical Tables
remove_labels

Remove all labels
add_footnote

Add a footnote attribute to a table
convert_factor

Convert labelled factors to regular factors
add_column_total

Add a column total
add_facade_alt

Add a facade to a tsg table (alternative way)
add_row_total

Add a row total
add_source_note

Add a source note attribute to a table
add_facade

Add a facade to a tsg table
collapse_list

Collapse a list of data frames or tibbles into a single data frame
add_table_title

Add a title attribute to a table
add_table_subtitle

Add a subtitle attribute to a table
generate_template

Generate a template
generate_output

Generate output in specified format (e.g., xlsx, html, pdf, word)
gt_apply_spanners

Apply spanner labels to a gt table based on separator-split column names
parse_excel_numfmt_decimals

Parse decimal places from an Excel number format string
generate_crosstab

Generate cross-tabulation
get_tsg_facade

Get a facade from the package or a file
generate_frequency

Generate frequency table
gt_apply_text_decoration

Apply text decoration (bold/italic/underline) to gt locations
gt_apply_numeric_format

Apply numeric cell formatting to a gt table based on facade settings