-
Notifications
You must be signed in to change notification settings - Fork 584
/
Copy path091-knitr-table.Rnw
70 lines (52 loc) · 1.49 KB
/
091-knitr-table.Rnw
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\title{Demos of tables with \texttt{kable()} in knitr}
\author{Yihui Xie}
\begin{document}
\maketitle
<<setup, include=FALSE>>=
library(knitr)
# in this document we mainly use results='asis', so set it globally
opts_chunk$set(results='asis')
@
The function \texttt{kable()} is a simple table generator. Unlike
sophisticated packages (e.g. \textbf{xtable}, which has more than 30
arguments), this function only has a minimal number of arguments to generate
simple tables.
<<test-a>>=
library(knitr)
kable(head(iris))
kable(head(iris, 0))
kable(head(iris, 1))
@
\section{No row names}
<<test-b>>=
kable(head(iris), row.names=FALSE)
@
\section{At most 3 digits}
<<test-c>>=
kable(matrix(rnorm(20),4), digits=3)
@
\section{Alignment}
By default, numeric columns are right-aligned, and other columns are
left-aligned.
<<test-d>>=
kable(head(iris), align=c('l', 'c', 'r', 'l', 'r'))
@
\section{Default tables are ugly}
Let's use the \textbf{booktabs} package. The air is suddenly fresh!
<<test-e>>=
kable(head(iris), row.names=FALSE, booktabs=TRUE)
kable(head(iris, 0), row.names=FALSE, booktabs=TRUE)
kable(head(iris, 1), row.names=FALSE, booktabs=TRUE)
kable(head(mtcars[, 1:7]), booktabs=TRUE)
@
\section{Long tables}
<<test-f>>=
kable(mtcars[, 1:7], longtable=TRUE, booktabs=TRUE)
@
\section{Want more features?}
No, that is all I have. You should turn to other packages for help. I'm not
going to reinvent their wheels.
\end{document}