blob: a926b2546ffbe5912532e3b8e9a752c4a31da3f6 [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#' @import stringr
Hao Zhudb04e302015-11-15 16:57:38 -05005#' @export
6
7magic_mirror <- function(input){
Hao Zhu4adea852015-11-16 16:38:34 -05008 if(!"knitr_kable" %in% attr(input, "class")){
Hao Zhudb04e302015-11-15 16:57:38 -05009 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 Zhu4adea852015-11-16 16:38:34 -050014 kable_info <- magic_mirror_latex(input)
Hao Zhudb04e302015-11-15 16:57:38 -050015 }
16 if (kable_format == "html"){
Hao Zhu4adea852015-11-16 16:38:34 -050017 kable_info <- magic_mirror_html(input)
Hao Zhudb04e302015-11-15 16:57:38 -050018 }
Hao Zhu4adea852015-11-16 16:38:34 -050019 return(kable_info)
Hao Zhudb04e302015-11-15 16:57:38 -050020}
21
Hao Zhu8977a8a2015-11-19 16:52:21 -050022#' Magic mirror for latex tables --------------
23#' @param input The output of kable
Hao Zhudb04e302015-11-15 16:57:38 -050024magic_mirror_latex <- function(input){
Hao Zhuc05e1812017-02-25 01:45:35 -050025 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 Zhu4adea852015-11-16 16:38:34 -050029 # 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
Hao Zhuc05e1812017-02-25 01:45:35 -050035 kable_info$booktabs <- grepl("\\\\toprule", input)
Hao Zhu4adea852015-11-16 16:38:34 -050036 # Align
37 kable_info$align <- gsub("\\|", "", str_match(
Hao Zhudc4b7142015-11-19 10:37:53 -050038 input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2])
Hao Zhuc05e1812017-02-25 01:45:35 -050039 # valign
40 kable_info$valign <- gsub("\\|", "", str_match(
41 input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2])
Hao Zhu4adea852015-11-16 16:38:34 -050042 # N of columns
43 kable_info$ncol <- nchar(kable_info$align)
Hao Zhu4adea852015-11-16 16:38:34 -050044 # Caption
45 kable_info$caption <- str_match(input, "caption\\{(.*?)\\}")[2]
Hao Zhudc4b7142015-11-19 10:37:53 -050046 # 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 Zhu4adea852015-11-16 16:38:34 -050056 # Contents
57 kable_info$contents <- str_match_all(input, "\n(.*)\\\\\\\\")[[1]][,2]
58 # Column names
59 kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]]
60 # Row names
61 kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*")
Hao Zhuc05e1812017-02-25 01:45:35 -050062
63 kable_info$centering <- grepl("\\\\centering", input)
64
65 kable_info$table_env <- (!is.na(kable_info$caption) &
66 kable_info$tabular != "longtable")
Hao Zhu4adea852015-11-16 16:38:34 -050067 return(kable_info)
Hao Zhudb04e302015-11-15 16:57:38 -050068}
Hao Zhu8977a8a2015-11-19 16:52:21 -050069
70#' Magic Mirror for html table --------
71#'
72#' @param input The output of kable
Hao Zhu8977a8a2015-11-19 16:52:21 -050073magic_mirror_html <- function(input){
74 kable_info <- list(table.attr = NULL, align = NULL,
Hao Zhuc05e1812017-02-25 01:45:35 -050075 ncol = NULL, nrow = NULL, colnames = NULL, rownames = NULL,
Hao Zhu8977a8a2015-11-19 16:52:21 -050076 caption = NULL, contents = NULL)
Hao Zhu26234122017-02-22 15:34:33 -050077 kable_data <- html_table(read_html(input))
Hao Zhu8977a8a2015-11-19 16:52:21 -050078 # Caption
79 kable_info$caption <- names(kable_data)
80 # Contents
81 kable_info$contents <- kable_data[[1]]
82 # colnames
83 kable_info$colnames <- str_replace_all(
84 str_trim(names(kable_data[[1]])), "V[0-9]{1,2}", ""
85 )
86 # rownames
87 kable_info$rownames <- as.character(kable_data[[1]][,1])
88 if(str_trim(names(kable_data[[1]])[1]) != "V1"){
89 kable_info$rownames <- c(str_trim(names(kable_data[[1]])[1]),
90 kable_info$rownames)}
91 # ncol
92 kable_info$ncol <- length(kable_info$colnames)
93 # nrow
94 kable_info$nrow <- length(kable_info$rownames)
95 # table.attr
96 kable_info$table.attr <- str_match(input, "<table class = '(.*)'>")[2]
97 # align
98 kable_info$align <- str_match_all(
99 input, 'style=\\"text-align:([^;]*);'
100 )[[1]][,2]
101 kable_info$align <- paste0(
102 str_extract(tail(kable_info$align, kable_info$ncol), "."), collapse = ""
103 )
104 return(kable_info)
105}
106
Hao Zhu26234122017-02-22 15:34:33 -0500107
Hao Zhu8977a8a2015-11-19 16:52:21 -0500108