blob: 5cf071e25f01e67ef33c7e4cb9f45e8343989ca0 [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 = "html")
8## If you don't define format here, you'll need put `format = "html"` in every kable function.
9
10## ------------------------------------------------------------------------
11kable(dt)
12
13## ------------------------------------------------------------------------
14kable(dt) %>%
15 kable_styling()
16
17## ------------------------------------------------------------------------
18kable(dt) %>%
19 kable_styling(bootstrap_options = c("striped", "hover"))
20
21## ------------------------------------------------------------------------
22kable(dt) %>%
23 kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
24
25## ------------------------------------------------------------------------
26kable(dt) %>%
27 kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
28
29## ------------------------------------------------------------------------
30kable(dt) %>%
31 kable_styling(bootstrap_options = "striped", full_width = F)
32
33## ------------------------------------------------------------------------
34kable(dt) %>%
35 kable_styling(bootstrap_options = "striped", full_width = F, position = "left")
36
37## ------------------------------------------------------------------------
38kable(dt) %>%
39 kable_styling(bootstrap_options = "striped", full_width = F, position = "float_right")
40
41## ------------------------------------------------------------------------
42kable(dt) %>%
43 kable_styling(bootstrap_options = "striped", font_size = 7)
44
45## ------------------------------------------------------------------------
46kable(dt) %>%
47 kable_styling("striped") %>%
48 add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2))
49
50## ------------------------------------------------------------------------
51kable(dt) %>%
52 kable_styling(c("striped", "bordered")) %>%
53 add_header_above(c(" ", "Group 1" = 2, "Group 2" = 2, "Group 3" = 2)) %>%
54 add_header_above(c(" ", "Group 4" = 4, "Group 5" = 2)) %>%
55 add_header_above(c(" ", "Group 6" = 6))
56
57## ------------------------------------------------------------------------
58kable(dt) %>%
59 kable_styling("striped") %>%
60 add_footnote(c("Footnote 1", "Have a good day."), notation = "alphabet")
61
62## ------------------------------------------------------------------------
63kable(dt) %>%
64 kable_styling("striped") %>%
65 add_footnote(c("Footnote 1", "Have a good day."), notation = "number")
66
67## ------------------------------------------------------------------------
68kable(dt) %>%
69 kable_styling("striped") %>%
70 add_footnote(c("Footnote 1", "Footnote 2", "Footnote 3"), notation = "symbol")
71
72## ------------------------------------------------------------------------
73kable(dt, caption = "Demo Table[note]") %>%
74 kable_styling("striped") %>%
75 add_header_above(c(" ", "Group 1[note]" = 3, "Group 2[note]" = 3)) %>%
76 add_footnote(c("This table is from mtcars",
77 "Group 1 contains mpg, cyl and disp",
78 "Group 2 contains hp, drat and wt"),
79 notation = "symbol")
80
81## ------------------------------------------------------------------------
82kable(mtcars[1:10, 1:6], caption = "Group Rows") %>%
83 kable_styling("striped", full_width = F) %>%
84 group_rows("Group 1", 4, 7) %>%
85 group_rows("Group 2", 8, 10)
86
87## ------------------------------------------------------------------------
88kable(dt) %>%
89 kable_styling("striped", full_width = F) %>%
90 group_rows("Group 1", 3, 5, label_row_css = "background-color: #666; color: #fff;")
91
92## ------------------------------------------------------------------------
93kable(dt) %>%
94 kable_styling("striped", full_width = F) %>%
95 add_indent(c(1, 3, 5))
96
97## ------------------------------------------------------------------------
98text_tbl <- data.frame(
99 Items = c("Item 1", "Item 2", "Item 3"),
100 Features = c(
101 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. ",
102 "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. ",
103 "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. "
104 )
105)
106
107kable(text_tbl) %>%
108 kable_styling(full_width = F) %>%
109 column_spec(1, bold = T) %>%
110 column_spec(2, width = "30em")
111
112## ------------------------------------------------------------------------
113kable(dt) %>%
114 kable_styling("striped", full_width = F) %>%
115 column_spec(7, bold = T) %>%
116 row_spec(5, bold = T)
117
118## ------------------------------------------------------------------------
119collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
120 C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
121 C3 = 1:15,
122 C4 = sample(c(0,1), 15, replace = TRUE))
123kable(collapse_rows_dt, "html", align = "c") %>%
124 kable_styling(full_width = F) %>%
125 column_spec(1, bold=T) %>%
126 collapse_rows(columns = 1:2)
127