Learn R Programming

insight (version 1.0.0)

apply_table_theme: Data frame and Tables Pretty Formatting

Description

Function to export data frames into tables, which can be printed to the console, or displayed in markdown or HTML format (and thereby, exported to other formats like Word or PDF). The table width is automatically adjusted to fit into the width of the display device (e.g., width of console). Use the table_width argument to control this behaviour.

Usage

apply_table_theme(out, x, theme = "default", sub_header_positions = NULL)

export_table( x, sep = " | ", header = "-", cross = NULL, empty_line = NULL, digits = 2, protect_integers = TRUE, missing = "", width = NULL, format = NULL, title = NULL, caption = title, subtitle = NULL, footer = NULL, align = NULL, by = NULL, zap_small = FALSE, table_width = "auto", remove_duplicates = FALSE, verbose = TRUE, ... )

Value

If format = "text" (or NULL), a formatted character string is returned. format = "markdown" (or "md") returns a character string of class knitr_kable, which renders nicely in markdown files. format = "html"

returns an gt object (created by the gt package), which - by default - is displayed in the IDE's viewer pane or default browser. This object can be further modified with the various gt-functions.

Arguments

out

A tinytable object.

x

A data frame. May also be a list of data frames, to export multiple data frames into multiple tables.

theme

The theme to apply to the table. One of "default", "grid", "striped", "bootstrap", "void", "tabular", or "darklines".

sub_header_positions

A vector of row positions to apply a border to. Currently particular for internal use of other easystats packages.

sep

Column separator.

header

Header separator. Can be NULL.

cross

Character that is used where separator and header lines cross.

empty_line

Separator used for empty lines. If NULL, line remains empty (i.e. filled with whitespaces).

digits

Number of digits for rounding or significant figures. May also be "signif" to return significant figures or "scientific" to return scientific notation. Control the number of digits by adding the value as suffix, e.g. digits = "scientific4" to have scientific notation with 4 decimal places, or digits = "signif5" for 5 significant figures (see also signif()).

protect_integers

Should integers be kept as integers (i.e., without decimals)?

missing

Value by which NA values are replaced. By default, an empty string (i.e. "") is returned for NA.

width

Refers to the width of columns (with numeric values). Can be either NULL, a number or a named numeric vector. If NULL, the width for each column is adjusted to the minimum required width. If a number, columns with numeric values will have the minimum width specified in width. If a named numeric vector, value names are matched against column names, and for each match, the specified width is used (see 'Examples'). Only applies to text-format (see format).

format

Name of output-format, as string. If NULL (or "text"), returned output is used for basic printing. Can be one of NULL (the default) resp. "text" for plain text, "markdown" (or "md") for markdown and "html" for HTML output.

title, caption, subtitle

Table title (same as caption) and subtitle, as strings. If NULL, no title or subtitle is printed, unless it is stored as attributes (table_title, or its alias table_caption, and table_subtitle). If x is a list of data frames, caption may be a list of table captions, one for each table.

footer

Table footer, as string. For markdown-formatted tables, table footers, due to the limitation in markdown rendering, are actually just a new text line under the table. If x is a list of data frames, footer may be a list of table captions, one for each table.

align

Column alignment. For markdown-formatted tables, the default align = NULL will right-align numeric columns, while all other columns will be left-aligned. If format = "html", the default is left-align first column and center all remaining. May be a string to indicate alignment rules for the complete table, like "left", "right", "center" or "firstleft" (to left-align first column, center remaining); or a string with abbreviated alignment characters, where the length of the string must equal the number of columns. For instance, align = "lccrl" would left-align the first column, center the second and third, right-align column four and left-align the fifth column.

by

Name of column in x that indicates grouping for tables. Only applies when format = "html". by is passed down to gt::gt(groupname_col = by).

zap_small

Logical, if TRUE, small values are rounded after digits decimal places. If FALSE, values with more decimal places than digits are printed in scientific notation.

table_width

Numeric,"auto", NULL or Inf, indicating the width of the complete table.

  • If table_width = "auto" (default) and the table is wider than the current width (i.e. line length) of the console (or any other source for textual output, like markdown files), the table is split into multiple parts.

  • Else, if table_width is numeric and table rows are larger than table_width, the table is split into multiple parts. For each new table, the first column is repeated for better orientation.

  • Use NULL or Inf to turn off automatic splitting of the table.

  • options(easystats_table_width = <value>) can be used to set a default width for tables.

remove_duplicates

Logical, if TRUE and table is split into multiple parts, duplicated ("empty") rows will be removed. If FALSE, empty rows will be preserved. Only applies when table_width is not NULL (or Inf) and table is split into multiple parts.

verbose

Toggle messages and warnings.

...

Currently not used.

See Also

Examples

Run this code
export_table(head(iris))
export_table(head(iris), cross = "+")
export_table(head(iris), sep = " ", header = "*", digits = 1)

# split longer tables
export_table(head(iris), table_width = 30)

# \donttest{
# colored footers
data(iris)
x <- as.data.frame(iris[1:5, ])
attr(x, "table_footer") <- c("This is a yellow footer line.", "yellow")
export_table(x)

attr(x, "table_footer") <- list(
  c("\nA yellow line", "yellow"),
  c("\nAnd a red line", "red"),
  c("\nAnd a blue line", "blue")
)
export_table(x)

attr(x, "table_footer") <- list(
  c("Without the ", "yellow"),
  c("new-line character ", "red"),
  c("we can have multiple colors per line.", "blue")
)
export_table(x)
# }

# column-width
d <- data.frame(
  x = c(1, 2, 3),
  y = c(100, 200, 300),
  z = c(10000, 20000, 30000)
)
export_table(d)
export_table(d, width = 8)
export_table(d, width = c(x = 5, z = 10))
export_table(d, width = c(x = 5, y = 5, z = 10), align = "lcr")

Run the code above in your browser using DataLab