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 | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 6 | #' @export |
| 7 | |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 8 | magic_mirror <- function(kable_input){ |
| 9 | if (!"knitr_kable" %in% attr(kable_input, "class")) { |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 10 | warning("magic_mirror may not be able to produce correct result if the", |
| 11 | " input table is not rendered by knitr::kable. ") |
| 12 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 13 | kable_format <- attr(kable_input, "format") |
| 14 | if (kable_format == "latex") { |
| 15 | kable_info <- magic_mirror_latex(kable_input) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 16 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 17 | if (kable_format == "html") { |
| 18 | kable_info <- magic_mirror_html(kable_input) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 19 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 20 | return(kable_info) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 21 | } |
| 22 | |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 23 | #' Magic mirror for latex tables -------------- |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 24 | #' @param kable_input The output of kable |
| 25 | magic_mirror_latex <- function(kable_input){ |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 26 | kable_info <- list(tabular = NULL, booktabs = FALSE, align = NULL, |
| 27 | valign = NULL, ncol = NULL, nrow = NULL, colnames = NULL, |
| 28 | rownames = NULL, caption = NULL, contents = NULL, |
| 29 | centering = FALSE, table_env = FALSE) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 30 | # Tabular |
| 31 | kable_info$tabular <- ifelse( |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 32 | grepl("\\\\begin\\{tabular\\}", kable_input), |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 33 | "tabular", "longtable" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 34 | ) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 35 | # Booktabs |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 36 | kable_info$booktabs <- grepl("\\\\toprule", kable_input) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 37 | # Align |
| 38 | kable_info$align <- gsub("\\|", "", str_match( |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 39 | kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2]) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 40 | # valign |
| 41 | kable_info$valign <- gsub("\\|", "", str_match( |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 42 | kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2]) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 43 | # N of columns |
| 44 | kable_info$ncol <- nchar(kable_info$align) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 45 | # Caption |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 46 | kable_info$caption <- str_match(kable_input, "caption\\{(.*?)\\n")[2] |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 47 | # N of rows |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 48 | kable_info$nrow <- str_count(kable_input, "\\\\\n") - |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 49 | # in the dev version (currently as of 11.2015) of knitr, when longtable is |
| 50 | # enabled, caption is moved inside the tabular environment. As a result, |
| 51 | # the number of rows should be adjusted. |
| 52 | ifelse( |
| 53 | kable_info$tabular == "longtable" & !is.na(kable_info$caption) & |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 54 | !str_detect(kable_input, "\\\\begin\\{table\\}\\n\\n\\\\caption"), |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 55 | 1,0 |
| 56 | ) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 57 | # Contents |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 58 | kable_info$contents <- str_match_all(kable_input, "\n(.*)\\\\\\\\")[[1]][,2] |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 59 | if (kable_info$tabular == "longtable" & !is.na(kable_info$caption)) { |
| 60 | kable_info$contents <- kable_info$contents[-1] |
| 61 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 62 | # Column names |
| 63 | kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]] |
| 64 | # Row names |
| 65 | kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*") |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 66 | |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 67 | kable_info$centering <- grepl("\\\\centering", kable_input) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 68 | |
| 69 | kable_info$table_env <- (!is.na(kable_info$caption) & |
| 70 | kable_info$tabular != "longtable") |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 71 | return(kable_info) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 72 | } |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 73 | |
| 74 | #' Magic Mirror for html table -------- |
| 75 | #' |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 76 | #' @param kable_input The output of kable |
| 77 | magic_mirror_html <- function(kable_input){ |
| 78 | kable_info <- list() |
| 79 | kable_xml <- read_xml(as.character(kable_input)) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 80 | # Caption |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 81 | kable_info$caption <- xml_text(xml_child(kable_xml, "caption")) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 82 | # Contents |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 83 | kable_info$contents <- html_table(read_html(as.character(kable_input)))[[1]] |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 84 | # colnames |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 85 | kable_info$colnames <- lapply(xml_children(xml_child(kable_xml, "thead")), |
| 86 | xml_children) |
| 87 | kable_info$colnames <- kable_info$colnames[[length(kable_info$colnames)]] |
| 88 | kable_info$colnames <- trimws(xml_text(kable_info$colnames)) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 89 | kable_info$ncol <- length(kable_info$colnames) |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 90 | kable_info$nrow_header <- length(xml_children(xml_child(kable_xml, "thead"))) |
| 91 | kable_info$nrow_body <- nrow(kable_info$contents) |
| 92 | kable_info$table_class <- xml_attr(kable_xml, "class") |
| 93 | kable_info$table_style <- xml_attr(kable_xml, "style") |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 94 | return(kable_info) |
| 95 | } |
| 96 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 97 | |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 98 | |