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. |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 6 | #' @param level_of_indent a numeric value for the indent level. Default is 1. |
Hao Zhu | 8f46db8 | 2020-08-18 21:48:23 -0400 | [diff] [blame] | 7 | #' @param all_cols T/F whether to apply indentation to all columns |
Damian Thomas | f36f960 | 2021-04-28 09:59:51 -0400 | [diff] [blame^] | 8 | #' @param target_cols A vector of numeric column positions. Default is 1. |
Hao Zhu | bd95bb2 | 2017-05-22 16:08:49 -0400 | [diff] [blame] | 9 | #' |
Hao Zhu | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame] | 10 | #' @examples |
| 11 | #' \dontrun{ |
| 12 | #' x <- knitr::kable(head(mtcars), "html") |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 13 | #' # Add indentations to the 2nd & 4th row |
Samia | 67d7057 | 2020-07-06 00:54:13 -0400 | [diff] [blame] | 14 | #' add_indent(x, c(2, 4), level_of_indent = 1) |
Hao Zhu | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame] | 15 | #' } |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 16 | #' |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 17 | #' @export |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 18 | add_indent <- function(kable_input, positions, |
Damian Thomas | f36f960 | 2021-04-28 09:59:51 -0400 | [diff] [blame^] | 19 | level_of_indent = 1, all_cols = FALSE, |
| 20 | target_cols = 1) { |
Samia | 106ad58 | 2020-07-15 14:19:09 -0400 | [diff] [blame] | 21 | |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 22 | if (!is.numeric(positions)) { |
| 23 | stop("Positions can only take numeric row numbers (excluding header rows).") |
| 24 | } |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 25 | kable_format <- attr(kable_input, "format") |
| 26 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 27 | warning("Please specify format in kable. kableExtra can customize either ", |
| 28 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 29 | "for details.") |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 30 | return(kable_input) |
| 31 | } |
| 32 | if (kable_format == "html") { |
Damian Thomas | f36f960 | 2021-04-28 09:59:51 -0400 | [diff] [blame^] | 33 | return(add_indent_html(kable_input, positions, level_of_indent, all_cols, target_cols)) |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 34 | } |
| 35 | if (kable_format == "latex") { |
Damian Thomas | f36f960 | 2021-04-28 09:59:51 -0400 | [diff] [blame^] | 36 | return(add_indent_latex(kable_input, positions, level_of_indent, all_cols, target_cols)) |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 40 | # Add indentation for LaTeX |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 41 | add_indent_latex <- function(kable_input, positions, |
Damian Thomas | f36f960 | 2021-04-28 09:59:51 -0400 | [diff] [blame^] | 42 | level_of_indent = 1, all_cols = FALSE, |
| 43 | target_cols = 1) { |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 44 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 45 | out <- solve_enc(kable_input) |
Samia | 67d7057 | 2020-07-06 00:54:13 -0400 | [diff] [blame] | 46 | level_of_indent<-as.numeric(level_of_indent) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 47 | |
| 48 | if (table_info$duplicated_rows) { |
| 49 | dup_fx_out <- fix_duplicated_rows_latex(out, table_info) |
| 50 | out <- dup_fx_out[[1]] |
| 51 | table_info <- dup_fx_out[[2]] |
| 52 | } |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 53 | |
Hao Zhu | 37dbe3f | 2018-05-14 11:16:06 -0400 | [diff] [blame] | 54 | max_position <- table_info$nrow - table_info$position_offset |
Leo | 83f0513 | 2018-05-10 14:12:16 +0800 | [diff] [blame] | 55 | |
| 56 | if (max(positions) > max_position) { |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 57 | stop("There aren't that many rows in the table. Check positions in ", |
| 58 | "add_indent_latex.") |
| 59 | } |
| 60 | |
Hao Zhu | 37dbe3f | 2018-05-14 11:16:06 -0400 | [diff] [blame] | 61 | for (i in positions + table_info$position_offset) { |
Leo | 83f0513 | 2018-05-10 14:12:16 +0800 | [diff] [blame] | 62 | rowtext <- table_info$contents[i] |
Damian Thomas | f36f960 | 2021-04-28 09:59:51 -0400 | [diff] [blame^] | 63 | new_rowtext <- unlist(latex_row_cells(rowtext)) |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 64 | if (all_cols) { |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 65 | new_rowtext <- lapply(new_rowtext, function(x) { |
| 66 | paste0("\\\\hspace\\{", level_of_indent ,"em\\}", x) |
| 67 | }) |
| 68 | new_rowtext <- paste(unlist(new_rowtext), collapse = " & ") |
| 69 | } else { |
Damian Thomas | f36f960 | 2021-04-28 09:59:51 -0400 | [diff] [blame^] | 70 | if (all(target_cols %in% seq_along(new_rowtext))) { |
| 71 | new_rowtext[target_cols] <- latex_indent_unit(new_rowtext[target_cols], level_of_indent) |
| 72 | } else { |
| 73 | stop("There aren't that many columns in the row. Check target_cols in ", |
| 74 | "add_indent_latex.") |
| 75 | } |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 76 | } |
Damian Thomas | f36f960 | 2021-04-28 09:59:51 -0400 | [diff] [blame^] | 77 | new_rowtext <- paste(unlist(new_rowtext), collapse = " & ") |
| 78 | out <- sub(rowtext, new_rowtext, out, perl = TRUE) |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 79 | table_info$contents[i] <- new_rowtext |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 80 | } |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 81 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 82 | attr(out, "kable_meta") <- table_info |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 83 | return(out) |
Samia | 6c4b24f | 2020-07-06 00:46:41 -0400 | [diff] [blame] | 84 | |
| 85 | |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 88 | latex_indent_unit <- function(rowtext, level_of_indent) { |
| 89 | paste0("\\\\hspace\\{", level_of_indent ,"em\\}", rowtext) |
Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame] | 90 | } |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 91 | |
Samia | 6c4b24f | 2020-07-06 00:46:41 -0400 | [diff] [blame] | 92 | |
| 93 | |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 94 | # Add indentation for HTML |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 95 | add_indent_html <- function(kable_input, positions, |
Damian Thomas | f36f960 | 2021-04-28 09:59:51 -0400 | [diff] [blame^] | 96 | level_of_indent = 1, all_cols = FALSE, |
| 97 | target_cols = 1) { |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 98 | kable_attrs <- attributes(kable_input) |
| 99 | |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 100 | kable_xml <- kable_as_xml(kable_input) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 101 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 102 | |
| 103 | group_header_rows <- attr(kable_input, "group_header_rows") |
| 104 | if (!is.null(group_header_rows)) { |
| 105 | positions <- positions_corrector(positions, group_header_rows, |
| 106 | length(xml_children(kable_tbody))) |
| 107 | } |
Alan Butler | c81c1fe | 2018-01-30 11:16:18 -0700 | [diff] [blame] | 108 | |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 109 | for (i in positions) { |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 110 | row_to_edit = xml_children(kable_tbody)[[i]] |
| 111 | if (all_cols) { |
| 112 | target_cols = 1:xml2::xml_length(row_to_edit) |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | for (j in target_cols) { |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 116 | node_to_edit <- xml_child(row_to_edit, j) |
| 117 | if (!xml_has_attr(node_to_edit, "indentlevel")) { |
Hao Zhu | 594057b | 2021-01-19 00:50:50 -0500 | [diff] [blame] | 118 | xml_attr(node_to_edit, "style") <- paste0( |
Hao Zhu | 9410a27 | 2020-08-03 01:11:47 -0400 | [diff] [blame] | 119 | xml_attr(node_to_edit, "style"), "padding-left: ",paste0(level_of_indent*2,"em;") |
| 120 | ) |
| 121 | xml_attr(node_to_edit, "indentlevel") <- 1 |
| 122 | } else { |
| 123 | indentLevel <- as.numeric(xml_attr(node_to_edit, "indentlevel")) |
| 124 | xml_attr(node_to_edit, "style") <- sub( |
| 125 | paste0("padding-left: ", indentLevel * 2, "em;"), |
| 126 | paste0("padding-left: ", (indentLevel + 1) * 2, "em;"), |
| 127 | xml_attr(node_to_edit, "style") |
| 128 | ) |
| 129 | xml_attr(node_to_edit, "indentlevel") <- indentLevel + 1 |
| 130 | } |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 131 | } |
| 132 | } |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 133 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 134 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 135 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 136 | return(out) |
| 137 | } |