| #' Collapse repeat rows to multirow cell |
| #' |
| #' @description Experimenting. Don't use it in production. |
| collapse_rows <- function(kable_input, columns) { |
| if (is.null(columns)) { |
| stop("Please specify numeric positions of columns you want to collapse.") |
| } |
| kable_format <- attr(kable_input, "format") |
| if (!kable_format %in% c("html", "latex")) { |
| message("Currently generic markdown table using pandoc is not supported.") |
| return(kable_input) |
| } |
| if (kable_format == "html") { |
| return(collapse_rows_html(kable_input, columns)) |
| } |
| if (kable_format == "latex") { |
| return(collapse_rows_latex(kable_input, columns)) |
| } |
| } |
| |
| collapse_rows_html <- function(kable_input, columns) { |
| kable_attrs <- attributes(kable_input) |
| kable_xml <- read_xml(as.character(kable_input), options = "COMPACT") |
| kable_tbody <- xml_tpart(kable_xml, "tbody") |
| |
| kable_dt <- rvest::html_table(xml2::read_html(as.character(kable_input)))[[1]] |
| kable_dt$row_id <- rownames(kable_dt) |
| collapse_matrix <- collapse_row_matrix(kable_dt, columns) |
| |
| for (i in 1:nrow(collapse_matrix)) { |
| matrix_row <- collapse_matrix[i, ] |
| if (sum(matrix_row) != length(matrix_row)) { |
| target_row <- xml_child(kable_tbody, i) |
| row_node_rm_count <- 0 |
| for (j in 1:length(matrix_row)) { |
| if (matrix_row[j] != 1) { |
| collapsing_col <- as.numeric(sub("x", "", names(matrix_row)[j])) - |
| row_node_rm_count |
| target_cell <- xml_child(target_row, collapsing_col) |
| if (matrix_row[j] == 0) { |
| xml_remove(target_cell) |
| row_node_rm_count <- row_node_rm_count + 1 |
| } else { |
| xml_attr(target_cell, "rowspan") <- matrix_row[j] |
| xml_attr(target_cell, "style") <- paste0( |
| xml_attr(target_cell, "style"), |
| "vertical-align: middle !important;") |
| } |
| } |
| } |
| } |
| } |
| |
| out <- structure(as.character(kable_xml), format = "html", |
| class = "knitr_kable") |
| attributes(out) <- kable_attrs |
| return(out) |
| } |
| |
| collapse_rows_latex <- function(kable_input, columns) { |
| # table_info <- magic_mirror(kable_input) |
| # target_row <- table_info$contents[row + 1] |
| # new_row <- latex_row_cells(target_row) |
| # if (bold) { |
| # new_row <- lapply(new_row, function(x) { |
| # paste0("\\\\bfseries{", x, "}") |
| # }) |
| # } |
| # if (italic) { |
| # new_row <- lapply(new_row, function(x) { |
| # paste0("\\\\em{", x, "}") |
| # }) |
| # } |
| # new_row <- paste(unlist(new_row), collapse = " & ") |
| # |
| # out <- sub(target_row, new_row, as.character(kable_input), perl = T) |
| # out <- structure(out, format = "latex", class = "knitr_kable") |
| # attr(out, "kable_meta") <- table_info |
| # return(out) |
| kable_input |
| } |