User-facing helper that prints a visually aligned, spicy-styled ASCII table
created by functions such as freq() or cross_tab().
It automatically adjusts column alignment, spacing, and separators for
improved readability in console outputs.
This function wraps the internal renderer build_ascii_table(), adding
optional titles, notes, and automatic alignment rules depending on the type
of table.
Usage
spicy_print_table(
x,
title = attr(x, "title"),
note = attr(x, "note"),
padding = 2L,
first_column_line = TRUE,
row_total_line = TRUE,
column_total_line = TRUE,
bottom_line = FALSE,
lines_color = "darkgrey",
align_left_cols = NULL,
align_center_cols = integer(0),
group_sep_rows = integer(0),
total_row_idx = attr(x, "total_row_idx"),
...
)Arguments
- x
A
spicy_tableordata.frameto be printed.- title
Optional title displayed above the table. Defaults to the
"title"attribute ofxif present.- note
Optional note displayed below the table. Defaults to the
"note"attribute ofxif present.- padding
Non-negative integer giving the number of extra characters added to each column's auto-computed width (max of cell-content width and header width). Defaults to
2L. Seebuild_ascii_table()for the precise formula and the migration note from the pre-0.11.0 string enum.- first_column_line
Logical. If
TRUE(the default), adds a vertical separator after the first column.- row_total_line, column_total_line, bottom_line
Logical flags controlling the presence of horizontal lines before total rows/columns or at the bottom of the table. Both
row_total_lineandcolumn_total_linedefault toTRUE;bottom_linedefaults toFALSE.- lines_color
Character. Color for table separators. Defaults to
"darkgrey". Only applied if the output supports ANSI colors (seecrayon::has_color()).- align_left_cols
Integer vector of column indices to left-align. If
NULL(the default), alignment is auto-detected based onx:For
freqtables ->c(1, 2)For
crosstables ->1
- align_center_cols
Integer vector of column indices to center-align. Defaults to
integer(0).- group_sep_rows
Integer vector of row indices before which a light dashed separator line is drawn. Defaults to
integer(0).- total_row_idx
Optional integer vector of 1-based row indices identifying the totals rows; defaults to the
"total_row_idx"attribute ofx(set bycross_tab()). Seebuild_ascii_table().- ...
Additional arguments passed to
build_ascii_table().
Details
spicy_print_table() detects whether the table represents frequencies
(freq-style) or cross-tabulations (cross-style) and adjusts formatting
accordingly:
For frequency tables, the first two columns (Category and Values) are left-aligned.
For cross tables, only the first column (row variable) is left-aligned.
The function supports Unicode line-drawing characters and colored separators using the crayon package, with graceful fallback to monochrome output when color is not supported. If the table exceeds the console width, it is split into stacked horizontal panels while repeating the left-most identifier columns.
See also
build_ascii_table() for the underlying text rendering engine.
print.spicy_freq_table() for the specialized printing method used by freq().
Examples
# Simple demonstration
df <- data.frame(
Category = c("Valid", "", "Missing", "Total"),
Values = c("Yes", "No", "NA", ""),
Freq. = c(12, 8, 1, 21),
Percent = c(57.1, 38.1, 4.8, 100.0)
)
spicy_print_table(df,
title = "Frequency table: Example",
note = "Class: data.frame\nData: demo"
)
#> Frequency table: Example
#>
#> Category │ Values Freq. Percent
#> ────────────┼──────────────────────────────
#> Valid │ Yes 12 57.1
#> │ No 8 38.1
#> Missing │ NA 1 4.8
#> ────────────┼──────────────────────────────
#> Total │ 21 100
#>
#> Class: data.frame
#> Data: demo