Add examples to the doc site
diff --git a/R/add_header_left.R b/R/add_header_left.R
index 1b4e1fe..6449879 100644
--- a/R/add_header_left.R
+++ b/R/add_header_left.R
@@ -23,6 +23,9 @@
#' only one for LaTeX. Option full_midline is a TRUE/FALSE option to control
#' if the mid line needs to be extended to the end of row.
#'
+#' @examples x <- knitr::kable(head(mtcars), "html")
+#' add_header_left(x, c("A" = 2, "B" = 2, "C" = 2))
+#'
#' @export
add_header_left <- function(kable_input, header = NULL, header_name = "",
align = "c", width = NULL, bold = F, italic = F,
diff --git a/R/collapse_rows.R b/R/collapse_rows.R
index f9602e8..d9a305c 100644
--- a/R/collapse_rows.R
+++ b/R/collapse_rows.R
@@ -9,6 +9,10 @@
#' @param kable_input Output of `knitr::kable()` with `format` specified
#' @param columns Numeric column positions where rows need to be collapsed.
#'
+#' @examples dt <- data.frame(a = c(1, 1, 2, 2), b = c("a", "a", "a", "b"))
+#' x <- knitr::kable(dt, "html")
+#' collapse_rows(x)
+#'
#' @export
collapse_rows <- function(kable_input, columns = NULL) {
# if (is.null(columns)) {
diff --git a/docs/awesome_table_in_html.Rmd b/docs/awesome_table_in_html.Rmd
index 9a60fcf..7232c89 100644
--- a/docs/awesome_table_in_html.Rmd
+++ b/docs/awesome_table_in_html.Rmd
@@ -228,3 +228,17 @@
row_spec(5, bold = T)
```
+# Collapse Rows in Selected Columns
+Function `group_rows` is great for showing simple structural information on rows but sometimes people may need to show structural information with multiple layers. When it happens, you may consider to use `collapse_rows` instead, which will put repeating cells in columns into multi-row cells.
+
+```{r}
+collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
+ C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
+ C3 = 1:15,
+ C4 = sample(c(0,1), 15, replace = TRUE))
+head(collapse_rows_dt)
+kable(collapse_rows_dt, "html", align = "c") %>%
+ kable_styling(full_width = F) %>%
+ column_spec(1, bold=T) %>%
+ collapse_rows(columns = 1:2)
+```
diff --git a/docs/awesome_table_in_html.html b/docs/awesome_table_in_html.html
index 421f706..61a69fc 100644
--- a/docs/awesome_table_in_html.html
+++ b/docs/awesome_table_in_html.html
@@ -11,7 +11,7 @@
<meta name="author" content="Hao Zhu" />
-<meta name="date" content="2017-06-21" />
+<meta name="date" content="2017-06-26" />
<title>Create Awesome HTML Table with knitr::kable and kableExtra</title>
@@ -217,7 +217,7 @@
<h1 class="title toc-ignore">Create Awesome HTML Table with knitr::kable and kableExtra</h1>
<h4 class="author"><em>Hao Zhu</em></h4>
-<h4 class="date"><em>2017-06-21</em></h4>
+<h4 class="date"><em>2017-06-26</em></h4>
</div>
@@ -3435,6 +3435,185 @@
</tbody>
</table>
</div>
+<div id="collapse-rows-in-selected-columns" class="section level1">
+<h1>Collapse Rows in Selected Columns</h1>
+<p>Function <code>group_rows</code> is great for showing simple structural information on rows but sometimes people may need to show structural information with multiple layers. When it happens, you may consider to use <code>collapse_rows</code> instead, which will put repeating cells in columns into multi-row cells.</p>
+<pre class="r"><code>collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
+ C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
+ C3 = 1:15,
+ C4 = sample(c(0,1), 15, replace = TRUE))
+head(collapse_rows_dt)</code></pre>
+<pre><code>## C1 C2 C3 C4
+## 1 a c 1 0
+## 2 a c 2 0
+## 3 a c 3 1
+## 4 a c 4 1
+## 5 a c 5 1
+## 6 a c 6 1</code></pre>
+<pre class="r"><code>kable(collapse_rows_dt, "html", align = "c") %>%
+ kable_styling(full_width = F) %>%
+ column_spec(1, bold=T) %>%
+ collapse_rows(columns = 1:2)</code></pre>
+<?xml version="1.0" encoding="UTF-8" ?>
+<table class="table" style="width: auto !important; margin-left: auto; margin-right: auto;">
+<thead>
+<tr>
+<th style="text-align:center;">
+C1
+</th>
+<th style="text-align:center;">
+C2
+</th>
+<th style="text-align:center;">
+C3
+</th>
+<th style="text-align:center;">
+C4
+</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td style="text-align:center;font-weight: bold;vertical-align: middle !important;" rowspan="10">
+a
+</td>
+<td style="text-align:center;vertical-align: middle !important;" rowspan="7">
+c
+</td>
+<td style="text-align:center;">
+1
+</td>
+<td style="text-align:center;">
+0
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+2
+</td>
+<td style="text-align:center;">
+0
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+3
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+4
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+5
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+6
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+7
+</td>
+<td style="text-align:center;">
+0
+</td>
+</tr>
+<tr>
+<td style="text-align:center;vertical-align: middle !important;" rowspan="3">
+d
+</td>
+<td style="text-align:center;">
+8
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+9
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+10
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+<tr>
+<td style="text-align:center;font-weight: bold;vertical-align: middle !important;" rowspan="5">
+b
+</td>
+<td style="text-align:center;vertical-align: middle !important;" rowspan="2">
+c
+</td>
+<td style="text-align:center;">
+11
+</td>
+<td style="text-align:center;">
+0
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+12
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+<tr>
+<td style="text-align:center;vertical-align: middle !important;" rowspan="3">
+d
+</td>
+<td style="text-align:center;">
+13
+</td>
+<td style="text-align:center;">
+0
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+14
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+<tr>
+<td style="text-align:center;">
+15
+</td>
+<td style="text-align:center;">
+1
+</td>
+</tr>
+</tbody>
+</table>
+</div>
diff --git a/docs/awesome_table_in_pdf.Rmd b/docs/awesome_table_in_pdf.Rmd
index 351686b..f75b87c 100644
--- a/docs/awesome_table_in_pdf.Rmd
+++ b/docs/awesome_table_in_pdf.Rmd
@@ -252,3 +252,17 @@
row_spec(5, bold = T)
```
+# Collapse Rows in Selected Columns
+Function `group_rows` is great for showing simple structural information on rows but sometimes people may need to show structural information with multiple layers. When it happens, you may consider to use `collapse_rows` instead, which will put repeating cells in columns into multi-row cells.
+
+```{r}
+collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
+ C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
+ C3 = 1:15,
+ C4 = sample(c(0,1), 15, replace = TRUE))
+head(collapse_rows_dt)
+kable(collapse_rows_dt, "latex", booktabs = T, align = "c") %>%
+ column_spec(1, bold=T) %>%
+ collapse_rows(columns = 1:2)
+```
+
diff --git a/docs/awesome_table_in_pdf.pdf b/docs/awesome_table_in_pdf.pdf
index 037484d..c8c4ded 100644
--- a/docs/awesome_table_in_pdf.pdf
+++ b/docs/awesome_table_in_pdf.pdf
Binary files differ
diff --git a/man/add_header_left.Rd b/man/add_header_left.Rd
index aab6b16..79847f8 100644
--- a/man/add_header_left.Rd
+++ b/man/add_header_left.Rd
@@ -39,3 +39,8 @@
`add_header_above`, users can use this function to add multiple layers of
heading columns one by one.
}
+\examples{
+x <- knitr::kable(head(mtcars), "html")
+add_header_left(x, c("A" = 2, "B" = 2, "C" = 2))
+
+}
diff --git a/man/collapse_rows.Rd b/man/collapse_rows.Rd
index 57b2d7c..49deb7f 100644
--- a/man/collapse_rows.Rd
+++ b/man/collapse_rows.Rd
@@ -18,3 +18,9 @@
and make them multirow cells. Note that if you want to use `column_spec` to
specify column styles, you should use `column_spec` before `collapse_rows`.
}
+\examples{
+dt <- data.frame(a = c(1, 1, 2, 2), b = c("a", "a", "a", "b"))
+x <- knitr::kable(dt, "html")
+collapse_rows(x)
+
+}