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")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame^] | 18 | 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 Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 21 | return(kable_input) |
| 22 | } |
| 23 | if (kable_format == "html") { |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 24 | return(add_indent_html(kable_input, positions)) |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 25 | } |
| 26 | if (kable_format == "latex") { |
| 27 | return(add_indent_latex(kable_input, positions)) |
| 28 | } |
| 29 | } |
| 30 | |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 31 | # Add indentation for LaTeX |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 32 | add_indent_latex <- function(kable_input, positions) { |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 33 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 34 | out <- enc2utf8(as.character(kable_input)) |
| 35 | |
| 36 | if (table_info$duplicated_rows) { |
| 37 | dup_fx_out <- fix_duplicated_rows_latex(out, table_info) |
| 38 | out <- dup_fx_out[[1]] |
| 39 | table_info <- dup_fx_out[[2]] |
| 40 | } |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 41 | |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 42 | if (max(positions) > table_info$nrow - 1) { |
| 43 | stop("There aren't that many rows in the table. Check positions in ", |
| 44 | "add_indent_latex.") |
| 45 | } |
| 46 | |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 47 | for (i in positions) { |
| 48 | rowtext <- table_info$contents[i + 1] |
Hao Zhu | e5bfbf1 | 2017-06-06 00:24:49 -0400 | [diff] [blame] | 49 | out <- sub(rowtext, latex_indent_unit(rowtext), out, perl = TRUE) |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 50 | table_info$contents[i + 1] <- latex_indent_unit(rowtext) |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 51 | } |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 52 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 53 | attr(out, "kable_meta") <- table_info |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 54 | return(out) |
| 55 | } |
| 56 | |
| 57 | latex_indent_unit <- function(rowtext) { |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 58 | paste0("\\\\hspace\\{1em\\}", rowtext) |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 59 | } |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 60 | |
| 61 | # Add indentation for HTML |
| 62 | add_indent_html <- function(kable_input, positions) { |
| 63 | kable_attrs <- attributes(kable_input) |
| 64 | |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 65 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 66 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 67 | |
| 68 | group_header_rows <- attr(kable_input, "group_header_rows") |
| 69 | if (!is.null(group_header_rows)) { |
| 70 | positions <- positions_corrector(positions, group_header_rows, |
| 71 | length(xml_children(kable_tbody))) |
| 72 | } |
| 73 | for (i in positions) { |
| 74 | node_to_edit <- xml_child(xml_children(kable_tbody)[[i]], 1) |
| 75 | if (!xml_has_attr(node_to_edit, "indentLevel")) { |
| 76 | xml_attr(node_to_edit, "style") <- paste( |
| 77 | xml_attr(node_to_edit, "style"), "padding-left: 2em;" |
| 78 | ) |
| 79 | xml_attr(node_to_edit, "indentLevel") <- 1 |
| 80 | } else { |
| 81 | indentLevel <- as.numeric(xml_attr(node_to_edit, "indentLevel")) |
| 82 | xml_attr(node_to_edit, "style") <- sub( |
| 83 | paste0("padding-left: ", indentLevel * 2, "em;"), |
| 84 | paste0("padding-left: ", (indentLevel + 1) * 2, "em;"), |
| 85 | xml_attr(node_to_edit, "style") |
| 86 | ) |
| 87 | xml_attr(node_to_edit, "indentLevel") <- indentLevel + 1 |
| 88 | } |
| 89 | } |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 90 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 91 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 92 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 93 | return(out) |
| 94 | } |