This 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,
  ...
)

Arguments

x

The data frame to be formatted into a table or a character string representing a table in row-wise format (see example).

title

Title string. If provided, it will be displayed at the top of the table.

footnote

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.

spanner

List with information on grouping columns. E.g. spanner = list("M" = 2:3, "SD" = 4:6).

row_group

List with information on grouping rows row_group = list("Start" = 1:2, "That is the second" = 3:5).

row_group_order

List with information on grouping order. E.g. row_group_order = c("That is the second", "Start").

rownames

Logical or NULL. If TRUE, rownames are shown. If NULL, rownames are shown when they are not identical to as.character(1:nrow(x)).

file

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.

use_col_labels

Logical. If TRUE, variable labels are used for column names. If FALSE, column names are used.

cols_label

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

cols_align

List with align align information. E.g., list(left = c(2,3), right = 1).

decimals

Number of decimals that will be printed.

round

Number of digits to which numbers should be rounded.

label_na

Label for replacing missing values.

markdown

If TRUE, interprets cell content as markdown.

gt

Additional arguments passed to gt::gt().

sort

Character vector with column names according to which the table should be sorted.

sort_decreasing

Logical. If TRUE, sorting is done in decreasing order.

...

Various arguments for backward compatibility.

Value

A gt table object.

Details

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.

Author

Juergen Wilbert

Examples

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
One
Two
First Second Third Fourth
Start
1.0 9.8 e n
2.0 7.0 d a
That is the second
3.0 −1.6 c s
4.0 6.4 b o
5.0 2.1 a r
Note. Footnote 1. Footnote 2.
## Rowwise table creation " id | name | age 1 | Alice | 30 1 | John | 40 " |> nice_table()
id name age
1 Alice 30
1 John 40