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