blob: 8d79160825ab3306126430061bf90a1d0bd7687d [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
Samia5ca27462020-07-05 16:50:27 -040012add_indent <- function(kable_input, positions, no_of_indent) {
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")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050018 warning("Please specify format in kable. kableExtra can customize either ",
19 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
20 "for details.")
Hao Zhu8f417202017-05-20 16:37:14 -040021 return(kable_input)
22 }
23 if (kable_format == "html") {
Samia5ca27462020-07-05 16:50:27 -040024 return(add_indent_html(kable_input, positions, no_of_indent))
Hao Zhu8f417202017-05-20 16:37:14 -040025 }
26 if (kable_format == "latex") {
Samia5ca27462020-07-05 16:50:27 -040027 return(add_indent_latex(kable_input, positions, no_of_indent))
Hao Zhu8f417202017-05-20 16:37:14 -040028 }
29}
30
Hao Zhu62cdde52017-05-20 22:16:03 -040031# Add indentation for LaTeX
Samia5ca27462020-07-05 16:50:27 -040032add_indent_latex <- function(kable_input, positions, no_of_indent) {
Hao Zhud972e7f2017-05-22 13:27:15 -040033 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -040034 out <- solve_enc(kable_input)
Samia6c4b24f2020-07-06 00:46:41 -040035 no_of_indent<-as.numeric(no_of_indent)
36
Hao Zhu064990d2017-10-17 18:08:42 -040037
38 if (table_info$duplicated_rows) {
39 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
40 out <- dup_fx_out[[1]]
41 table_info <- dup_fx_out[[2]]
42 }
Hao Zhu8f417202017-05-20 16:37:14 -040043
Hao Zhu37dbe3f2018-05-14 11:16:06 -040044 max_position <- table_info$nrow - table_info$position_offset
Leo83f05132018-05-10 14:12:16 +080045
46 if (max(positions) > max_position) {
Hao Zhu8f417202017-05-20 16:37:14 -040047 stop("There aren't that many rows in the table. Check positions in ",
48 "add_indent_latex.")
49 }
50
Hao Zhu37dbe3f2018-05-14 11:16:06 -040051 for (i in positions + table_info$position_offset) {
Leo83f05132018-05-10 14:12:16 +080052 rowtext <- table_info$contents[i]
Hao Zhu09d01262019-01-07 22:06:22 -050053 out <- sub(paste0(rowtext, "\\\\\\\\"),
54 paste0(latex_indent_unit(rowtext), "\\\\\\\\"),
Quôc Peyroted7e6242018-09-05 14:59:16 +020055 out, perl = TRUE)
Leo83f05132018-05-10 14:12:16 +080056 table_info$contents[i] <- latex_indent_unit(rowtext)
Hao Zhu8f417202017-05-20 16:37:14 -040057 }
Hao Zhu2ce42b92017-06-15 17:15:33 -040058 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -040059 attr(out, "kable_meta") <- table_info
Hao Zhu8f417202017-05-20 16:37:14 -040060 return(out)
Samia6c4b24f2020-07-06 00:46:41 -040061
62
Hao Zhu8f417202017-05-20 16:37:14 -040063}
64
65latex_indent_unit <- function(rowtext) {
Samia00095d42020-07-05 19:05:11 -040066 paste0("\\\\hspace\\{",no_of_indent,"em\\}", rowtext)
Hao Zhu8f417202017-05-20 16:37:14 -040067}
Hao Zhu62cdde52017-05-20 22:16:03 -040068
Samia6c4b24f2020-07-06 00:46:41 -040069
70
Hao Zhu62cdde52017-05-20 22:16:03 -040071# Add indentation for HTML
Samia5ca27462020-07-05 16:50:27 -040072add_indent_html <- function(kable_input, positions, no_of_indent) {
Hao Zhu62cdde52017-05-20 22:16:03 -040073 kable_attrs <- attributes(kable_input)
74
Hao Zhu558c72f2017-07-24 15:12:00 -040075 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -040076 kable_tbody <- xml_tpart(kable_xml, "tbody")
77
78 group_header_rows <- attr(kable_input, "group_header_rows")
79 if (!is.null(group_header_rows)) {
80 positions <- positions_corrector(positions, group_header_rows,
81 length(xml_children(kable_tbody)))
82 }
Alan Butlerc81c1fe2018-01-30 11:16:18 -070083
Hao Zhu62cdde52017-05-20 22:16:03 -040084 for (i in positions) {
85 node_to_edit <- xml_child(xml_children(kable_tbody)[[i]], 1)
Alan Butlerc81c1fe2018-01-30 11:16:18 -070086 if (!xml_has_attr(node_to_edit, "indentlevel")) {
Hao Zhu62cdde52017-05-20 22:16:03 -040087 xml_attr(node_to_edit, "style") <- paste(
Samia5ca27462020-07-05 16:50:27 -040088 xml_attr(node_to_edit, "style"), "padding-left: ",paste0(no_of_indent*2,"em;")
Hao Zhu62cdde52017-05-20 22:16:03 -040089 )
Alan Butlerc81c1fe2018-01-30 11:16:18 -070090 xml_attr(node_to_edit, "indentlevel") <- 1
Hao Zhu62cdde52017-05-20 22:16:03 -040091 } else {
Alan Butlerc81c1fe2018-01-30 11:16:18 -070092 indentLevel <- as.numeric(xml_attr(node_to_edit, "indentlevel"))
Hao Zhu62cdde52017-05-20 22:16:03 -040093 xml_attr(node_to_edit, "style") <- sub(
94 paste0("padding-left: ", indentLevel * 2, "em;"),
95 paste0("padding-left: ", (indentLevel + 1) * 2, "em;"),
96 xml_attr(node_to_edit, "style")
97 )
Alan Butlerc81c1fe2018-01-30 11:16:18 -070098 xml_attr(node_to_edit, "indentlevel") <- indentLevel + 1
Hao Zhu62cdde52017-05-20 22:16:03 -040099 }
100 }
Hao Zhuf2dfd142017-07-24 14:43:28 -0400101 out <- as_kable_xml(kable_xml)
Hao Zhu62cdde52017-05-20 22:16:03 -0400102 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500103 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu62cdde52017-05-20 22:16:03 -0400104 return(out)
105}