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