blob: b53423fefc008d693f4cdd8dc72513b9591e0789 [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.
Hao Zhu9410a272020-08-03 01:11:47 -04006#' @param level_of_indent a numeric value for the indent level. Default is 1.
Hao Zhu8f46db82020-08-18 21:48:23 -04007#' @param all_cols T/F whether to apply indentation to all columns
Hao Zhubd95bb22017-05-22 16:08:49 -04008#'
Hao Zhu9399dcc2020-08-26 17:27:38 -04009#' @examples
10#' \dontrun{
11#' x <- knitr::kable(head(mtcars), "html")
Hao Zhu78e61222017-05-24 20:53:35 -040012#' # Add indentations to the 2nd & 4th row
Samia67d70572020-07-06 00:54:13 -040013#' add_indent(x, c(2, 4), level_of_indent = 1)
Hao Zhu9399dcc2020-08-26 17:27:38 -040014#' }
Hao Zhu78e61222017-05-24 20:53:35 -040015#'
Hao Zhu8f417202017-05-20 16:37:14 -040016#' @export
Hao Zhu9410a272020-08-03 01:11:47 -040017add_indent <- function(kable_input, positions,
18 level_of_indent = 1, all_cols = FALSE) {
Samia106ad582020-07-15 14:19:09 -040019
Hao Zhud972e7f2017-05-22 13:27:15 -040020 if (!is.numeric(positions)) {
21 stop("Positions can only take numeric row numbers (excluding header rows).")
22 }
Hao Zhu8f417202017-05-20 16:37:14 -040023 kable_format <- attr(kable_input, "format")
24 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050025 warning("Please specify format in kable. kableExtra can customize either ",
26 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
27 "for details.")
Hao Zhu8f417202017-05-20 16:37:14 -040028 return(kable_input)
29 }
30 if (kable_format == "html") {
Hao Zhu9410a272020-08-03 01:11:47 -040031 return(add_indent_html(kable_input, positions, level_of_indent, all_cols))
Hao Zhu8f417202017-05-20 16:37:14 -040032 }
33 if (kable_format == "latex") {
Hao Zhu9410a272020-08-03 01:11:47 -040034 return(add_indent_latex(kable_input, positions, level_of_indent, all_cols))
Hao Zhu8f417202017-05-20 16:37:14 -040035 }
36}
37
Hao Zhu62cdde52017-05-20 22:16:03 -040038# Add indentation for LaTeX
Hao Zhu9410a272020-08-03 01:11:47 -040039add_indent_latex <- function(kable_input, positions,
40 level_of_indent = 1, all_cols = FALSE) {
Hao Zhud972e7f2017-05-22 13:27:15 -040041 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -040042 out <- solve_enc(kable_input)
Samia67d70572020-07-06 00:54:13 -040043 level_of_indent<-as.numeric(level_of_indent)
Hao Zhu064990d2017-10-17 18:08:42 -040044
45 if (table_info$duplicated_rows) {
46 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
47 out <- dup_fx_out[[1]]
48 table_info <- dup_fx_out[[2]]
49 }
Hao Zhu8f417202017-05-20 16:37:14 -040050
Hao Zhu37dbe3f2018-05-14 11:16:06 -040051 max_position <- table_info$nrow - table_info$position_offset
Leo83f05132018-05-10 14:12:16 +080052
53 if (max(positions) > max_position) {
Hao Zhu8f417202017-05-20 16:37:14 -040054 stop("There aren't that many rows in the table. Check positions in ",
55 "add_indent_latex.")
56 }
57
Hao Zhu37dbe3f2018-05-14 11:16:06 -040058 for (i in positions + table_info$position_offset) {
Leo83f05132018-05-10 14:12:16 +080059 rowtext <- table_info$contents[i]
Hao Zhu9410a272020-08-03 01:11:47 -040060 if (all_cols) {
61 new_rowtext <- latex_row_cells(rowtext)
62 new_rowtext <- lapply(new_rowtext, function(x) {
63 paste0("\\\\hspace\\{", level_of_indent ,"em\\}", x)
64 })
65 new_rowtext <- paste(unlist(new_rowtext), collapse = " & ")
66 } else {
67 new_rowtext <- latex_indent_unit(rowtext, level_of_indent)
68 }
Thomas Jemmettec8d5662018-12-06 09:03:11 +000069 out <- sub(paste0(rowtext, "(\\\\\\\\\\*?(\\[.*\\])?\n)"),
Hao Zhufce64692020-08-18 23:39:27 -040070 paste0(new_rowtext, "\\1"),
Quôc Peyroted7e6242018-09-05 14:59:16 +020071 out, perl = TRUE)
Hao Zhu9410a272020-08-03 01:11:47 -040072 table_info$contents[i] <- new_rowtext
Hao Zhu8f417202017-05-20 16:37:14 -040073 }
Hao Zhu2ce42b92017-06-15 17:15:33 -040074 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -040075 attr(out, "kable_meta") <- table_info
Hao Zhu8f417202017-05-20 16:37:14 -040076 return(out)
Samia6c4b24f2020-07-06 00:46:41 -040077
78
Hao Zhu8f417202017-05-20 16:37:14 -040079}
80
Hao Zhu9410a272020-08-03 01:11:47 -040081latex_indent_unit <- function(rowtext, level_of_indent) {
82 paste0("\\\\hspace\\{", level_of_indent ,"em\\}", rowtext)
Hao Zhu8f417202017-05-20 16:37:14 -040083}
Hao Zhu62cdde52017-05-20 22:16:03 -040084
Samia6c4b24f2020-07-06 00:46:41 -040085
86
Hao Zhu62cdde52017-05-20 22:16:03 -040087# Add indentation for HTML
Hao Zhu9410a272020-08-03 01:11:47 -040088add_indent_html <- function(kable_input, positions,
89 level_of_indent = 1, all_cols = FALSE) {
Hao Zhu62cdde52017-05-20 22:16:03 -040090 kable_attrs <- attributes(kable_input)
91
Hao Zhu9410a272020-08-03 01:11:47 -040092 kable_xml <- kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -040093 kable_tbody <- xml_tpart(kable_xml, "tbody")
94
95 group_header_rows <- attr(kable_input, "group_header_rows")
96 if (!is.null(group_header_rows)) {
97 positions <- positions_corrector(positions, group_header_rows,
98 length(xml_children(kable_tbody)))
99 }
Alan Butlerc81c1fe2018-01-30 11:16:18 -0700100
Hao Zhu62cdde52017-05-20 22:16:03 -0400101 for (i in positions) {
Hao Zhu9410a272020-08-03 01:11:47 -0400102 row_to_edit = xml_children(kable_tbody)[[i]]
103 if (all_cols) {
104 target_cols = 1:xml2::xml_length(row_to_edit)
Hao Zhu62cdde52017-05-20 22:16:03 -0400105 } else {
Hao Zhu9410a272020-08-03 01:11:47 -0400106 target_cols = 1
107 }
108
109 for (j in target_cols) {
Hao Zhu9410a272020-08-03 01:11:47 -0400110 node_to_edit <- xml_child(row_to_edit, j)
111 if (!xml_has_attr(node_to_edit, "indentlevel")) {
112 xml_attr(node_to_edit, "style") <- paste(
113 xml_attr(node_to_edit, "style"), "padding-left: ",paste0(level_of_indent*2,"em;")
114 )
115 xml_attr(node_to_edit, "indentlevel") <- 1
116 } else {
117 indentLevel <- as.numeric(xml_attr(node_to_edit, "indentlevel"))
118 xml_attr(node_to_edit, "style") <- sub(
119 paste0("padding-left: ", indentLevel * 2, "em;"),
120 paste0("padding-left: ", (indentLevel + 1) * 2, "em;"),
121 xml_attr(node_to_edit, "style")
122 )
123 xml_attr(node_to_edit, "indentlevel") <- indentLevel + 1
124 }
Hao Zhu62cdde52017-05-20 22:16:03 -0400125 }
126 }
Hao Zhuf2dfd142017-07-24 14:43:28 -0400127 out <- as_kable_xml(kable_xml)
Hao Zhu62cdde52017-05-20 22:16:03 -0400128 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500129 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu62cdde52017-05-20 22:16:03 -0400130 return(out)
131}