Description Usage Arguments Details Value Note References See Also Examples
A very simple table generator, and it is simple by design. It is not intended
to replace any other R packages for making tables. The kable()
function returns a single table for a single data object, and returns a table
that contains multiple tables if the input object is a list of data objects.
The kables() function is similar to kable(x) when x is a
list of data objects, but kables() accepts a list of kable()
values directly instead of data objects (see examples below).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
x |
For |
format |
A character string. Possible values are |
digits |
Maximum number of digits for numeric columns, passed to
|
row.names |
Logical: whether to include row names. By default, row names
are included if |
col.names |
A character vector of column names to be used in the table. |
align |
Column alignment: a character vector consisting of |
caption |
The table caption. |
label |
The table reference label. By default, the label is obtained
from |
format.args |
A list of arguments to be passed to |
escape |
Boolean; whether to escape special characters when producing
HTML or LaTeX tables. When |
... |
Other arguments (see Examples). |
Missing values (NA) in the table are displayed as NA by
default. If you want to display them with other characters, you can set the
option knitr.kable.NA, e.g. options(knitr.kable.NA = '') to
hide NA values.
A character vector of the table source code.
When using kable() as a top-level expression, you do not
need to explicitly print() it due to R's automatic implicit
printing. When it is wrapped inside other expressions (such as a
for loop), you must explicitly print(kable(...)).
See https://github.com/yihui/knitr-examples/blob/master/091-knitr-table.Rnw for some examples in LaTeX, but they also apply to other document formats.
Other R packages such as huxtable, xtable, kableExtra, and tables for HTML and LaTeX tables, and ascii and pander for different flavors of markdown output and some advanced features and table styles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | d1 = head(iris)
d2 = head(mtcars)
# pipe tables by default
kable(d1)
kable(d2[, 1:5])
# no inner padding
kable(d2, format = "pipe", padding = 0)
# more padding
kable(d2, format = "pipe", padding = 2)
kable(d1, format = "latex")
kable(d1, format = "html")
kable(d1, format = "latex", caption = "Title of the table")
kable(d1, format = "html", caption = "Title of the table")
# use the booktabs package
kable(mtcars, format = "latex", booktabs = TRUE)
# use the longtable package
kable(matrix(1000, ncol = 5), format = "latex", digits = 2, longtable = TRUE)
# change LaTeX default table environment
kable(d1, format = "latex", caption = "My table", table.envir = "table*")
# add some table attributes
kable(d1, format = "html", table.attr = "id=\"mytable\"")
# reST output
kable(d2, format = "rst")
# no row names
kable(d2, format = "rst", row.names = FALSE)
# Pandoc simple tables
kable(d2, format = "simple", caption = "Title of the table")
# format numbers using , as decimal point, and ' as thousands separator
x = as.data.frame(matrix(rnorm(60, 1e+06, 10000), 10))
kable(x, format.args = list(decimal.mark = ",", big.mark = "'"))
# save the value
x = kable(d2, format = "html")
cat(x, sep = "\n")
# can also set options(knitr.table.format = 'html') so that the output is HTML
# multiple tables via either kable(list(x1, x2)) or kables(list(kable(x1),
# kable(x2)))
kable(list(d1, d2), caption = "A tale of two tables")
kables(list(kable(d1, align = "l"), kable(d2)), caption = "A tale of two tables")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.