blob: 741a478dcd19a548ddbec828943b433e215b33c7 [file] [log] [blame]
Hao Zhu014d6212017-08-07 04:20:23 +08001## ------------------------------------------------------------------------
2library(knitr)
3library(kableExtra)
4dt <- mtcars[1:5, 1:6]
5
6## ------------------------------------------------------------------------
7options(knitr.table.format = "latex")
8## If you don't define format here, you'll need put `format = "latex"`
9## in every kable function.
10
11## ------------------------------------------------------------------------
12kable(dt)
13
14## ------------------------------------------------------------------------
15kable(dt, booktabs = T)
16
17## ------------------------------------------------------------------------
18kable(dt, booktabs = T) %>%
19 kable_styling(latex_options = "striped")
20
21## ------------------------------------------------------------------------
22kable(dt, caption = "Demo table", booktabs = T) %>%
23 kable_styling(latex_options = c("striped", "hold_position"))
24
25## ------------------------------------------------------------------------
26kable(cbind(dt, dt, dt), booktabs = T) %>%
27 kable_styling(latex_options = c("striped", "scale_down"))
28
29## ------------------------------------------------------------------------
30kable(cbind(dt), booktabs = T) %>%
31 kable_styling(latex_options = c("striped", "scale_down"))
32
33## ------------------------------------------------------------------------
34long_dt <- rbind(mtcars, mtcars)
35
36kable(long_dt, longtable = T, booktabs = T, caption = "Longtable") %>%
37 add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) %>%
38 kable_styling(latex_options = c("repeat_header"))
39
40## ------------------------------------------------------------------------
41kable(dt, booktabs = T) %>%
42 kable_styling(full_width = T)
43
44## ------------------------------------------------------------------------
45kable(dt, booktabs = T) %>%
46 kable_styling(position = "center")
47
48## ------------------------------------------------------------------------
49kable(dt, booktabs = T) %>%
50 kable_styling(position = "float_right")
51
52## ------------------------------------------------------------------------
53kable(dt, booktabs = T) %>%
54 kable_styling(font_size = 7)
55
56## ------------------------------------------------------------------------
57text_tbl <- data.frame(
58 Items = c("Item 1", "Item 2", "Item 3"),
59 Features = c(
60 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. ",
61 "In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. ",
62 "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. "
63 )
64)
65
66kable(text_tbl, booktabs = T) %>%
67 kable_styling(full_width = F) %>%
68 column_spec(1, bold = T) %>%
69 column_spec(2, width = "30em")
70
71## ------------------------------------------------------------------------
72kable(dt, booktabs = T) %>%
73 kable_styling("striped", full_width = F) %>%
74 column_spec(7, bold = T) %>%
75 row_spec(5, bold = T)
76
77## ------------------------------------------------------------------------
78kable(dt, booktabs = T) %>%
79 kable_styling() %>%
80 add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2))
81
82## ------------------------------------------------------------------------
83kable(dt, booktabs = T) %>%
84 kable_styling(latex_options = "striped") %>%
85 add_header_above(c(" ", "Group 1" = 2, "Group 2" = 2, "Group 3" = 2)) %>%
86 add_header_above(c(" ", "Group 4" = 4, "Group 5" = 2)) %>%
87 add_header_above(c(" ", "Group 6" = 6), bold = T, italic = T)
88
89## ------------------------------------------------------------------------
90kable(mtcars[1:10, 1:6], caption = "Group Rows", booktabs = T) %>%
91 kable_styling() %>%
92 group_rows("Group 1", 4, 7) %>%
93 group_rows("Group 2", 8, 10)
94
95## ------------------------------------------------------------------------
96kable(dt, booktabs = T) %>%
97 group_rows("Group 1", 4, 5, latex_gap_space = "2em")
98
99## ------------------------------------------------------------------------
100kable(dt, booktabs = T) %>%
101 add_indent(c(1, 3, 5))
102
103## ------------------------------------------------------------------------
104collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
105 C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
106 C3 = 1:15,
107 C4 = sample(c(0,1), 15, replace = TRUE))
108kable(collapse_rows_dt, "latex", booktabs = T, align = "c") %>%
109 column_spec(1, bold=T) %>%
110 collapse_rows(columns = 1:2)
111
112## ------------------------------------------------------------------------
113kable(collapse_rows_dt, "latex", align = "c") %>%
114 column_spec(1, bold = T, width = "5em") %>%
115 collapse_rows(1:2)
116
117## ------------------------------------------------------------------------
118kable(dt, booktabs = T) %>%
119 kable_styling() %>%
120 add_footnote(c("Footnote 1", "Have a good day."), notation = "alphabet")
121
122## ------------------------------------------------------------------------
123kable(dt, booktabs = T) %>%
124 kable_styling() %>%
125 add_footnote(c("Footnote 1", "Have a good day."), notation = "number")
126
127## ------------------------------------------------------------------------
128kable(dt, booktabs = T) %>%
129 kable_styling() %>%
130 add_footnote(c("Footnote 1", "Footnote 2", "Footnote 3"), notation = "symbol")
131
132## ------------------------------------------------------------------------
133kable(dt, caption = "Demo Table[note]", booktabs = T) %>%
134 kable_styling(latex_options = "hold_position") %>%
135 add_header_above(c(" ", "Group 1[note]" = 3, "Group 2[note]" = 3)) %>%
136 add_footnote(c("This table is from mtcars",
137 "Group 1 contains mpg, cyl and disp",
138 "Group 2 contains hp, drat and wt"),
139 notation = "symbol")
140
141## ------------------------------------------------------------------------
142kable(dt, caption = "Demo Table (Landscape)[note]", booktabs = T) %>%
143 kable_styling(latex_options = c("hold_position")) %>%
144 add_header_above(c(" ", "Group 1[note]" = 3, "Group 2[note]" = 3)) %>%
145 add_footnote(c("This table is from mtcars",
146 "Group 1 contains mpg, cyl and disp",
147 "Group 2 contains hp, drat and wt"),
148 notation = "symbol") %>%
149 group_rows("Group 1", 4, 5) %>%
150 landscape()
151