Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 1 | #' Magic mirror that returns kable's attributes |
| 2 | #' |
| 3 | #' @param input The output of kable |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 4 | #' @importFrom knitr kable |
| 5 | #' @import stringr |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 6 | #' @export |
| 7 | |
| 8 | magic_mirror <- function(input){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 9 | if(!"knitr_kable" %in% attr(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 | } |
| 13 | kable_format <- attr(input, "format") |
| 14 | if (kable_format == "latex"){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 15 | kable_info <- magic_mirror_latex(input) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 16 | } |
| 17 | if (kable_format == "html"){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 18 | kable_info <- magic_mirror_html(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 -------------- |
| 24 | #' @param input The output of kable |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 25 | magic_mirror_latex <- function(input){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 26 | kable_info <- list(tabular = NULL, booktabs = NULL, align = NULL, |
| 27 | ncol=NULL, nrow=NULL, colnames = NULL, rownames = NULL, |
| 28 | caption = NULL, contents = NULL) |
| 29 | # Tabular |
| 30 | kable_info$tabular <- ifelse( |
| 31 | grepl("\\\\begin\\{tabular\\}", input), |
| 32 | "tabular", "longtable" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 33 | ) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 34 | # Booktabs |
| 35 | kable_info$booktabs <- ifelse(grepl("\\\\toprule", input), TRUE, FALSE) |
| 36 | # Align |
| 37 | kable_info$align <- gsub("\\|", "", str_match( |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 38 | input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2]) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 39 | # N of columns |
| 40 | kable_info$ncol <- nchar(kable_info$align) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 41 | # Caption |
| 42 | kable_info$caption <- str_match(input, "caption\\{(.*?)\\}")[2] |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 43 | # N of rows |
| 44 | kable_info$nrow <- str_count(input, "\\\\\n") - |
| 45 | # in the dev version (currently as of 11.2015) of knitr, when longtable is |
| 46 | # enabled, caption is moved inside the tabular environment. As a result, |
| 47 | # the number of rows should be adjusted. |
| 48 | ifelse( |
| 49 | kable_info$tabular == "longtable" & !is.na(kable_info$caption) & |
| 50 | !str_detect(input, "\\\\begin\\{table\\}\\n\\n\\\\caption"), |
| 51 | 1,0 |
| 52 | ) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 53 | # Contents |
| 54 | kable_info$contents <- str_match_all(input, "\n(.*)\\\\\\\\")[[1]][,2] |
| 55 | # Column names |
| 56 | kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]] |
| 57 | # Row names |
| 58 | kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*") |
| 59 | return(kable_info) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 60 | } |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 61 | |
| 62 | #' Magic Mirror for html table -------- |
| 63 | #' |
| 64 | #' @param input The output of kable |
| 65 | #' |
| 66 | #' @importFrom XML readHTMLTable |
| 67 | magic_mirror_html <- function(input){ |
| 68 | kable_info <- list(table.attr = NULL, align = NULL, |
| 69 | ncol=NULL, nrow=NULL, colnames = NULL, rownames = NULL, |
| 70 | caption = NULL, contents = NULL) |
| 71 | kable_data <- readHTMLTable(input[1]) |
| 72 | # Caption |
| 73 | kable_info$caption <- names(kable_data) |
| 74 | # Contents |
| 75 | kable_info$contents <- kable_data[[1]] |
| 76 | # colnames |
| 77 | kable_info$colnames <- str_replace_all( |
| 78 | str_trim(names(kable_data[[1]])), "V[0-9]{1,2}", "" |
| 79 | ) |
| 80 | # rownames |
| 81 | kable_info$rownames <- as.character(kable_data[[1]][,1]) |
| 82 | if(str_trim(names(kable_data[[1]])[1]) != "V1"){ |
| 83 | kable_info$rownames <- c(str_trim(names(kable_data[[1]])[1]), |
| 84 | kable_info$rownames)} |
| 85 | # ncol |
| 86 | kable_info$ncol <- length(kable_info$colnames) |
| 87 | # nrow |
| 88 | kable_info$nrow <- length(kable_info$rownames) |
| 89 | # table.attr |
| 90 | kable_info$table.attr <- str_match(input, "<table class = '(.*)'>")[2] |
| 91 | # align |
| 92 | kable_info$align <- str_match_all( |
| 93 | input, 'style=\\"text-align:([^;]*);' |
| 94 | )[[1]][,2] |
| 95 | kable_info$align <- paste0( |
| 96 | str_extract(tail(kable_info$align, kable_info$ncol), "."), collapse = "" |
| 97 | ) |
| 98 | return(kable_info) |
| 99 | } |
| 100 | |
| 101 | #' @export |
| 102 | magrittr::`%>%` |
| 103 | |