Merge pull request #136 from bsalzer/master
additional options for latex charts
diff --git a/R/group_rows.R b/R/group_rows.R
index 7300eb3..6cc8ec5 100644
--- a/R/group_rows.R
+++ b/R/group_rows.R
@@ -19,6 +19,12 @@
#' documents.
#' @param escape A T/F value showing whether special characters should be
#' escaped.
+#' @param latex_align Adjust justification of group_label in latex only. Value should be "c" for
+#' centered on row, "r" for right justification, or "l" for left justification. Default
+#' Value is "l" If using html, the alignment can be set by using the label_row_css
+#' parameter.
+#' @param colnum A numeric that determines how many columns the text should span.
+#' The default setting will have the text span the entire length.
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' # Put Row 2 to Row 5 into a Group and label it as "Group A"
@@ -30,7 +36,7 @@
index = NULL,
label_row_css = "border-bottom: 1px solid;",
latex_gap_space = "0.3em",
- escape = TRUE) {
+ escape = TRUE, latex_align = "l", colnum = NULL) {
kable_format <- attr(kable_input, "format")
if (!kable_format %in% c("html", "latex")) {
@@ -41,28 +47,32 @@
}
if (is.null(index)) {
if (kable_format == "html") {
+ if(!missing(latex_align)) warning("latex_align parameter is not used in HTML Mode,
+ use label_row_css instead.")
return(group_rows_html(kable_input, group_label, start_row, end_row,
- label_row_css, escape))
+ label_row_css, escape, colnum))
}
if (kable_format == "latex") {
return(group_rows_latex(kable_input, group_label, start_row, end_row,
- latex_gap_space, escape))
+ latex_gap_space, escape, latex_align, colnum))
}
} else {
index <- group_row_index_translator(index)
out <- kable_input
if (kable_format == "html") {
for (i in 1:nrow(index)) {
+ if(!missing(latex_align)) warning("latex_align parameter is not used in HTML Mode,
+ use label_row_css instead.")
out <- group_rows_html(out, index$header[i],
index$start[i], index$end[i],
- label_row_css, escape)
+ label_row_css, escape, colnum)
}
}
if (kable_format == "latex") {
for (i in 1:nrow(index)) {
out <- group_rows_latex(out, index$header[i],
index$start[i], index$end[i],
- latex_gap_space, escape)
+ latex_gap_space, escape, latex_align, colnum)
}
}
return(out)
@@ -79,7 +89,7 @@
}
group_rows_html <- function(kable_input, group_label, start_row, end_row,
- label_row_css, escape) {
+ label_row_css, escape, colnum) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
kable_tbody <- xml_tpart(kable_xml, "tbody")
@@ -97,7 +107,9 @@
# Insert a group header row
starting_node <- xml_child(kable_tbody, group_seq[1])
- kable_ncol <- length(xml_children(starting_node))
+ kable_ncol <- ifelse(is.null(colnum),
+ length(xml_children(starting_node)),
+ colnum)
group_header_row_text <- paste0(
'<tr groupLength="', length(group_seq), '"><td colspan="', kable_ncol,
'" style="', label_row_css, '"><strong>', group_label,
@@ -115,7 +127,7 @@
}
group_rows_latex <- function(kable_input, group_label, start_row, end_row,
- gap_space, escape) {
+ gap_space, escape, latex_align, colnum) {
table_info <- magic_mirror(kable_input)
out <- enc2utf8(as.character(kable_input))
@@ -135,14 +147,17 @@
if (table_info$booktabs) {
new_rowtext <- paste0(
"\\\\addlinespace[", gap_space, "]\n",
- "\\\\multicolumn{", table_info$ncol, "}{l}{\\\\textbf{", group_label,
+ "\\\\multicolumn{", ifelse(is.null(colnum),
+ table_info$ncol,
+ colnum),
+ "}{", latex_align, "}{\\\\textbf{", group_label,
"}}\\\\\\\\\n",
rowtext
)
} else {
rowtext <- paste0("\\\\hline\n", rowtext)
new_rowtext <- paste0(
- "\\\\hline\n\\\\multicolumn{", table_info$ncol, "}{l}{\\\\textbf{",
+ "\\\\hline\n\\\\multicolumn{", table_info$ncol, "}{", latex_align,"}{\\\\textbf{",
group_label, "}}\\\\\\\\\n", rowtext
)
}
diff --git a/R/kable_styling.R b/R/kable_styling.R
index 305d3d0..bad743d 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -35,6 +35,9 @@
#' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be
#' imported.
#' @param font_size A numeric input for table font size
+#' @param row_label_position A character string determining the justification of the row
+#' labels in a table. Possible values inclued `l` for left, `c` for center, and `r` for
+#' right. The default value is `l` for left justifcation.
#' @param ... extra options for HTML or LaTeX. See `details`.
#'
#' @details For LaTeX, extra options includes:
@@ -59,6 +62,7 @@
full_width = NULL,
position = "center",
font_size = NULL,
+ row_label_position = "l",
...) {
if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
@@ -88,6 +92,9 @@
if (is.null(full_width)) {
full_width <- getOption("kable_styling_full_width", T)
}
+ if(!missing(row_label_position)) {
+ warning("'row_label_position' is not active for HTML tables yet and parameter will not be used.")
+ }
return(htmlTable_styling(kable_input,
bootstrap_options = bootstrap_options,
full_width = full_width,
@@ -102,7 +109,9 @@
latex_options = latex_options,
full_width = full_width,
position = position,
- font_size = font_size, ...))
+ font_size = font_size,
+ row_label_position = row_label_position,
+ ...))
}
}
@@ -186,7 +195,8 @@
repeat_header_method = c("append", "replace"),
repeat_header_continued = FALSE,
stripe_color = "gray!6",
- latex_table_env = NULL) {
+ latex_table_env = NULL,
+ row_label_position) {
latex_options <- match.arg(
latex_options,
@@ -249,6 +259,21 @@
out <- structure(out, format = "latex", class = "knitr_kable")
attr(out, "kable_meta") <- table_info
+
+ if (row_label_position != "l") {
+ if(table_info$tabular=="longtable") {
+ out <- sub("\\\\begin\\{longtable\\}\\{l",
+ paste0("\\\\begin\\{longtable\\}\\{",
+ row_label_position),
+ out)
+ } else {
+ out <- sub("\\\\begin\\{tabular\\}\\{l",
+ paste0("\\\\begin\\{tabular\\}\\{",
+ row_label_position),
+ out)
+ }
+ }
+
return(out)
}
diff --git a/tests/visual_tests/add_indent_and_group_rows_pdf.Rmd b/tests/visual_tests/add_indent_and_group_rows_pdf.Rmd
index 06439c8..b942a0a 100644
--- a/tests/visual_tests/add_indent_and_group_rows_pdf.Rmd
+++ b/tests/visual_tests/add_indent_and_group_rows_pdf.Rmd
@@ -30,7 +30,7 @@
dplyr::mutate(wt = paste0("%", mpg)) %>%
kable(format = "latex", booktabs = T) %>%
kable_styling() %>%
- group_rows("Group 1", 4, 7) %>%
+ group_rows("Group 1", 4, 7, latex_align='r',colnum=3) %>%
group_rows("Group 2", 8, 10)
```
@@ -39,7 +39,7 @@
kable(format = "latex", booktabs = T) %>%
kable_styling() %>%
group_rows("Group 1", 4, 7) %>%
- group_rows("Group 2", 8, 10) %>%
+ group_rows("Group 2", 8, 10, latex_align='c') %>%
group_rows("Group 2", 20, 22)
aa <- mtcars