blob: a0cb37f928a2c694b44d94f8d6f52259ffdddcc8 [file] [log] [blame]
Hao Zhu8f417202017-05-20 16:37:14 -04001#' Add indentations to row headers
Hao Zhubd95bb22017-05-22 16:08:49 -04002#'
3#' @param kable_input Output of `knitr::kable()` with `format` specified
4#' @param positions A vector of numeric row numbers for the rows that need to
5#' be indented.
6#'
Hao Zhu78e61222017-05-24 20:53:35 -04007#' @examples x <- knitr::kable(head(mtcars), "html")
8#' # Add indentations to the 2nd & 4th row
9#' add_indent(x, c(2, 4))
10#'
Hao Zhu8f417202017-05-20 16:37:14 -040011#' @export
12add_indent <- function(kable_input, positions) {
Hao Zhud972e7f2017-05-22 13:27:15 -040013 if (!is.numeric(positions)) {
14 stop("Positions can only take numeric row numbers (excluding header rows).")
15 }
Hao Zhu8f417202017-05-20 16:37:14 -040016 kable_format <- attr(kable_input, "format")
17 if (!kable_format %in% c("html", "latex")) {
18 message("Currently generic markdown table using pandoc is not supported.")
19 return(kable_input)
20 }
21 if (kable_format == "html") {
Hao Zhu62cdde52017-05-20 22:16:03 -040022 return(add_indent_html(kable_input, positions))
Hao Zhu8f417202017-05-20 16:37:14 -040023 }
24 if (kable_format == "latex") {
25 return(add_indent_latex(kable_input, positions))
26 }
27}
28
Hao Zhu62cdde52017-05-20 22:16:03 -040029# Add indentation for LaTeX
Hao Zhu8f417202017-05-20 16:37:14 -040030add_indent_latex <- function(kable_input, positions) {
Hao Zhud972e7f2017-05-22 13:27:15 -040031 table_info <- magic_mirror(kable_input)
Hao Zhu064990d2017-10-17 18:08:42 -040032 out <- enc2utf8(as.character(kable_input))
33
34 if (table_info$duplicated_rows) {
35 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
36 out <- dup_fx_out[[1]]
37 table_info <- dup_fx_out[[2]]
38 }
Hao Zhu8f417202017-05-20 16:37:14 -040039
Hao Zhu8f417202017-05-20 16:37:14 -040040 if (max(positions) > table_info$nrow - 1) {
41 stop("There aren't that many rows in the table. Check positions in ",
42 "add_indent_latex.")
43 }
44
Hao Zhu8f417202017-05-20 16:37:14 -040045 for (i in positions) {
46 rowtext <- table_info$contents[i + 1]
Hao Zhue5bfbf12017-06-06 00:24:49 -040047 out <- sub(rowtext, latex_indent_unit(rowtext), out, perl = TRUE)
Hao Zhu2ce42b92017-06-15 17:15:33 -040048 table_info$contents[i + 1] <- latex_indent_unit(rowtext)
Hao Zhu8f417202017-05-20 16:37:14 -040049 }
Hao Zhu2ce42b92017-06-15 17:15:33 -040050 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -040051 attr(out, "kable_meta") <- table_info
Hao Zhu8f417202017-05-20 16:37:14 -040052 return(out)
53}
54
55latex_indent_unit <- function(rowtext) {
Hao Zhu2ce42b92017-06-15 17:15:33 -040056 paste0("\\\\hspace\\{1em\\}", rowtext)
Hao Zhu8f417202017-05-20 16:37:14 -040057}
Hao Zhu62cdde52017-05-20 22:16:03 -040058
59# Add indentation for HTML
60add_indent_html <- function(kable_input, positions) {
61 kable_attrs <- attributes(kable_input)
62
Hao Zhu558c72f2017-07-24 15:12:00 -040063 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -040064 kable_tbody <- xml_tpart(kable_xml, "tbody")
65
66 group_header_rows <- attr(kable_input, "group_header_rows")
67 if (!is.null(group_header_rows)) {
68 positions <- positions_corrector(positions, group_header_rows,
69 length(xml_children(kable_tbody)))
70 }
71 for (i in positions) {
72 node_to_edit <- xml_child(xml_children(kable_tbody)[[i]], 1)
73 if (!xml_has_attr(node_to_edit, "indentLevel")) {
74 xml_attr(node_to_edit, "style") <- paste(
75 xml_attr(node_to_edit, "style"), "padding-left: 2em;"
76 )
77 xml_attr(node_to_edit, "indentLevel") <- 1
78 } else {
79 indentLevel <- as.numeric(xml_attr(node_to_edit, "indentLevel"))
80 xml_attr(node_to_edit, "style") <- sub(
81 paste0("padding-left: ", indentLevel * 2, "em;"),
82 paste0("padding-left: ", (indentLevel + 1) * 2, "em;"),
83 xml_attr(node_to_edit, "style")
84 )
85 xml_attr(node_to_edit, "indentLevel") <- indentLevel + 1
86 }
87 }
Hao Zhuf2dfd142017-07-24 14:43:28 -040088 out <- as_kable_xml(kable_xml)
Hao Zhu62cdde52017-05-20 22:16:03 -040089 attributes(out) <- kable_attrs
90 return(out)
91}