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 | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 25 | kable_info <- list(tabular = NULL, booktabs = FALSE, align = NULL, |
| 26 | valign = NULL, ncol = NULL, nrow = NULL, colnames = NULL, |
| 27 | rownames = NULL, caption = NULL, contents = NULL, |
| 28 | centering = FALSE, table_env = FALSE) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 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 |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 35 | kable_info$booktabs <- grepl("\\\\toprule", input) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 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 | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 39 | # valign |
| 40 | kable_info$valign <- gsub("\\|", "", str_match( |
| 41 | input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2]) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 42 | # N of columns |
| 43 | kable_info$ncol <- nchar(kable_info$align) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 44 | # Caption |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame^] | 45 | kable_info$caption <- str_match(input, "caption\\{(.*?)\\n")[2] |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 46 | # N of rows |
| 47 | kable_info$nrow <- str_count(input, "\\\\\n") - |
| 48 | # in the dev version (currently as of 11.2015) of knitr, when longtable is |
| 49 | # enabled, caption is moved inside the tabular environment. As a result, |
| 50 | # the number of rows should be adjusted. |
| 51 | ifelse( |
| 52 | kable_info$tabular == "longtable" & !is.na(kable_info$caption) & |
| 53 | !str_detect(input, "\\\\begin\\{table\\}\\n\\n\\\\caption"), |
| 54 | 1,0 |
| 55 | ) |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 56 | # Contents |
| 57 | kable_info$contents <- str_match_all(input, "\n(.*)\\\\\\\\")[[1]][,2] |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame^] | 58 | if (kable_info$tabular == "longtable" & !is.na(kable_info$caption)) { |
| 59 | kable_info$contents <- kable_info$contents[-1] |
| 60 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 61 | # Column names |
| 62 | kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]] |
| 63 | # Row names |
| 64 | kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*") |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 65 | |
| 66 | kable_info$centering <- grepl("\\\\centering", input) |
| 67 | |
| 68 | kable_info$table_env <- (!is.na(kable_info$caption) & |
| 69 | kable_info$tabular != "longtable") |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 70 | return(kable_info) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 71 | } |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 72 | |
| 73 | #' Magic Mirror for html table -------- |
| 74 | #' |
| 75 | #' @param input The output of kable |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 76 | magic_mirror_html <- function(input){ |
| 77 | kable_info <- list(table.attr = NULL, align = NULL, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 78 | ncol = NULL, nrow = NULL, colnames = NULL, rownames = NULL, |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 79 | caption = NULL, contents = NULL) |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 80 | kable_data <- html_table(read_html(input)) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 81 | # Caption |
| 82 | kable_info$caption <- names(kable_data) |
| 83 | # Contents |
| 84 | kable_info$contents <- kable_data[[1]] |
| 85 | # colnames |
| 86 | kable_info$colnames <- str_replace_all( |
| 87 | str_trim(names(kable_data[[1]])), "V[0-9]{1,2}", "" |
| 88 | ) |
| 89 | # rownames |
| 90 | kable_info$rownames <- as.character(kable_data[[1]][,1]) |
| 91 | if(str_trim(names(kable_data[[1]])[1]) != "V1"){ |
| 92 | kable_info$rownames <- c(str_trim(names(kable_data[[1]])[1]), |
| 93 | kable_info$rownames)} |
| 94 | # ncol |
| 95 | kable_info$ncol <- length(kable_info$colnames) |
| 96 | # nrow |
| 97 | kable_info$nrow <- length(kable_info$rownames) |
| 98 | # table.attr |
| 99 | kable_info$table.attr <- str_match(input, "<table class = '(.*)'>")[2] |
| 100 | # align |
| 101 | kable_info$align <- str_match_all( |
| 102 | input, 'style=\\"text-align:([^;]*);' |
| 103 | )[[1]][,2] |
| 104 | kable_info$align <- paste0( |
| 105 | str_extract(tail(kable_info$align, kable_info$ncol), "."), collapse = "" |
| 106 | ) |
| 107 | return(kable_info) |
| 108 | } |
| 109 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 110 | |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 111 | |