blob: 8a90c49f5b5775cf57f4ded491cc4dbb769c498b [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 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
Hao Zhu9410a272020-08-03 01:11:47 -040013add_indent <- function(kable_input, positions,
14 level_of_indent = 1, all_cols = FALSE) {
Samia106ad582020-07-15 14:19:09 -040015
Hao Zhud972e7f2017-05-22 13:27:15 -040016 if (!is.numeric(positions)) {
17 stop("Positions can only take numeric row numbers (excluding header rows).")
18 }
Hao Zhu8f417202017-05-20 16:37:14 -040019 kable_format <- attr(kable_input, "format")
20 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050021 warning("Please specify format in kable. kableExtra can customize either ",
22 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
23 "for details.")
Hao Zhu8f417202017-05-20 16:37:14 -040024 return(kable_input)
25 }
26 if (kable_format == "html") {
Hao Zhu9410a272020-08-03 01:11:47 -040027 return(add_indent_html(kable_input, positions, level_of_indent, all_cols))
Hao Zhu8f417202017-05-20 16:37:14 -040028 }
29 if (kable_format == "latex") {
Hao Zhu9410a272020-08-03 01:11:47 -040030 return(add_indent_latex(kable_input, positions, level_of_indent, all_cols))
Hao Zhu8f417202017-05-20 16:37:14 -040031 }
32}
33
Hao Zhu62cdde52017-05-20 22:16:03 -040034# Add indentation for LaTeX
Hao Zhu9410a272020-08-03 01:11:47 -040035add_indent_latex <- function(kable_input, positions,
36 level_of_indent = 1, all_cols = FALSE) {
Hao Zhud972e7f2017-05-22 13:27:15 -040037 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -040038 out <- solve_enc(kable_input)
Samia67d70572020-07-06 00:54:13 -040039 level_of_indent<-as.numeric(level_of_indent)
Samia6c4b24f2020-07-06 00:46:41 -040040
Hao Zhu064990d2017-10-17 18:08:42 -040041 if (table_info$duplicated_rows) {
42 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
43 out <- dup_fx_out[[1]]
44 table_info <- dup_fx_out[[2]]
45 }
Hao Zhu8f417202017-05-20 16:37:14 -040046
Hao Zhu37dbe3f2018-05-14 11:16:06 -040047 max_position <- table_info$nrow - table_info$position_offset
Leo83f05132018-05-10 14:12:16 +080048
49 if (max(positions) > max_position) {
Hao Zhu8f417202017-05-20 16:37:14 -040050 stop("There aren't that many rows in the table. Check positions in ",
51 "add_indent_latex.")
52 }
53
Hao Zhu37dbe3f2018-05-14 11:16:06 -040054 for (i in positions + table_info$position_offset) {
Leo83f05132018-05-10 14:12:16 +080055 rowtext <- table_info$contents[i]
Hao Zhu9410a272020-08-03 01:11:47 -040056 if (all_cols) {
57 new_rowtext <- latex_row_cells(rowtext)
58 new_rowtext <- lapply(new_rowtext, function(x) {
59 paste0("\\\\hspace\\{", level_of_indent ,"em\\}", x)
60 })
61 new_rowtext <- paste(unlist(new_rowtext), collapse = " & ")
62 } else {
63 new_rowtext <- latex_indent_unit(rowtext, level_of_indent)
64 }
Hao Zhu09d01262019-01-07 22:06:22 -050065 out <- sub(paste0(rowtext, "\\\\\\\\"),
Hao Zhu9410a272020-08-03 01:11:47 -040066 paste0(new_rowtext, "\\\\\\\\"),
Quôc Peyroted7e6242018-09-05 14:59:16 +020067 out, perl = TRUE)
Hao Zhu9410a272020-08-03 01:11:47 -040068 table_info$contents[i] <- new_rowtext
Hao Zhu8f417202017-05-20 16:37:14 -040069 }
Hao Zhu2ce42b92017-06-15 17:15:33 -040070 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -040071 attr(out, "kable_meta") <- table_info
Hao Zhu8f417202017-05-20 16:37:14 -040072 return(out)
Samia6c4b24f2020-07-06 00:46:41 -040073
74
Hao Zhu8f417202017-05-20 16:37:14 -040075}
76
Hao Zhu9410a272020-08-03 01:11:47 -040077latex_indent_unit <- function(rowtext, level_of_indent) {
78 paste0("\\\\hspace\\{", level_of_indent ,"em\\}", rowtext)
Hao Zhu8f417202017-05-20 16:37:14 -040079}
Hao Zhu62cdde52017-05-20 22:16:03 -040080
Samia6c4b24f2020-07-06 00:46:41 -040081
82
Hao Zhu62cdde52017-05-20 22:16:03 -040083# Add indentation for HTML
Hao Zhu9410a272020-08-03 01:11:47 -040084add_indent_html <- function(kable_input, positions,
85 level_of_indent = 1, all_cols = FALSE) {
Hao Zhu62cdde52017-05-20 22:16:03 -040086 kable_attrs <- attributes(kable_input)
87
Hao Zhu9410a272020-08-03 01:11:47 -040088 kable_xml <- kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -040089 kable_tbody <- xml_tpart(kable_xml, "tbody")
90
91 group_header_rows <- attr(kable_input, "group_header_rows")
92 if (!is.null(group_header_rows)) {
93 positions <- positions_corrector(positions, group_header_rows,
94 length(xml_children(kable_tbody)))
95 }
Alan Butlerc81c1fe2018-01-30 11:16:18 -070096
Hao Zhu62cdde52017-05-20 22:16:03 -040097 for (i in positions) {
Hao Zhu9410a272020-08-03 01:11:47 -040098 row_to_edit = xml_children(kable_tbody)[[i]]
99 if (all_cols) {
100 target_cols = 1:xml2::xml_length(row_to_edit)
Hao Zhu62cdde52017-05-20 22:16:03 -0400101 } else {
Hao Zhu9410a272020-08-03 01:11:47 -0400102 target_cols = 1
103 }
104
105 for (j in target_cols) {
106 print(j)
107 node_to_edit <- xml_child(row_to_edit, j)
108 if (!xml_has_attr(node_to_edit, "indentlevel")) {
109 xml_attr(node_to_edit, "style") <- paste(
110 xml_attr(node_to_edit, "style"), "padding-left: ",paste0(level_of_indent*2,"em;")
111 )
112 xml_attr(node_to_edit, "indentlevel") <- 1
113 } else {
114 indentLevel <- as.numeric(xml_attr(node_to_edit, "indentlevel"))
115 xml_attr(node_to_edit, "style") <- sub(
116 paste0("padding-left: ", indentLevel * 2, "em;"),
117 paste0("padding-left: ", (indentLevel + 1) * 2, "em;"),
118 xml_attr(node_to_edit, "style")
119 )
120 xml_attr(node_to_edit, "indentlevel") <- indentLevel + 1
121 }
Hao Zhu62cdde52017-05-20 22:16:03 -0400122 }
123 }
Hao Zhuf2dfd142017-07-24 14:43:28 -0400124 out <- as_kable_xml(kable_xml)
Hao Zhu62cdde52017-05-20 22:16:03 -0400125 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500126 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu62cdde52017-05-20 22:16:03 -0400127 return(out)
128}