nice_table.default.RdThis function takes a data frame and formats it into a nicely formatted HTML
table using the gt packages.
# Default S3 method
nice_table(
x,
title = NULL,
footnote = NULL,
spanner = NULL,
row_group = NULL,
row_group_order = NULL,
rownames = NULL,
file = NULL,
use_col_labels = FALSE,
cols_label = NULL,
cols_align = NULL,
decimals = NULL,
round = NULL,
label_na = NULL,
markdown = FALSE,
gt = NULL,
sort = NULL,
sort_decreasing = FALSE,
...
)The data frame to be formatted into a table or a character string representing a table in row-wise format (see example).
Title string. If provided, it will be displayed at the top of the table.
Add footnotes to the table. Can be a character vector. Each element will be a separate footnote. If multiple footnotes are provided, they will be concatenated.
List with information on grouping columns. E.g. spanner = list("M" = 2:3, "SD" = 4:6).
List with information on grouping rows row_group = list("Start" = 1:2, "That is the second" = 3:5).
List with information on grouping order. E.g.
row_group_order = c("That is the second", "Start").
Logical or NULL. If TRUE, rownames are shown. If NULL,
rownames are shown when they are not identical to
as.character(1:nrow(x)).
Character string with filename. If set, an additional file is
exported (html or docx format is possible). If set TRUE, a filename is
automatically created based on the title. If NULL (default), no file is
exported.
Logical. If TRUE, variable labels are used for column names. If FALSE, column names are used.
List with renaming information for columns (old_name =
new_name). E.g., cols_label = list(old_name1 = "New Name 1", old_name2 = "New Name 2").
List with align align information.
E.g., list(left = c(2,3), right = 1).
Number of decimals that will be printed.
Number of digits to which numbers should be rounded.
Label for replacing missing values.
If TRUE, interprets cell content as markdown.
Additional arguments passed to gt::gt().
Character vector with column names according to which the table should be sorted.
Logical. If TRUE, sorting is done in decreasing order.
Various arguments for backward compatibility.
A gt table object.
The function allows for various customizations such as adding titles, footnotes, grouping columns and rows, aligning columns, rounding numeric values, and more. It also supports exporting the table to HTML or DOCX files.
df <- data.frame(
x = 1:5, y = rnorm(5, mean = 10, sd = 12),
c = letters[5:1], d = sample(letters, 5)
)
nice_table(
df,
title = "A nice title",
footnote = c("Footnote 1", "Footnote 2"),
spanner = list("One" = 1:2, "Two" = 3:4),
row_group = list("Start" = 1:2, "That is the second" = 3:5),
cols_label = list(x = "First", y = "Second", c = "Third", d = "Fourth"),
decimals = 1
)
Table
A nice title
First
Second
Third
Fourth
Start
That is the second
Note. Footnote 1. Footnote 2.
## Rowwise table creation
"
id | name | age
1 | Alice | 30
1 | John | 40
" |>
nice_table()
id
name
age