blob: 09eea05e09aeecaf486a51d943092fdda8a678ee [file] [log] [blame]
Hao Zhudb04e302015-11-15 16:57:38 -05001#' Magic mirror that returns kable's attributes
2#'
Hao Zhuf7994dd2017-02-27 16:58:42 -05003#' @description Mirror mirror tell me, how does this kable look like?
4#'
5#' @param kable_input The output of kable
Hao Zhu78e61222017-05-24 20:53:35 -04006#'
7#' @examples magic_mirror(knitr::kable(head(mtcars), "html"))
Hao Zhudb04e302015-11-15 16:57:38 -05008#' @export
9
Hao Zhuf7994dd2017-02-27 16:58:42 -050010magic_mirror <- function(kable_input){
11 if (!"knitr_kable" %in% attr(kable_input, "class")) {
Hao Zhudb04e302015-11-15 16:57:38 -050012 warning("magic_mirror may not be able to produce correct result if the",
13 " input table is not rendered by knitr::kable. ")
14 }
Hao Zhu32f43f72017-06-20 18:24:54 -040015 if ("kable_meta" %in% names(attributes(kable_input))) {
16 return(attr(kable_input, "kable_meta"))
Hao Zhu9b45a182017-02-27 18:17:46 -050017 }
Hao Zhuf7994dd2017-02-27 16:58:42 -050018 kable_format <- attr(kable_input, "format")
19 if (kable_format == "latex") {
20 kable_info <- magic_mirror_latex(kable_input)
Hao Zhudb04e302015-11-15 16:57:38 -050021 }
Hao Zhuf7994dd2017-02-27 16:58:42 -050022 if (kable_format == "html") {
23 kable_info <- magic_mirror_html(kable_input)
Hao Zhudb04e302015-11-15 16:57:38 -050024 }
Hao Zhu4adea852015-11-16 16:38:34 -050025 return(kable_info)
Hao Zhudb04e302015-11-15 16:57:38 -050026}
27
Hao Zhu953f3bd2017-07-28 11:43:40 -040028# Magic mirror for latex tables --------------
Hao Zhuf7994dd2017-02-27 16:58:42 -050029magic_mirror_latex <- function(kable_input){
Hao Zhuc05e1812017-02-25 01:45:35 -050030 kable_info <- list(tabular = NULL, booktabs = FALSE, align = NULL,
31 valign = NULL, ncol = NULL, nrow = NULL, colnames = NULL,
Hao Zhud57c2d72017-08-16 22:51:17 -040032 rownames = NULL, caption = NULL, caption.short = NULL,
33 contents = NULL,
Hao Zhuc05e1812017-02-25 01:45:35 -050034 centering = FALSE, table_env = FALSE)
Hao Zhu4adea852015-11-16 16:38:34 -050035 # Tabular
36 kable_info$tabular <- ifelse(
Hao Zhuf7994dd2017-02-27 16:58:42 -050037 grepl("\\\\begin\\{tabular\\}", kable_input),
Hao Zhu4adea852015-11-16 16:38:34 -050038 "tabular", "longtable"
Hao Zhudb04e302015-11-15 16:57:38 -050039 )
Hao Zhu4adea852015-11-16 16:38:34 -050040 # Booktabs
Hao Zhuf7994dd2017-02-27 16:58:42 -050041 kable_info$booktabs <- grepl("\\\\toprule", kable_input)
Hao Zhu4adea852015-11-16 16:38:34 -050042 # Align
43 kable_info$align <- gsub("\\|", "", str_match(
Hao Zhubff01912017-05-23 18:05:00 -040044 kable_input, paste0("\\\\begin\\{",
45 kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2])
46 kable_info$align_vector <- unlist(strsplit(kable_info$align, ""))
Hao Zhuf4b35292017-06-25 22:38:37 -100047 kable_info$align_vector_origin <- kable_info$align_vector
Hao Zhuc05e1812017-02-25 01:45:35 -050048 # valign
49 kable_info$valign <- gsub("\\|", "", str_match(
Hao Zhuf7994dd2017-02-27 16:58:42 -050050 kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2])
Hao Zhubff01912017-05-23 18:05:00 -040051 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 Zhu4adea852015-11-16 16:38:34 -050058 # N of columns
59 kable_info$ncol <- nchar(kable_info$align)
Hao Zhu4adea852015-11-16 16:38:34 -050060 # Caption
Hao Zhud57c2d72017-08-16 22:51:17 -040061 if (str_detect(kable_input, "caption\\[")) {
Hao Zhu70b89b12017-08-19 14:52:55 -040062 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,
qifei8743c772017-08-30 21:20:34 +020066 nchar(caption_line))
Hao Zhud57c2d72017-08-16 22:51:17 -040067 } 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 Zhu4adea852015-11-16 16:38:34 -050075 # Contents
Hao Zhuf7994dd2017-02-27 16:58:42 -050076 kable_info$contents <- str_match_all(kable_input, "\n(.*)\\\\\\\\")[[1]][,2]
Hao Zhu2ce42b92017-06-15 17:15:33 -040077 kable_info$contents <- regex_escape(kable_info$contents, T)
Hao Zhud384bc22018-05-12 22:24:30 -040078 if (kable_info$tabular == "longtable" & !is.na(kable_info$caption) &
79 !str_detect(kable_input, "\\\\begin\\{table\\}\\n\\n\\\\caption")) {
Hao Zhua3fc0c42017-02-27 12:04:59 -050080 kable_info$contents <- kable_info$contents[-1]
81 }
Hao Zhud384bc22018-05-12 22:24:30 -040082 if (!is.null(attr(kable_input, "n_head"))) {
83 n_head <- attr(kable_input, "n_head")
84 kable_info$new_header_row <- kable_info$contents[seq(n_head - 1, 1)]
85 kable_info$contents <- kable_info$contents[-seq(1, n_head - 1)]
86 kable_info$header_df <- extra_header_to_header_df(kable_info$new_header_row)
87 kable_info$new_header_row <- paste0(kable_info$new_header_row, "\\\\\\\\")
88 }
89 kable_info$nrow <- length(kable_info$contents)
Hao Zhu064990d2017-10-17 18:08:42 -040090 kable_info$duplicated_rows <- (sum(duplicated(kable_info$contents)) != 0)
Hao Zhu4adea852015-11-16 16:38:34 -050091 # Column names
Hao Zhu37dbe3f2018-05-14 11:16:06 -040092 if (kable_info$booktabs & !grepl("\\\\midrule", kable_input)) {
93 kable_info$colnames <- NULL
94 kable_info$position_offset <- 0
Leo83f05132018-05-10 14:12:16 +080095 } else {
96 kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]]
Hao Zhu37dbe3f2018-05-14 11:16:06 -040097 kable_info$position_offset <- 1
Leo83f05132018-05-10 14:12:16 +080098 }
Hao Zhu4adea852015-11-16 16:38:34 -050099 # Row names
100 kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*")
Hao Zhuc05e1812017-02-25 01:45:35 -0500101
Hao Zhuf7994dd2017-02-27 16:58:42 -0500102 kable_info$centering <- grepl("\\\\centering", kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500103
104 kable_info$table_env <- (!is.na(kable_info$caption) &
105 kable_info$tabular != "longtable")
Hao Zhud384bc22018-05-12 22:24:30 -0400106
Hao Zhu4adea852015-11-16 16:38:34 -0500107 return(kable_info)
Hao Zhudb04e302015-11-15 16:57:38 -0500108}
Hao Zhu8977a8a2015-11-19 16:52:21 -0500109
Hao Zhud384bc22018-05-12 22:24:30 -0400110extra_header_to_header_df <- function(extra_header_rows) {
111 lapply(str_split(extra_header_rows, " \\& "), function(x) {
112 as.data.frame(t(sapply(x, extra_header_to_header_df_)), row.names = NA)
113 })
114}
115
116extra_header_to_header_df_ <- function(x) {
117 if (trimws(x) == "") return(c(header = " ", colspan = "1"))
118 x <- trimws(x)
119 x_header <- str_match(x, "([^\\}\\{]*)\\\\\\}$")[2]
120 x_colspan <- str_match(x, "^\\\\\\\\multicolumn\\\\\\{([^\\\\\\}]*)")[2]
121 return(c(header = x_header, colspan = x_colspan))
122}
123
Hao Zhu953f3bd2017-07-28 11:43:40 -0400124# Magic Mirror for html table --------
Hao Zhuf7994dd2017-02-27 16:58:42 -0500125magic_mirror_html <- function(kable_input){
126 kable_info <- list()
Hao Zhu558c72f2017-07-24 15:12:00 -0400127 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu8977a8a2015-11-19 16:52:21 -0500128 # Caption
Hao Zhuf7994dd2017-02-27 16:58:42 -0500129 kable_info$caption <- xml_text(xml_child(kable_xml, "caption"))
Hao Zhu8977a8a2015-11-19 16:52:21 -0500130 # Contents
Hao Zhu74eb6ad2017-03-04 09:32:37 -0500131 # kable_info$contents <- html_table(read_html(as.character(kable_input)))[[1]]
Hao Zhu8977a8a2015-11-19 16:52:21 -0500132 # colnames
Hao Zhuf7994dd2017-02-27 16:58:42 -0500133 kable_info$colnames <- lapply(xml_children(xml_child(kable_xml, "thead")),
134 xml_children)
135 kable_info$colnames <- kable_info$colnames[[length(kable_info$colnames)]]
136 kable_info$colnames <- trimws(xml_text(kable_info$colnames))
Hao Zhu8977a8a2015-11-19 16:52:21 -0500137 kable_info$ncol <- length(kable_info$colnames)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500138 kable_info$nrow_header <- length(xml_children(xml_child(kable_xml, "thead")))
139 kable_info$nrow_body <- nrow(kable_info$contents)
140 kable_info$table_class <- xml_attr(kable_xml, "class")
141 kable_info$table_style <- xml_attr(kable_xml, "style")
Hao Zhu8977a8a2015-11-19 16:52:21 -0500142 return(kable_info)
143}
144
Hao Zhu26234122017-02-22 15:34:33 -0500145