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