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