Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 1 | #' Magic mirror that returns kable's attributes |
| 2 | #' |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 3 | #' @description Mirror mirror tell me, how does this kable look like? |
| 4 | #' |
| 5 | #' @param kable_input The output of kable |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 6 | #' |
| 7 | #' @examples magic_mirror(knitr::kable(head(mtcars), "html")) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 8 | #' @export |
| 9 | |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 10 | magic_mirror <- function(kable_input){ |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 11 | kable_format <- attr(kable_input, "format") |
| 12 | if (kable_format == "latex") { |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 13 | table_info <- magic_mirror_latex(kable_input) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 14 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 15 | if (kable_format == "html") { |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 16 | table_info <- magic_mirror_html(kable_input) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 17 | } |
Vincent Arel-Bundock | ef8ffce | 2021-01-19 08:55:16 -0500 | [diff] [blame^] | 18 | if ("kable_meta" %in% names(attributes(kable_input))) { |
| 19 | out <- attr(kable_input, "kable_meta") |
| 20 | # if we return `kable_meta` immediately, `kable_styling` will use the |
| 21 | # original `table_env` value. So if we call `kable_styling` twice on the |
| 22 | # same object, it will nest a table within a table. Make sure this does not |
| 23 | # happen. |
| 24 | if (kable_format == "latex") { |
| 25 | table_info <- magic_mirror_latex(kable_input) |
| 26 | if (table_info$table_env && !out$table_env) { |
| 27 | out$table_env <- table_info$table_env |
| 28 | } |
| 29 | } |
| 30 | return(out) |
| 31 | } |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 32 | return(table_info) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 33 | } |
| 34 | |
Hao Zhu | 953f3bd | 2017-07-28 11:43:40 -0400 | [diff] [blame] | 35 | # Magic mirror for latex tables -------------- |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 36 | magic_mirror_latex <- function(kable_input){ |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 37 | table_info <- list(tabular = NULL, booktabs = FALSE, align = NULL, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 38 | valign = NULL, ncol = NULL, nrow = NULL, colnames = NULL, |
Hao Zhu | d57c2d7 | 2017-08-16 22:51:17 -0400 | [diff] [blame] | 39 | rownames = NULL, caption = NULL, caption.short = NULL, |
| 40 | contents = NULL, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 41 | centering = FALSE, table_env = FALSE) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 42 | # Tabular |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 43 | table_info$tabular <- ifelse( |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 44 | grepl("\\\\begin\\{tabular\\}", kable_input), |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 45 | "tabular", "longtable" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 46 | ) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 47 | # Booktabs |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 48 | table_info$booktabs <- grepl("\\\\toprule", kable_input) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 49 | # Align |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 50 | table_info$align <- gsub("\\|", "", str_match( |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 51 | kable_input, paste0("\\\\begin\\{", |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 52 | table_info$tabular,"\\}.*\\{(.*?)\\}"))[2]) |
| 53 | table_info$align_vector <- unlist(strsplit(table_info$align, "")) |
| 54 | table_info$align_vector_origin <- table_info$align_vector |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 55 | # valign |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 56 | table_info$valign <- gsub("\\|", "", str_match( |
| 57 | kable_input, paste0("\\\\begin\\{", table_info$tabular,"\\}(.*)\\{.*?\\}"))[2]) |
| 58 | table_info$valign2 <- sub("\\[", "\\\\[", table_info$valign) |
| 59 | table_info$valign2 <- sub("\\]", "\\\\]", table_info$valign2) |
| 60 | table_info$valign3 <- sub("\\[", "", table_info$valign) |
| 61 | table_info$valign3 <- sub("\\]", "", table_info$valign3) |
| 62 | table_info$begin_tabular <- paste0("\\\\begin\\{", table_info$tabular, "\\}", |
| 63 | table_info$valign2) |
| 64 | table_info$end_tabular <- paste0("\\\\end\\{", table_info$tabular, "\\}") |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 65 | # N of columns |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 66 | table_info$ncol <- nchar(table_info$align) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 67 | # Caption |
Hao Zhu | d57c2d7 | 2017-08-16 22:51:17 -0400 | [diff] [blame] | 68 | if (str_detect(kable_input, "caption\\[")) { |
Hao Zhu | 70b89b1 | 2017-08-19 14:52:55 -0400 | [diff] [blame] | 69 | caption_line <- str_match(kable_input, "\\\\caption(.*)\\n")[2] |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 70 | table_info$caption.short <- str_match(caption_line, "\\[(.*?)\\]")[2] |
| 71 | table_info$caption <- substr(caption_line, |
| 72 | nchar(table_info$caption.short) + 4, |
qifei | 8743c77 | 2017-08-30 21:20:34 +0200 | [diff] [blame] | 73 | nchar(caption_line)) |
Hao Zhu | d57c2d7 | 2017-08-16 22:51:17 -0400 | [diff] [blame] | 74 | } else { |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 75 | table_info$caption <- str_match(kable_input, "caption\\{(.*?)\\n")[2] |
Hao Zhu | d57c2d7 | 2017-08-16 22:51:17 -0400 | [diff] [blame] | 76 | } |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 77 | if (table_info$tabular == "longtable") { |
| 78 | table_info$caption <- str_sub(table_info$caption, 1, -4) |
Hao Zhu | d57c2d7 | 2017-08-16 22:51:17 -0400 | [diff] [blame] | 79 | } else { |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 80 | table_info$caption <- str_sub(table_info$caption, 1, -2) |
Hao Zhu | d57c2d7 | 2017-08-16 22:51:17 -0400 | [diff] [blame] | 81 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 82 | # Contents |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 83 | table_info$contents <- str_match_all(kable_input, "\n(.*)\\\\\\\\")[[1]][,2] |
| 84 | table_info$contents <- regex_escape(table_info$contents, T) |
| 85 | if (table_info$tabular == "longtable" & !is.na(table_info$caption) & |
Hao Zhu | d384bc2 | 2018-05-12 22:24:30 -0400 | [diff] [blame] | 86 | !str_detect(kable_input, "\\\\begin\\{table\\}\\n\\n\\\\caption")) { |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 87 | table_info$contents <- table_info$contents[-1] |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 88 | } |
Hao Zhu | d384bc2 | 2018-05-12 22:24:30 -0400 | [diff] [blame] | 89 | if (!is.null(attr(kable_input, "n_head"))) { |
| 90 | n_head <- attr(kable_input, "n_head") |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 91 | table_info$new_header_row <- table_info$contents[seq(n_head - 1, 1)] |
| 92 | table_info$contents <- table_info$contents[-seq(1, n_head - 1)] |
| 93 | table_info$header_df <- extra_header_to_header_df(table_info$new_header_row) |
| 94 | table_info$new_header_row <- paste0(table_info$new_header_row, "\\\\\\\\") |
Hao Zhu | d384bc2 | 2018-05-12 22:24:30 -0400 | [diff] [blame] | 95 | } |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 96 | table_info$nrow <- length(table_info$contents) |
| 97 | table_info$duplicated_rows <- (sum(duplicated(table_info$contents)) != 0) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 98 | # Column names |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 99 | if (table_info$booktabs & !grepl("\\\\midrule", kable_input)) { |
| 100 | table_info$colnames <- NULL |
| 101 | table_info$position_offset <- 0 |
Leo | 83f0513 | 2018-05-10 14:12:16 +0800 | [diff] [blame] | 102 | } else { |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 103 | table_info$colnames <- str_split(table_info$contents[1], " \\& ")[[1]] |
| 104 | table_info$position_offset <- 1 |
Leo | 83f0513 | 2018-05-10 14:12:16 +0800 | [diff] [blame] | 105 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 106 | # Row names |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 107 | table_info$rownames <- str_extract(table_info$contents, "^[^ &]*") |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 108 | |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 109 | table_info$centering <- grepl("\\\\centering", kable_input) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 110 | |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 111 | table_info$table_env <- (!is.na(table_info$caption) & |
Vincent Arel-Bundock | ef8ffce | 2021-01-19 08:55:16 -0500 | [diff] [blame^] | 112 | table_info$tabular != "longtable") || |
| 113 | grepl("\\\\begin\\{table\\}", kable_input) |
Hao Zhu | d384bc2 | 2018-05-12 22:24:30 -0400 | [diff] [blame] | 114 | |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 115 | return(table_info) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 116 | } |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 117 | |
Hao Zhu | d384bc2 | 2018-05-12 22:24:30 -0400 | [diff] [blame] | 118 | extra_header_to_header_df <- function(extra_header_rows) { |
| 119 | lapply(str_split(extra_header_rows, " \\& "), function(x) { |
| 120 | as.data.frame(t(sapply(x, extra_header_to_header_df_)), row.names = NA) |
| 121 | }) |
| 122 | } |
| 123 | |
| 124 | extra_header_to_header_df_ <- function(x) { |
| 125 | if (trimws(x) == "") return(c(header = " ", colspan = "1")) |
| 126 | x <- trimws(x) |
| 127 | x_header <- str_match(x, "([^\\}\\{]*)\\\\\\}$")[2] |
| 128 | x_colspan <- str_match(x, "^\\\\\\\\multicolumn\\\\\\{([^\\\\\\}]*)")[2] |
| 129 | return(c(header = x_header, colspan = x_colspan)) |
| 130 | } |
| 131 | |
Hao Zhu | 953f3bd | 2017-07-28 11:43:40 -0400 | [diff] [blame] | 132 | # Magic Mirror for html table -------- |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 133 | magic_mirror_html <- function(kable_input){ |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 134 | table_info <- list() |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 135 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 136 | # Caption |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 137 | table_info$caption <- xml_text(xml_child(kable_xml, "caption")) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 138 | # Contents |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 139 | # table_info$contents <- html_table(read_html(as.character(kable_input)))[[1]] |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 140 | # colnames |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 141 | table_info$colnames <- lapply(xml_children(xml_child(kable_xml, "thead")), |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 142 | xml_children) |
Hao Zhu | 24bf30c | 2019-01-22 02:14:33 -0500 | [diff] [blame] | 143 | table_info$colnames <- table_info$colnames[[length(table_info$colnames)]] |
| 144 | table_info$colnames <- trimws(xml_text(table_info$colnames)) |
| 145 | table_info$ncol <- length(table_info$colnames) |
| 146 | table_info$nrow_header <- length(xml_children(xml_child(kable_xml, "thead"))) |
| 147 | table_info$nrow_body <- nrow(table_info$contents) |
| 148 | table_info$table_class <- xml_attr(kable_xml, "class") |
| 149 | table_info$table_style <- xml_attr(kable_xml, "style") |
| 150 | return(table_info) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 153 | |