Debian Codegraph @

man/kable.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/table.R
3 \name{kable}
4 \alias{kable}
5 \alias{kables}
6 \title{Create tables in LaTeX, HTML, Markdown and reStructuredText}
7 \usage{
8 kable(
9 x,
10 format,
11 digits = getOption("digits"),
12 row.names = NA,
13 col.names = NA,
14 align,
15 caption = opts_current$get("tab.cap"),
16 label = NULL,
17 format.args = list(),
18 escape = TRUE,
19 ...
20 )
21
22 kables(x, format, caption = NULL, label = NULL)
23 }
24 \arguments{
25 \item{x}{For \code{kable()}, \code{x} is an R object, which is typically a
26 matrix or data frame. For \code{kables()}, a list with each element being a
27 returned value from \code{kable()}.}
28
29 \item{format}{A character string. Possible values are \code{latex},
30 \code{html}, \code{pipe} (Pandoc's pipe tables), \code{simple} (Pandoc's
31 simple tables), \code{rst}, \code{jira}, and \code{org} (Emacs Org-mode).
32 The value of this argument will be automatically determined if the function
33 is called within a \pkg{knitr} document. The \code{format} value can also
34 be set in the global option \code{knitr.table.format}. If \code{format} is
35 a function, it must return a character string.}
36
37 \item{digits}{Maximum number of digits for numeric columns, passed to
38 \code{round()}. This can also be a vector of length \code{ncol(x)}, to set
39 the number of digits for individual columns.}
40
41 \item{row.names}{Logical: whether to include row names. By default, row names
42 are included if \code{rownames(x)} is neither \code{NULL} nor identical to
43 \code{1:nrow(x)}.}
44
45 \item{col.names}{A character vector of column names to be used in the table.}
46
47 \item{align}{Column alignment: a character vector consisting of \code{'l'}
48 (left), \code{'c'} (center) and/or \code{'r'} (right). By default or if
49 \code{align = NULL}, numeric columns are right-aligned, and other columns
50 are left-aligned. If \code{length(align) == 1L}, the string will be
51 expanded to a vector of individual letters, e.g. \code{'clc'} becomes
52 \code{c('c', 'l', 'c')}, unless the output format is LaTeX.}
53
54 \item{caption}{The table caption. By default, it is retrieved from the chunk
55 option \code{tab.cap}.}
56
57 \item{label}{The table reference label. By default, the label is obtained
58 from \code{knitr::\link{opts_current}$get('label')} (i.e., the current
59 chunk label). To disable the label, use \code{label = NA}.}
60
61 \item{format.args}{A list of arguments to be passed to \code{\link{format}()}
62 to format table values, e.g. \code{list(big.mark = ',')}.}
63
64 \item{escape}{Boolean; whether to escape special characters when producing
65 HTML or LaTeX tables. When \code{escape = FALSE}, you have to make sure
66 that special characters will not trigger syntax errors in LaTeX or HTML.}
67
68 \item{...}{Other arguments (see Examples and References).}
69 }
70 \value{
71 A character vector of the table source code.
72 }
73 \description{
74 A very simple table generator, and it is simple by design. It is not intended
75 to replace any other R packages for making tables. The \code{kable()}
76 function returns a single table for a single data object, and returns a table
77 that contains multiple tables if the input object is a list of data objects.
78 The \code{kables()} function is similar to \code{kable(x)} when \code{x} is a
79 list of data objects, but \code{kables()} accepts a list of \code{kable()}
80 values directly instead of data objects (see examples below).
81 }
82 \details{
83 Missing values (\code{NA}) in the table are displayed as \code{NA} by
84 default. If you want to display them with other characters, you can set the
85 option \code{knitr.kable.NA}, e.g. \code{options(knitr.kable.NA = '')} to
86 hide \code{NA} values.
87
88 You can set the option \code{knitr.kable.max_rows} to limit the number of
89 rows to show in the table, e.g., \code{options(knitr.kable.max_rows = 30)}.
90 }
91 \note{
92 When using \code{kable()} as a \emph{top-level} expression, you do not
93 need to explicitly \code{print()} it due to R's automatic implicit
94 printing. When it is wrapped inside other expressions (such as a
95 \code{\link{for}} loop), you must explicitly \code{print(kable(...))}.
96 }
97 \examples{
98 d1 = head(iris)
99 d2 = head(mtcars)
100 # pipe tables by default
101 kable(d1)
102 kable(d2[, 1:5])
103 # no inner padding
104 kable(d2, format = "pipe", padding = 0)
105 # more padding
106 kable(d2, format = "pipe", padding = 2)
107 kable(d1, format = "latex")
108 kable(d1, format = "html")
109 kable(d1, format = "latex", caption = "Title of the table")
110 kable(d1, format = "html", caption = "Title of the table")
111 # use the booktabs package
112 kable(mtcars, format = "latex", booktabs = TRUE)
113 # use the longtable package
114 kable(matrix(1000, ncol = 5), format = "latex", digits = 2, longtable = TRUE)
115 # change LaTeX default table environment
116 kable(d1, format = "latex", caption = "My table", table.envir = "table*")
117 # add some table attributes
118 kable(d1, format = "html", table.attr = "id=\"mytable\"")
119 # reST output
120 kable(d2, format = "rst")
121 # no row names
122 kable(d2, format = "rst", row.names = FALSE)
123 # Pandoc simple tables
124 kable(d2, format = "simple", caption = "Title of the table")
125 # format numbers using , as decimal point, and ' as thousands separator
126 x = as.data.frame(matrix(rnorm(60, 1e+06, 10000), 10))
127 kable(x, format.args = list(decimal.mark = ",", big.mark = "'"))
128 # save the value
129 x = kable(d2, format = "html")
130 cat(x, sep = "\n")
131 # can also set options(knitr.table.format = 'html') so that the output is HTML
132
133 # multiple tables via either kable(list(x1, x2)) or kables(list(kable(x1),
134 # kable(x2)))
135 kable(list(d1, d2), caption = "A tale of two tables")
136 kables(list(kable(d1, align = "l"), kable(d2)), caption = "A tale of two tables")
137 }
138 \references{
139 See
140 \url{https://bookdown.org/yihui/rmarkdown-cookbook/kable.html} for some
141 examples about this function, including specific arguments according to the
142 \code{format} selected.
143 }
144 \seealso{
145 Other R packages such as \pkg{huxtable}, \pkg{xtable},
146 \pkg{kableExtra}, \pkg{gt} and \pkg{tables} for HTML and LaTeX tables, and
147 \pkg{ascii} and \pkg{pander} for different flavors of markdown output and
148 some advanced features and table styles. For more on other packages for
149 creating tables, see
150 \url{https://bookdown.org/yihui/rmarkdown-cookbook/table-other.html}.
151 }