Merge pull request #711 from krlmlr/f-row-pos-first
Add row_group_label_position = first option
diff --git a/R/collapse_rows.R b/R/collapse_rows.R
index 81c8f97..cb64a8e 100644
--- a/R/collapse_rows.R
+++ b/R/collapse_rows.R
@@ -9,18 +9,21 @@
#' @param kable_input Output of `knitr::kable()` with `format` specified
#' @param columns A numeric value or vector indicating in which column(s) rows
#' need to be collapsed.
-#' @param valign Select from "top", "middle"(default), "bottom". The reason why
+#' @param valign Select from "top", "middle" (default), "bottom". The reason why
#' "top" is not default is that the multirow package on CRAN win-builder is
#' not up to date.
+#' Only used when `row_group_label_position` is `identity`.
#' @param latex_hline Option controlling the behavior of adding hlines to table.
#' Choose from `full`, `major`, `none`, `custom` and `linespace`.
#' @param custom_latex_hline Numeric column positions whose collapsed rows will
#' be separated by hlines.
#' @param row_group_label_position Option controlling positions of row group
-#' labels. Choose from `identity`, `stack`.
+#' labels. Choose from `identity`, `stack`, or `first` -- the latter behaves
+#' like `identity` when `row_group_label_position` is `top` but without using
+#' the multirow package.
#' @param row_group_label_fonts A list of arguments that can be supplied to
#' group_rows function to format the row group label when
-#' `row_group_label_position` is `stack`
+#' `row_group_label_position` is `stack`.
#' @param headers_to_remove Numeric column positions where headers should be
#' removed when they are stacked.
#' @param target If multiple columns are selected to do collapsing and a target
@@ -46,7 +49,7 @@
valign = c("middle", "top", "bottom"),
latex_hline = c("full", "major", "none", "custom",
"linespace"),
- row_group_label_position = c('identity', 'stack'),
+ row_group_label_position = c("identity", "stack", "first"),
custom_latex_hline = NULL,
row_group_label_fonts = NULL,
headers_to_remove = NULL,
@@ -72,7 +75,7 @@
if (kable_format == "latex") {
latex_hline <- match.arg(latex_hline)
row_group_label_position <- match.arg(row_group_label_position,
- c('identity', 'stack'))
+ c("identity", "stack", "first"))
return(collapse_rows_latex(kable_input, columns, latex_hline, valign,
row_group_label_position, row_group_label_fonts, custom_latex_hline,
headers_to_remove, target, col_names, longtable_clean_cut))
@@ -200,6 +203,10 @@
if(columns[j] < ncol(collapse_matrix) || collapse_matrix_rev[i, j] == 0){
new_kable_dt[i, columns[j]] <- ''
}
+ } else if(row_group_label_position == 'first'){
+ if(columns[j] <= ncol(collapse_matrix) && collapse_matrix_rev[i, j] == 0){
+ new_kable_dt[i, columns[j]] <- ''
+ }
} else {
new_kable_dt[i, columns[j]] <- collapse_new_dt_item(
kable_dt[i, columns[j]], collapse_matrix[i, j], column_width,
diff --git a/man/collapse_rows.Rd b/man/collapse_rows.Rd
index 8efcb55..a78e29d 100644
--- a/man/collapse_rows.Rd
+++ b/man/collapse_rows.Rd
@@ -9,7 +9,7 @@
columns = NULL,
valign = c("middle", "top", "bottom"),
latex_hline = c("full", "major", "none", "custom", "linespace"),
- row_group_label_position = c("identity", "stack"),
+ row_group_label_position = c("identity", "stack", "first"),
custom_latex_hline = NULL,
row_group_label_fonts = NULL,
headers_to_remove = NULL,
@@ -24,22 +24,25 @@
\item{columns}{A numeric value or vector indicating in which column(s) rows
need to be collapsed.}
-\item{valign}{Select from "top", "middle"(default), "bottom". The reason why
+\item{valign}{Select from "top", "middle" (default), "bottom". The reason why
"top" is not default is that the multirow package on CRAN win-builder is
-not up to date.}
+not up to date.
+Only used when \code{row_group_label_position} is \code{identity}.}
\item{latex_hline}{Option controlling the behavior of adding hlines to table.
Choose from \code{full}, \code{major}, \code{none}, \code{custom} and \code{linespace}.}
\item{row_group_label_position}{Option controlling positions of row group
-labels. Choose from \code{identity}, \code{stack}.}
+labels. Choose from \code{identity}, \code{stack}, or \code{first} -- the latter behaves
+like \code{identity} when \code{row_group_label_position} is \code{top} but without using
+the multirow package.}
\item{custom_latex_hline}{Numeric column positions whose collapsed rows will
be separated by hlines.}
\item{row_group_label_fonts}{A list of arguments that can be supplied to
group_rows function to format the row group label when
-\code{row_group_label_position} is \code{stack}}
+\code{row_group_label_position} is \code{stack}.}
\item{headers_to_remove}{Numeric column positions where headers should be
removed when they are stacked.}
diff --git a/vignettes/awesome_table_in_pdf.Rmd b/vignettes/awesome_table_in_pdf.Rmd
index 7025dd7..25518b7 100644
--- a/vignettes/awesome_table_in_pdf.Rmd
+++ b/vignettes/awesome_table_in_pdf.Rmd
@@ -493,7 +493,9 @@
## Group rows via multi-row cell
Function `pack_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 using `collapse_rows` instead, which will put repeating cells in columns into multi-row cells.
-In LaTeX, `collapse_rows` adds some extra hlines to help differentiate groups. You can customize this behavior using the `latex_hline` argument. You can choose from `full` (default), `major` and `none`. Vertical alignment of cells is controlled by the `valign` option. You can choose from "top", "middle"(default) and "bottom". Be cautious that the vertical alignment option was only introduced in multirow in 2016. If you are using a legacy LaTeX distribution, you will run into trouble if you set `valign` to be either "top" or "bottom".
+In LaTeX, `collapse_rows` adds some extra hlines to help differentiate groups. You can customize this behavior using the `latex_hline` argument. You can choose from `full` (default), `major` and `none`.
+
+Vertical alignment of cells (with the default `row_group_label_position = "identity")`) is controlled by the `valign` option. You can choose from "top", "middle" (default) and "bottom". Be cautious that the vertical alignment option was only introduced in multirow in 2016. If you are using a legacy LaTeX distribution, you will run into trouble if you set `valign` to be either "top" or "bottom". Alternatively, use `row_group_label_position = "first"`, which will put the row group labels into the first row without using the `\multirow` LaTeX command at all.
```{r}
collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
@@ -502,7 +504,7 @@
C4 = sample(c(0,1), 15, replace = TRUE))
kbl(collapse_rows_dt, booktabs = T, align = "c") %>%
column_spec(1, bold=T) %>%
- collapse_rows(columns = 1:2, latex_hline = "major", valign = "middle")
+ collapse_rows(columns = 1:2, latex_hline = "major", row_group_label_position = "first")
```
Right now, you can't automatically make striped rows based on collapsed rows but you can do it manually via the `extra_latex_after` option in `row_spec`. This feature is not officially supported. I'm only document it here if you want to give it a try.