blob: 7f0a0e3590978efaa9a96958ff5f8a7d88083d47 [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.
Samia67d70572020-07-06 00:54:13 -04006#' @param level_of_indent a numeric value for the indent level
Hao Zhubd95bb22017-05-22 16:08:49 -04007#'
Hao Zhu78e61222017-05-24 20:53:35 -04008#' @examples x <- knitr::kable(head(mtcars), "html")
9#' # Add indentations to the 2nd & 4th row
Samia67d70572020-07-06 00:54:13 -040010#' add_indent(x, c(2, 4), level_of_indent = 1)
Hao Zhu78e61222017-05-24 20:53:35 -040011#'
Hao Zhu8f417202017-05-20 16:37:14 -040012#' @export
Samia67d70572020-07-06 00:54:13 -040013add_indent <- function(kable_input, positions, level_of_indent) {
Hao Zhud972e7f2017-05-22 13:27:15 -040014 if (!is.numeric(positions)) {
15 stop("Positions can only take numeric row numbers (excluding header rows).")
16 }
Hao Zhu8f417202017-05-20 16:37:14 -040017 kable_format <- attr(kable_input, "format")
18 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050019 warning("Please specify format in kable. kableExtra can customize either ",
20 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
21 "for details.")
Hao Zhu8f417202017-05-20 16:37:14 -040022 return(kable_input)
23 }
24 if (kable_format == "html") {
Samia67d70572020-07-06 00:54:13 -040025 return(add_indent_html(kable_input, positions, level_of_indent))
Hao Zhu8f417202017-05-20 16:37:14 -040026 }
27 if (kable_format == "latex") {
Samia67d70572020-07-06 00:54:13 -040028 return(add_indent_latex(kable_input, positions, level_of_indent))
Hao Zhu8f417202017-05-20 16:37:14 -040029 }
30}
31
Hao Zhu62cdde52017-05-20 22:16:03 -040032# Add indentation for LaTeX
Samia67d70572020-07-06 00:54:13 -040033add_indent_latex <- function(kable_input, positions, level_of_indent) {
Hao Zhud972e7f2017-05-22 13:27:15 -040034 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -040035 out <- solve_enc(kable_input)
Samia67d70572020-07-06 00:54:13 -040036 level_of_indent<-as.numeric(level_of_indent)
Samia6c4b24f2020-07-06 00:46:41 -040037
Hao Zhu064990d2017-10-17 18:08:42 -040038
39 if (table_info$duplicated_rows) {
40 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
41 out <- dup_fx_out[[1]]
42 table_info <- dup_fx_out[[2]]
43 }
Hao Zhu8f417202017-05-20 16:37:14 -040044
Hao Zhu37dbe3f2018-05-14 11:16:06 -040045 max_position <- table_info$nrow - table_info$position_offset
Leo83f05132018-05-10 14:12:16 +080046
47 if (max(positions) > max_position) {
Hao Zhu8f417202017-05-20 16:37:14 -040048 stop("There aren't that many rows in the table. Check positions in ",
49 "add_indent_latex.")
50 }
51
Hao Zhu37dbe3f2018-05-14 11:16:06 -040052 for (i in positions + table_info$position_offset) {
Leo83f05132018-05-10 14:12:16 +080053 rowtext <- table_info$contents[i]
Hao Zhu09d01262019-01-07 22:06:22 -050054 out <- sub(paste0(rowtext, "\\\\\\\\"),
55 paste0(latex_indent_unit(rowtext), "\\\\\\\\"),
Quôc Peyroted7e6242018-09-05 14:59:16 +020056 out, perl = TRUE)
Leo83f05132018-05-10 14:12:16 +080057 table_info$contents[i] <- latex_indent_unit(rowtext)
Hao Zhu8f417202017-05-20 16:37:14 -040058 }
Hao Zhu2ce42b92017-06-15 17:15:33 -040059 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -040060 attr(out, "kable_meta") <- table_info
Hao Zhu8f417202017-05-20 16:37:14 -040061 return(out)
Samia6c4b24f2020-07-06 00:46:41 -040062
63
Hao Zhu8f417202017-05-20 16:37:14 -040064}
65
66latex_indent_unit <- function(rowtext) {
Samia67d70572020-07-06 00:54:13 -040067 paste0("\\\\hspace\\{",level_of_indent,"em\\}", rowtext)
Hao Zhu8f417202017-05-20 16:37:14 -040068}
Hao Zhu62cdde52017-05-20 22:16:03 -040069
Samia6c4b24f2020-07-06 00:46:41 -040070
71
Hao Zhu62cdde52017-05-20 22:16:03 -040072# Add indentation for HTML
Samia67d70572020-07-06 00:54:13 -040073add_indent_html <- function(kable_input, positions, level_of_indent) {
Hao Zhu62cdde52017-05-20 22:16:03 -040074 kable_attrs <- attributes(kable_input)
75
Hao Zhu558c72f2017-07-24 15:12:00 -040076 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -040077 kable_tbody <- xml_tpart(kable_xml, "tbody")
78
79 group_header_rows <- attr(kable_input, "group_header_rows")
80 if (!is.null(group_header_rows)) {
81 positions <- positions_corrector(positions, group_header_rows,
82 length(xml_children(kable_tbody)))
83 }
Alan Butlerc81c1fe2018-01-30 11:16:18 -070084
Hao Zhu62cdde52017-05-20 22:16:03 -040085 for (i in positions) {
86 node_to_edit <- xml_child(xml_children(kable_tbody)[[i]], 1)
Alan Butlerc81c1fe2018-01-30 11:16:18 -070087 if (!xml_has_attr(node_to_edit, "indentlevel")) {
Hao Zhu62cdde52017-05-20 22:16:03 -040088 xml_attr(node_to_edit, "style") <- paste(
Samia67d70572020-07-06 00:54:13 -040089 xml_attr(node_to_edit, "style"), "padding-left: ",paste0(level_of_indent*2,"em;")
Hao Zhu62cdde52017-05-20 22:16:03 -040090 )
Alan Butlerc81c1fe2018-01-30 11:16:18 -070091 xml_attr(node_to_edit, "indentlevel") <- 1
Hao Zhu62cdde52017-05-20 22:16:03 -040092 } else {
Alan Butlerc81c1fe2018-01-30 11:16:18 -070093 indentLevel <- as.numeric(xml_attr(node_to_edit, "indentlevel"))
Hao Zhu62cdde52017-05-20 22:16:03 -040094 xml_attr(node_to_edit, "style") <- sub(
95 paste0("padding-left: ", indentLevel * 2, "em;"),
96 paste0("padding-left: ", (indentLevel + 1) * 2, "em;"),
97 xml_attr(node_to_edit, "style")
98 )
Alan Butlerc81c1fe2018-01-30 11:16:18 -070099 xml_attr(node_to_edit, "indentlevel") <- indentLevel + 1
Hao Zhu62cdde52017-05-20 22:16:03 -0400100 }
101 }
Hao Zhuf2dfd142017-07-24 14:43:28 -0400102 out <- as_kable_xml(kable_xml)
Hao Zhu62cdde52017-05-20 22:16:03 -0400103 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500104 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu62cdde52017-05-20 22:16:03 -0400105 return(out)
106}