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