blob: 999518063e0ad0b53ca3dde10b0ec310296f23db [file] [log] [blame]
Hao Zhudb04e302015-11-15 16:57:38 -05001#' Magic mirror that returns kable's attributes
2#'
3#' @param input The output of kable
Hao Zhu8977a8a2015-11-19 16:52:21 -05004#' @importFrom knitr kable
5#' @import stringr
Hao Zhudb04e302015-11-15 16:57:38 -05006#' @export
7
8magic_mirror <- function(input){
Hao Zhu4adea852015-11-16 16:38:34 -05009 if(!"knitr_kable" %in% attr(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 }
13 kable_format <- attr(input, "format")
14 if (kable_format == "latex"){
Hao Zhu4adea852015-11-16 16:38:34 -050015 kable_info <- magic_mirror_latex(input)
Hao Zhudb04e302015-11-15 16:57:38 -050016 }
17 if (kable_format == "html"){
Hao Zhu4adea852015-11-16 16:38:34 -050018 kable_info <- magic_mirror_html(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 --------------
24#' @param input The output of kable
Hao Zhudb04e302015-11-15 16:57:38 -050025magic_mirror_latex <- function(input){
Hao Zhu4adea852015-11-16 16:38:34 -050026 kable_info <- list(tabular = NULL, booktabs = NULL, align = NULL,
27 ncol=NULL, nrow=NULL, colnames = NULL, rownames = NULL,
28 caption = NULL, contents = NULL)
29 # Tabular
30 kable_info$tabular <- ifelse(
31 grepl("\\\\begin\\{tabular\\}", input),
32 "tabular", "longtable"
Hao Zhudb04e302015-11-15 16:57:38 -050033 )
Hao Zhu4adea852015-11-16 16:38:34 -050034 # Booktabs
35 kable_info$booktabs <- ifelse(grepl("\\\\toprule", input), TRUE, FALSE)
36 # Align
37 kable_info$align <- gsub("\\|", "", str_match(
Hao Zhudc4b7142015-11-19 10:37:53 -050038 input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2])
Hao Zhu4adea852015-11-16 16:38:34 -050039 # N of columns
40 kable_info$ncol <- nchar(kable_info$align)
Hao Zhu4adea852015-11-16 16:38:34 -050041 # Caption
42 kable_info$caption <- str_match(input, "caption\\{(.*?)\\}")[2]
Hao Zhudc4b7142015-11-19 10:37:53 -050043 # N of rows
44 kable_info$nrow <- str_count(input, "\\\\\n") -
45 # in the dev version (currently as of 11.2015) of knitr, when longtable is
46 # enabled, caption is moved inside the tabular environment. As a result,
47 # the number of rows should be adjusted.
48 ifelse(
49 kable_info$tabular == "longtable" & !is.na(kable_info$caption) &
50 !str_detect(input, "\\\\begin\\{table\\}\\n\\n\\\\caption"),
51 1,0
52 )
Hao Zhu4adea852015-11-16 16:38:34 -050053 # Contents
54 kable_info$contents <- str_match_all(input, "\n(.*)\\\\\\\\")[[1]][,2]
55 # Column names
56 kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]]
57 # Row names
58 kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*")
59 return(kable_info)
Hao Zhudb04e302015-11-15 16:57:38 -050060}
Hao Zhu8977a8a2015-11-19 16:52:21 -050061
62#' Magic Mirror for html table --------
63#'
64#' @param input The output of kable
65#'
66#' @importFrom XML readHTMLTable
67magic_mirror_html <- function(input){
68 kable_info <- list(table.attr = NULL, align = NULL,
69 ncol=NULL, nrow=NULL, colnames = NULL, rownames = NULL,
70 caption = NULL, contents = NULL)
71 kable_data <- readHTMLTable(input[1])
72 # Caption
73 kable_info$caption <- names(kable_data)
74 # Contents
75 kable_info$contents <- kable_data[[1]]
76 # colnames
77 kable_info$colnames <- str_replace_all(
78 str_trim(names(kable_data[[1]])), "V[0-9]{1,2}", ""
79 )
80 # rownames
81 kable_info$rownames <- as.character(kable_data[[1]][,1])
82 if(str_trim(names(kable_data[[1]])[1]) != "V1"){
83 kable_info$rownames <- c(str_trim(names(kable_data[[1]])[1]),
84 kable_info$rownames)}
85 # ncol
86 kable_info$ncol <- length(kable_info$colnames)
87 # nrow
88 kable_info$nrow <- length(kable_info$rownames)
89 # table.attr
90 kable_info$table.attr <- str_match(input, "<table class = '(.*)'>")[2]
91 # align
92 kable_info$align <- str_match_all(
93 input, 'style=\\"text-align:([^;]*);'
94 )[[1]][,2]
95 kable_info$align <- paste0(
96 str_extract(tail(kable_info$align, kable_info$ncol), "."), collapse = ""
97 )
98 return(kable_info)
99}
100
101#' @export
102magrittr::`%>%`
103