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){ |
| 11 | if (!"knitr_kable" %in% attr(kable_input, "class")) { |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 12 | warning("magic_mirror may not be able to produce correct result if the", |
| 13 | " input table is not rendered by knitr::kable. ") |
| 14 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 15 | if ("kable_meta" %in% names(attributes(kable_input))) { |
| 16 | return(attr(kable_input, "kable_meta")) |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 17 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 18 | kable_format <- attr(kable_input, "format") |
| 19 | if (kable_format == "latex") { |
| 20 | kable_info <- magic_mirror_latex(kable_input) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 21 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 22 | if (kable_format == "html") { |
| 23 | kable_info <- magic_mirror_html(kable_input) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 24 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 25 | return(kable_info) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 26 | } |
| 27 | |
Hao Zhu | 953f3bd | 2017-07-28 11:43:40 -0400 | [diff] [blame] | 28 | # Magic mirror for latex tables -------------- |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 29 | magic_mirror_latex <- function(kable_input){ |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 30 | kable_info <- list(tabular = NULL, booktabs = FALSE, align = NULL, |
| 31 | valign = NULL, ncol = NULL, nrow = NULL, colnames = NULL, |
Hao Zhu | d57c2d7 | 2017-08-16 22:51:17 -0400 | [diff] [blame] | 32 | rownames = NULL, caption = NULL, caption.short = NULL, |
| 33 | contents = NULL, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 34 | centering = FALSE, table_env = FALSE) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 35 | # Tabular |
| 36 | kable_info$tabular <- ifelse( |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 37 | grepl("\\\\begin\\{tabular\\}", kable_input), |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 38 | "tabular", "longtable" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 39 | ) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 40 | # Booktabs |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 41 | kable_info$booktabs <- grepl("\\\\toprule", kable_input) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 42 | # Align |
| 43 | kable_info$align <- gsub("\\|", "", str_match( |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 44 | kable_input, paste0("\\\\begin\\{", |
| 45 | kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2]) |
| 46 | kable_info$align_vector <- unlist(strsplit(kable_info$align, "")) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 47 | kable_info$align_vector_origin <- kable_info$align_vector |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 48 | # valign |
| 49 | kable_info$valign <- gsub("\\|", "", str_match( |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 50 | kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2]) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 51 | kable_info$valign2 <- sub("\\[", "\\\\[", kable_info$valign) |
| 52 | kable_info$valign2 <- sub("\\]", "\\\\]", kable_info$valign2) |
| 53 | kable_info$valign3 <- sub("\\[", "", kable_info$valign) |
| 54 | kable_info$valign3 <- sub("\\]", "", kable_info$valign3) |
| 55 | kable_info$begin_tabular <- paste0("\\\\begin\\{", kable_info$tabular, "\\}", |
| 56 | kable_info$valign2) |
| 57 | kable_info$end_tabular <- paste0("\\\\end\\{", kable_info$tabular, "\\}") |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 58 | # N of columns |
| 59 | kable_info$ncol <- nchar(kable_info$align) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 60 | # Caption |
Hao Zhu | d57c2d7 | 2017-08-16 22:51:17 -0400 | [diff] [blame] | 61 | if (str_detect(kable_input, "caption\\[")) { |
Hao Zhu | 70b89b1 | 2017-08-19 14:52:55 -0400 | [diff] [blame] | 62 | caption_line <- str_match(kable_input, "\\\\caption(.*)\\n")[2] |
| 63 | kable_info$caption.short <- str_match(caption_line, "\\[(.*?)\\]")[2] |
| 64 | kable_info$caption <- substr(caption_line, |
| 65 | nchar(kable_info$caption.short) + 4, |
| 66 | nchar(caption_line) - 1) |
Hao Zhu | d57c2d7 | 2017-08-16 22:51:17 -0400 | [diff] [blame] | 67 | } else { |
| 68 | kable_info$caption <- str_match(kable_input, "caption\\{(.*?)\\n")[2] |
| 69 | } |
| 70 | if (kable_info$tabular == "longtable") { |
| 71 | kable_info$caption <- str_sub(kable_info$caption, 1, -4) |
| 72 | } else { |
| 73 | kable_info$caption <- str_sub(kable_info$caption, 1, -2) |
| 74 | } |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 75 | # N of rows |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 76 | kable_info$nrow <- str_count(kable_input, "\\\\\n") - |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 77 | # in the dev version (currently as of 11.2015) of knitr, when longtable is |
| 78 | # enabled, caption is moved inside the tabular environment. As a result, |
| 79 | # the number of rows should be adjusted. |
| 80 | ifelse( |
| 81 | kable_info$tabular == "longtable" & !is.na(kable_info$caption) & |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 82 | !str_detect(kable_input, "\\\\begin\\{table\\}\\n\\n\\\\caption"), |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 83 | 1,0 |
| 84 | ) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 85 | # Contents |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 86 | kable_info$contents <- str_match_all(kable_input, "\n(.*)\\\\\\\\")[[1]][,2] |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 87 | kable_info$contents <- regex_escape(kable_info$contents, T) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 88 | if (kable_info$tabular == "longtable" & !is.na(kable_info$caption)) { |
| 89 | kable_info$contents <- kable_info$contents[-1] |
| 90 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 91 | # Column names |
| 92 | kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]] |
| 93 | # Row names |
| 94 | kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*") |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 95 | |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 96 | kable_info$centering <- grepl("\\\\centering", kable_input) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 97 | |
| 98 | kable_info$table_env <- (!is.na(kable_info$caption) & |
| 99 | kable_info$tabular != "longtable") |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 100 | return(kable_info) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 101 | } |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 102 | |
Hao Zhu | 953f3bd | 2017-07-28 11:43:40 -0400 | [diff] [blame] | 103 | # Magic Mirror for html table -------- |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 104 | magic_mirror_html <- function(kable_input){ |
| 105 | kable_info <- list() |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 106 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 107 | # Caption |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 108 | kable_info$caption <- xml_text(xml_child(kable_xml, "caption")) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 109 | # Contents |
Hao Zhu | 74eb6ad | 2017-03-04 09:32:37 -0500 | [diff] [blame] | 110 | # kable_info$contents <- html_table(read_html(as.character(kable_input)))[[1]] |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 111 | # colnames |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 112 | kable_info$colnames <- lapply(xml_children(xml_child(kable_xml, "thead")), |
| 113 | xml_children) |
| 114 | kable_info$colnames <- kable_info$colnames[[length(kable_info$colnames)]] |
| 115 | kable_info$colnames <- trimws(xml_text(kable_info$colnames)) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 116 | kable_info$ncol <- length(kable_info$colnames) |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 117 | kable_info$nrow_header <- length(xml_children(xml_child(kable_xml, "thead"))) |
| 118 | kable_info$nrow_body <- nrow(kable_info$contents) |
| 119 | kable_info$table_class <- xml_attr(kable_xml, "class") |
| 120 | kable_info$table_style <- xml_attr(kable_xml, "style") |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 121 | return(kable_info) |
| 122 | } |
| 123 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 124 | |