export_table | R Documentation |
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.
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,
column_names = NULL,
align = NULL,
by = NULL,
zap_small = FALSE,
table_width = "auto",
remove_duplicates = FALSE,
row_groups = NULL,
column_groups = NULL,
verbose = TRUE,
...
)
x |
A data frame. May also be a list of data frames, to export multiple data frames into multiple tables. |
sep |
Column separator. |
header |
Header separator. Can be |
cross |
Character that is used where separator and header lines cross. |
empty_line |
Separator used for empty lines. If |
digits |
Number of digits for rounding or significant figures. May also
be |
protect_integers |
Should integers be kept as integers (i.e., without decimals)? |
missing |
Value by which |
width |
Refers to the width of columns (with numeric values). Can be
either |
format |
Name of output-format, as string. If |
title , caption , subtitle |
Table title (same as caption) and subtitle, as
strings. If |
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 |
column_names |
Character vector of names that will be used as column names in the table. Must either be of same length as columns in the table, or a named vector, where names (LHS) indicate old column names, and values (RHS) are used as new column names. |
align |
Column alignment. For markdown-formatted tables, the default
|
by |
Name of column(s) in |
zap_small |
Logical, if |
table_width |
Numeric,
|
remove_duplicates |
Logical, if |
row_groups |
Named list, can be used as alternative to |
column_groups |
Named list, can be used to group columns in the printed
output. List elements must indicate column indices for columns that should
belong to one group. The names of the list elements will be used as group
names, which will be inserted as "column header row". Currently only
works for |
verbose |
Toggle messages and warnings. |
... |
Arguments passed to |
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. format = "tt"
returns a
tinytable::tt()
object, which is a lightweight table format that can be
used in markdown, LaTeX, HTML and other formats, depending on the context
where the table is used.
The values for caption
, subtitle
and footer
can also be provided
as attributes of x
, e.g. if caption = NULL
and x
has attribute
table_caption
, the value for this attribute will be used as table caption.
table_subtitle
is the attribute for subtitle
, and table_footer
for
footer
.
Vignettes Formatting, printing and exporting tables and Formatting model parameters.
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)
# group (split) tables by variables
export_table(head(mtcars, 8), by = "cyl")
# 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)
# rename column names
export_table(x, column_names = letters[1:5])
export_table(x, column_names = c(Species = "a"))
# 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")
# group rows in the table
## Not run:
data(mtcars)
# fit model
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$gear <- as.factor(mtcars$gear)
model <- lm(mpg ~ hp + gear * vs + cyl + drat, data = mtcars)
# model summary, don't select "Intercept" parameter
mp <- as.data.frame(format(
parameters::model_parameters(model, drop = "^\\(Intercept")
))
# define groups for the table
groups <- list(
Engine = c("cyl [6]", "cyl [8]", "vs", "hp"),
Interactions = c(8, 9),
Controls = c(2, 3, 7)
)
# export table with groups, using tinytable format
export_table(mp, format = "tt", row_groups = groups)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.