Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 1 | #' Put a few rows of a table into one category |
| 2 | #' |
| 3 | #' @export |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame^] | 4 | group_rows <- function(kable_input, group_label, start_row, end_row, |
| 5 | label_row_css = "border-bottom: 1px solid;", |
| 6 | latex_gap_space = "0.5em") { |
| 7 | if (!is.numeric(c(start_row, end_row))) { |
| 8 | stop("Start_row and end_row must be numeric position of rows (excluding", |
| 9 | "header rows and other group-title rows). ") |
| 10 | } |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 11 | kable_format <- attr(kable_input, "format") |
| 12 | if (!kable_format %in% c("html", "latex")) { |
| 13 | message("Currently generic markdown table using pandoc is not supported.") |
| 14 | return(kable_input) |
| 15 | } |
| 16 | if (kable_format == "html") { |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame^] | 17 | return(group_rows_html(kable_input, group_label, start_row, end_row, |
| 18 | label_row_css)) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 19 | } |
| 20 | if (kable_format == "latex") { |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame^] | 21 | return(group_rows_latex(kable_input, group_label, start_row, end_row, |
| 22 | latex_gap_space)) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 23 | } |
| 24 | } |
| 25 | |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame^] | 26 | group_rows_html <- function(kable_input, group_label, start_row, end_row, |
| 27 | label_row_css) { |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 28 | kable_attrs <- attributes(kable_input) |
| 29 | kable_xml <- read_xml(as.character(kable_input), options = "COMPACT") |
| 30 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 31 | |
| 32 | group_header_rows <- attr(kable_input, "group_header_rows") |
| 33 | group_seq <- seq(start_row, end_row) |
| 34 | if (!is.null(group_header_rows)) { |
| 35 | group_seq <- positions_corrector(group_seq, group_header_rows, |
| 36 | length(xml_children(kable_tbody))) |
| 37 | } |
| 38 | |
| 39 | # Insert a group header row |
| 40 | starting_node <- xml_child(kable_tbody, group_seq[1]) |
| 41 | kable_ncol <- length(xml_children(starting_node)) |
| 42 | group_header_row_text <- paste0( |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame^] | 43 | '<tr groupLength="', length(group_seq), '"><td colspan="', kable_ncol, |
| 44 | '" style="', label_row_css, '"><strong>', group_label, |
| 45 | "</strong></td></tr>" |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 46 | ) |
| 47 | group_header_row <- read_xml(group_header_row_text, options = "COMPACT") |
| 48 | xml_add_sibling(starting_node, group_header_row, .where = "before") |
| 49 | |
| 50 | # add indentations to items |
| 51 | out <- structure(as.character(kable_xml), format = "html", |
| 52 | class = "knitr_kable") |
| 53 | attributes(out) <- kable_attrs |
| 54 | attr(out, "group_header_rows") <- c(attr(out, "group_header_rows"), group_seq[1]) |
| 55 | out <- add_indent(out, positions = seq(start_row, end_row)) |
| 56 | return(out) |
| 57 | } |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame^] | 58 | |
| 59 | group_rows_latex <- function(kable_input, group_label, start_row, end_row, gap_space) { |
| 60 | table_info <- magic_mirror(kable_input) |
| 61 | out <- kable_input |
| 62 | |
| 63 | # Add group label |
| 64 | rowtext <- table_info$contents[start_row + 1] |
| 65 | if (table_info$booktabs) { |
| 66 | new_rowtext <- paste0( |
| 67 | "\\\\addlinespace[", gap_space, "]\n", |
| 68 | "\\\\multicolumn{", table_info$ncol, "}{l}{\\\\textbf{", group_label, |
| 69 | "}}\\\\\\\\\n", |
| 70 | rowtext |
| 71 | ) |
| 72 | # last_row <- paste0(table_info$contents[end_row + 1], "\\\\\\\\") |
| 73 | # out <- sub( |
| 74 | # last_row, |
| 75 | # paste0(last_row, "\n\\\\addlinespace[", gap_space, "]"), |
| 76 | # out |
| 77 | # ) |
| 78 | } else { |
| 79 | rowtext <- paste0("\\\\hline\n", rowtext) |
| 80 | new_rowtext <- paste0( |
| 81 | "\\\\hline\n\\\\multicolumn{", table_info$ncol, "}{l}{\\\\textbf{", |
| 82 | group_label, "}}\\\\\\\\\n", rowtext |
| 83 | ) |
| 84 | } |
| 85 | out <- sub(rowtext, new_rowtext, out) |
| 86 | if (table_info$booktabs) |
| 87 | out <- add_indent(out, seq(start_row, end_row)) |
| 88 | return(out) |
| 89 | } |