blob: 9b88457d8f05b3b730060c046f1615731d675718 [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 Zhudb04e302015-11-15 16:57:38 -05006#' @export
7
Hao Zhuf7994dd2017-02-27 16:58:42 -05008magic_mirror <- function(kable_input){
9 if (!"knitr_kable" %in% attr(kable_input, "class")) {
Hao Zhudb04e302015-11-15 16:57:38 -050010 warning("magic_mirror may not be able to produce correct result if the",
11 " input table is not rendered by knitr::kable. ")
12 }
Hao Zhuf7994dd2017-02-27 16:58:42 -050013 kable_format <- attr(kable_input, "format")
14 if (kable_format == "latex") {
15 kable_info <- magic_mirror_latex(kable_input)
Hao Zhudb04e302015-11-15 16:57:38 -050016 }
Hao Zhuf7994dd2017-02-27 16:58:42 -050017 if (kable_format == "html") {
18 kable_info <- magic_mirror_html(kable_input)
Hao Zhudb04e302015-11-15 16:57:38 -050019 }
Hao Zhu4adea852015-11-16 16:38:34 -050020 return(kable_info)
Hao Zhudb04e302015-11-15 16:57:38 -050021}
22
Hao Zhu8977a8a2015-11-19 16:52:21 -050023#' Magic mirror for latex tables --------------
Hao Zhuf7994dd2017-02-27 16:58:42 -050024#' @param kable_input The output of kable
25magic_mirror_latex <- function(kable_input){
Hao Zhuc05e1812017-02-25 01:45:35 -050026 kable_info <- list(tabular = NULL, booktabs = FALSE, align = NULL,
27 valign = NULL, ncol = NULL, nrow = NULL, colnames = NULL,
28 rownames = NULL, caption = NULL, contents = NULL,
29 centering = FALSE, table_env = FALSE)
Hao Zhu4adea852015-11-16 16:38:34 -050030 # Tabular
31 kable_info$tabular <- ifelse(
Hao Zhuf7994dd2017-02-27 16:58:42 -050032 grepl("\\\\begin\\{tabular\\}", kable_input),
Hao Zhu4adea852015-11-16 16:38:34 -050033 "tabular", "longtable"
Hao Zhudb04e302015-11-15 16:57:38 -050034 )
Hao Zhu4adea852015-11-16 16:38:34 -050035 # Booktabs
Hao Zhuf7994dd2017-02-27 16:58:42 -050036 kable_info$booktabs <- grepl("\\\\toprule", kable_input)
Hao Zhu4adea852015-11-16 16:38:34 -050037 # Align
38 kable_info$align <- gsub("\\|", "", str_match(
Hao Zhuf7994dd2017-02-27 16:58:42 -050039 kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2])
Hao Zhuc05e1812017-02-25 01:45:35 -050040 # valign
41 kable_info$valign <- gsub("\\|", "", str_match(
Hao Zhuf7994dd2017-02-27 16:58:42 -050042 kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2])
Hao Zhu4adea852015-11-16 16:38:34 -050043 # N of columns
44 kable_info$ncol <- nchar(kable_info$align)
Hao Zhu4adea852015-11-16 16:38:34 -050045 # Caption
Hao Zhuf7994dd2017-02-27 16:58:42 -050046 kable_info$caption <- str_match(kable_input, "caption\\{(.*?)\\n")[2]
Hao Zhudc4b7142015-11-19 10:37:53 -050047 # N of rows
Hao Zhuf7994dd2017-02-27 16:58:42 -050048 kable_info$nrow <- str_count(kable_input, "\\\\\n") -
Hao Zhudc4b7142015-11-19 10:37:53 -050049 # in the dev version (currently as of 11.2015) of knitr, when longtable is
50 # enabled, caption is moved inside the tabular environment. As a result,
51 # the number of rows should be adjusted.
52 ifelse(
53 kable_info$tabular == "longtable" & !is.na(kable_info$caption) &
Hao Zhuf7994dd2017-02-27 16:58:42 -050054 !str_detect(kable_input, "\\\\begin\\{table\\}\\n\\n\\\\caption"),
Hao Zhudc4b7142015-11-19 10:37:53 -050055 1,0
56 )
Hao Zhu4adea852015-11-16 16:38:34 -050057 # Contents
Hao Zhuf7994dd2017-02-27 16:58:42 -050058 kable_info$contents <- str_match_all(kable_input, "\n(.*)\\\\\\\\")[[1]][,2]
Hao Zhua3fc0c42017-02-27 12:04:59 -050059 if (kable_info$tabular == "longtable" & !is.na(kable_info$caption)) {
60 kable_info$contents <- kable_info$contents[-1]
61 }
Hao Zhu4adea852015-11-16 16:38:34 -050062 # Column names
63 kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]]
64 # Row names
65 kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*")
Hao Zhuc05e1812017-02-25 01:45:35 -050066
Hao Zhuf7994dd2017-02-27 16:58:42 -050067 kable_info$centering <- grepl("\\\\centering", kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -050068
69 kable_info$table_env <- (!is.na(kable_info$caption) &
70 kable_info$tabular != "longtable")
Hao Zhu4adea852015-11-16 16:38:34 -050071 return(kable_info)
Hao Zhudb04e302015-11-15 16:57:38 -050072}
Hao Zhu8977a8a2015-11-19 16:52:21 -050073
74#' Magic Mirror for html table --------
75#'
Hao Zhuf7994dd2017-02-27 16:58:42 -050076#' @param kable_input The output of kable
77magic_mirror_html <- function(kable_input){
78 kable_info <- list()
79 kable_xml <- read_xml(as.character(kable_input))
Hao Zhu8977a8a2015-11-19 16:52:21 -050080 # Caption
Hao Zhuf7994dd2017-02-27 16:58:42 -050081 kable_info$caption <- xml_text(xml_child(kable_xml, "caption"))
Hao Zhu8977a8a2015-11-19 16:52:21 -050082 # Contents
Hao Zhuf7994dd2017-02-27 16:58:42 -050083 kable_info$contents <- html_table(read_html(as.character(kable_input)))[[1]]
Hao Zhu8977a8a2015-11-19 16:52:21 -050084 # colnames
Hao Zhuf7994dd2017-02-27 16:58:42 -050085 kable_info$colnames <- lapply(xml_children(xml_child(kable_xml, "thead")),
86 xml_children)
87 kable_info$colnames <- kable_info$colnames[[length(kable_info$colnames)]]
88 kable_info$colnames <- trimws(xml_text(kable_info$colnames))
Hao Zhu8977a8a2015-11-19 16:52:21 -050089 kable_info$ncol <- length(kable_info$colnames)
Hao Zhuf7994dd2017-02-27 16:58:42 -050090 kable_info$nrow_header <- length(xml_children(xml_child(kable_xml, "thead")))
91 kable_info$nrow_body <- nrow(kable_info$contents)
92 kable_info$table_class <- xml_attr(kable_xml, "class")
93 kable_info$table_style <- xml_attr(kable_xml, "style")
Hao Zhu8977a8a2015-11-19 16:52:21 -050094 return(kable_info)
95}
96
Hao Zhu26234122017-02-22 15:34:33 -050097
Hao Zhu8977a8a2015-11-19 16:52:21 -050098