View source: R/table_summary.R
| table_summary | R Documentation |
This function takes a two-column data frame and formats it into a summary-like table.
The table can be optionally split into two parts, centered, and given a title.
It is useful for displaying summary information in a clean, tabular format.
The function also supports styling with ANSI colors and text formatting through
the {cli} package and column alignment options.
table_summary(
data,
title = NULL,
l = NULL,
header = FALSE,
center_table = FALSE,
border_char = "-",
style = list(),
align = NULL,
...
)
data |
A data frame with exactly two columns. The data to be summarized and displayed. |
title |
A character string. An optional title to be displayed above the table. |
l |
An integer. The number of rows to include in the left part of a split table.
If |
header |
A logical value. If |
center_table |
A logical value. If |
border_char |
Character used for borders. Default is |
style |
A list controlling the visual styling of table elements using ANSI formatting. Can include the following components:
Each style component can be either a predefined style string (e.g., "blue", "red_italic", "bold")
or a function that takes a context list with/without a |
align |
Controls the alignment of column values. Can be specified in three ways:
|
... |
Additional arguments (currently unused). |
This function does not return a value. It prints the formatted table to the console.
# Create a sample data frame
df = data.frame(
Category = c("A", "B", "C", "D", "E"),
Value = c(10, 20, 30, 40, 50)
)
# Display the table with a title and header
table_summary(df, title = "Sample Table", header = TRUE)
# Split the table after the second row and center it
table_summary(df, l = 2, center_table = TRUE)
# Use styling and alignment
table_summary(
df, header = TRUE,
style = list(
left_col = "blue_bold",
right_col = "red",
title = "green",
border_text = "yellow"
),
align = c("center", "right")
)
# Use custom styling with lambda functions
table_summary(
df, header = TRUE,
style = list(
left_col = \(ctx) cli::col_red(ctx), # ctx$value is another option
right_col = \(ctx) cli::col_blue(ctx)
),
align = list(left_col = "left", right_col = "right")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.