blob: 6276eae7b1af98d4380dc5c7a44d7f974c06067f [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 Zhu9b45a182017-02-27 18:17:46 -050015 if ("original_kable_meta" %in% names(attributes(kable_input))) {
16 return(attr(kable_input, "original_kable_meta"))
17 }
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 Zhu8977a8a2015-11-19 16:52:21 -050028#' Magic mirror for latex tables --------------
Hao Zhuf7994dd2017-02-27 16:58:42 -050029#' @param kable_input The output of kable
30magic_mirror_latex <- function(kable_input){
Hao Zhuc05e1812017-02-25 01:45:35 -050031 kable_info <- list(tabular = NULL, booktabs = FALSE, align = NULL,
32 valign = NULL, ncol = NULL, nrow = NULL, colnames = NULL,
33 rownames = NULL, caption = NULL, contents = NULL,
34 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 Zhuc05e1812017-02-25 01:45:35 -050047 # valign
48 kable_info$valign <- gsub("\\|", "", str_match(
Hao Zhuf7994dd2017-02-27 16:58:42 -050049 kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2])
Hao Zhubff01912017-05-23 18:05:00 -040050 kable_info$valign2 <- sub("\\[", "\\\\[", kable_info$valign)
51 kable_info$valign2 <- sub("\\]", "\\\\]", kable_info$valign2)
52 kable_info$valign3 <- sub("\\[", "", kable_info$valign)
53 kable_info$valign3 <- sub("\\]", "", kable_info$valign3)
54 kable_info$begin_tabular <- paste0("\\\\begin\\{", kable_info$tabular, "\\}",
55 kable_info$valign2)
56 kable_info$end_tabular <- paste0("\\\\end\\{", kable_info$tabular, "\\}")
Hao Zhu4adea852015-11-16 16:38:34 -050057 # N of columns
58 kable_info$ncol <- nchar(kable_info$align)
Hao Zhu4adea852015-11-16 16:38:34 -050059 # Caption
Hao Zhuf7994dd2017-02-27 16:58:42 -050060 kable_info$caption <- str_match(kable_input, "caption\\{(.*?)\\n")[2]
Hao Zhufdec1842017-06-08 17:06:04 -040061 kable_info$caption <- str_sub(kable_info$caption, 1, -4)
Hao Zhudc4b7142015-11-19 10:37:53 -050062 # N of rows
Hao Zhuf7994dd2017-02-27 16:58:42 -050063 kable_info$nrow <- str_count(kable_input, "\\\\\n") -
Hao Zhudc4b7142015-11-19 10:37:53 -050064 # in the dev version (currently as of 11.2015) of knitr, when longtable is
65 # enabled, caption is moved inside the tabular environment. As a result,
66 # the number of rows should be adjusted.
67 ifelse(
68 kable_info$tabular == "longtable" & !is.na(kable_info$caption) &
Hao Zhuf7994dd2017-02-27 16:58:42 -050069 !str_detect(kable_input, "\\\\begin\\{table\\}\\n\\n\\\\caption"),
Hao Zhudc4b7142015-11-19 10:37:53 -050070 1,0
71 )
Hao Zhu4adea852015-11-16 16:38:34 -050072 # Contents
Hao Zhuf7994dd2017-02-27 16:58:42 -050073 kable_info$contents <- str_match_all(kable_input, "\n(.*)\\\\\\\\")[[1]][,2]
Hao Zhue4ba9932017-06-07 12:45:43 -040074 kable_info$contents <- latex_contents_escape(kable_info$contents)
Hao Zhua3fc0c42017-02-27 12:04:59 -050075 if (kable_info$tabular == "longtable" & !is.na(kable_info$caption)) {
76 kable_info$contents <- kable_info$contents[-1]
77 }
Hao Zhu4adea852015-11-16 16:38:34 -050078 # Column names
79 kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]]
80 # Row names
81 kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*")
Hao Zhuc05e1812017-02-25 01:45:35 -050082
Hao Zhuf7994dd2017-02-27 16:58:42 -050083 kable_info$centering <- grepl("\\\\centering", kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -050084
85 kable_info$table_env <- (!is.na(kable_info$caption) &
86 kable_info$tabular != "longtable")
Hao Zhu4adea852015-11-16 16:38:34 -050087 return(kable_info)
Hao Zhudb04e302015-11-15 16:57:38 -050088}
Hao Zhu8977a8a2015-11-19 16:52:21 -050089
Hao Zhue4ba9932017-06-07 12:45:43 -040090latex_contents_escape <- function(x) {
91 x <- gsub("\\\\", "\\\\\\\\", x)
Hao Zhu2a288402017-06-08 18:23:32 -040092 x <- gsub("\\$", "\\\\\\$", x)
Hao Zhue4ba9932017-06-07 12:45:43 -040093 x <- gsub("\\(", "\\\\(", x)
94 x <- gsub("\\)", "\\\\)", x)
95 x <- gsub("\\[", "\\\\]", x)
96 x <- gsub("\\[", "\\\\]", x)
97}
98
Hao Zhu8977a8a2015-11-19 16:52:21 -050099#' Magic Mirror for html table --------
100#'
Hao Zhuf7994dd2017-02-27 16:58:42 -0500101#' @param kable_input The output of kable
102magic_mirror_html <- function(kable_input){
103 kable_info <- list()
104 kable_xml <- read_xml(as.character(kable_input))
Hao Zhu8977a8a2015-11-19 16:52:21 -0500105 # Caption
Hao Zhuf7994dd2017-02-27 16:58:42 -0500106 kable_info$caption <- xml_text(xml_child(kable_xml, "caption"))
Hao Zhu8977a8a2015-11-19 16:52:21 -0500107 # Contents
Hao Zhu74eb6ad2017-03-04 09:32:37 -0500108 # kable_info$contents <- html_table(read_html(as.character(kable_input)))[[1]]
Hao Zhu8977a8a2015-11-19 16:52:21 -0500109 # colnames
Hao Zhuf7994dd2017-02-27 16:58:42 -0500110 kable_info$colnames <- lapply(xml_children(xml_child(kable_xml, "thead")),
111 xml_children)
112 kable_info$colnames <- kable_info$colnames[[length(kable_info$colnames)]]
113 kable_info$colnames <- trimws(xml_text(kable_info$colnames))
Hao Zhu8977a8a2015-11-19 16:52:21 -0500114 kable_info$ncol <- length(kable_info$colnames)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500115 kable_info$nrow_header <- length(xml_children(xml_child(kable_xml, "thead")))
116 kable_info$nrow_body <- nrow(kable_info$contents)
117 kable_info$table_class <- xml_attr(kable_xml, "class")
118 kable_info$table_style <- xml_attr(kable_xml, "style")
Hao Zhu8977a8a2015-11-19 16:52:21 -0500119 return(kable_info)
120}
121
Hao Zhu26234122017-02-22 15:34:33 -0500122