Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 1 | #' Add indentations to row headers |
Hao Zhu | bd95bb2 | 2017-05-22 16:08:49 -0400 | [diff] [blame] | 2 | #' |
| 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 Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 7 | #' @examples x <- knitr::kable(head(mtcars), "html") |
| 8 | #' # Add indentations to the 2nd & 4th row |
| 9 | #' add_indent(x, c(2, 4)) |
| 10 | #' |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 11 | #' @export |
| 12 | add_indent <- function(kable_input, positions) { |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 13 | if (!is.numeric(positions)) { |
| 14 | stop("Positions can only take numeric row numbers (excluding header rows).") |
| 15 | } |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 16 | 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 Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 22 | return(add_indent_html(kable_input, positions)) |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 23 | } |
| 24 | if (kable_format == "latex") { |
| 25 | return(add_indent_latex(kable_input, positions)) |
| 26 | } |
| 27 | } |
| 28 | |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 29 | # Add indentation for LaTeX |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 30 | add_indent_latex <- function(kable_input, positions) { |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 31 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 32 | |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 33 | if (max(positions) > table_info$nrow - 1) { |
| 34 | stop("There aren't that many rows in the table. Check positions in ", |
| 35 | "add_indent_latex.") |
| 36 | } |
| 37 | |
| 38 | out <- kable_input |
| 39 | for (i in positions) { |
| 40 | rowtext <- table_info$contents[i + 1] |
Hao Zhu | e5bfbf1 | 2017-06-06 00:24:49 -0400 | [diff] [blame] | 41 | out <- sub(rowtext, latex_indent_unit(rowtext), out, perl = TRUE) |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 42 | } |
| 43 | return(out) |
| 44 | } |
| 45 | |
| 46 | latex_indent_unit <- function(rowtext) { |
| 47 | paste0("\\\\hspace{1em}", rowtext) |
| 48 | } |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 49 | |
| 50 | # Add indentation for HTML |
| 51 | add_indent_html <- function(kable_input, positions) { |
| 52 | kable_attrs <- attributes(kable_input) |
| 53 | |
| 54 | kable_xml <- read_xml(as.character(kable_input), options = "COMPACT") |
| 55 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 56 | |
| 57 | group_header_rows <- attr(kable_input, "group_header_rows") |
| 58 | if (!is.null(group_header_rows)) { |
| 59 | positions <- positions_corrector(positions, group_header_rows, |
| 60 | length(xml_children(kable_tbody))) |
| 61 | } |
| 62 | for (i in positions) { |
| 63 | node_to_edit <- xml_child(xml_children(kable_tbody)[[i]], 1) |
| 64 | if (!xml_has_attr(node_to_edit, "indentLevel")) { |
| 65 | xml_attr(node_to_edit, "style") <- paste( |
| 66 | xml_attr(node_to_edit, "style"), "padding-left: 2em;" |
| 67 | ) |
| 68 | xml_attr(node_to_edit, "indentLevel") <- 1 |
| 69 | } else { |
| 70 | indentLevel <- as.numeric(xml_attr(node_to_edit, "indentLevel")) |
| 71 | xml_attr(node_to_edit, "style") <- sub( |
| 72 | paste0("padding-left: ", indentLevel * 2, "em;"), |
| 73 | paste0("padding-left: ", (indentLevel + 1) * 2, "em;"), |
| 74 | xml_attr(node_to_edit, "style") |
| 75 | ) |
| 76 | xml_attr(node_to_edit, "indentLevel") <- indentLevel + 1 |
| 77 | } |
| 78 | } |
| 79 | out <- structure(as.character(kable_xml), format = "html", |
| 80 | class = "knitr_kable") |
| 81 | attributes(out) <- kable_attrs |
| 82 | return(out) |
| 83 | } |