| #' Magic mirror that returns kable's attributes |
| #' |
| #' @param input The output of kable |
| #' @import stringr |
| #' @export |
| |
| magic_mirror <- function(input){ |
| if(!"knitr_kable" %in% attr(input, "class")){ |
| warning("magic_mirror may not be able to produce correct result if the", |
| " input table is not rendered by knitr::kable. ") |
| } |
| kable_format <- attr(input, "format") |
| if (kable_format == "latex"){ |
| kable_info <- magic_mirror_latex(input) |
| } |
| if (kable_format == "html"){ |
| kable_info <- magic_mirror_html(input) |
| } |
| return(kable_info) |
| } |
| |
| #' Magic mirror for latex tables -------------- |
| #' @param input The output of kable |
| magic_mirror_latex <- function(input){ |
| kable_info <- list(tabular = NULL, booktabs = FALSE, align = NULL, |
| valign = NULL, ncol = NULL, nrow = NULL, colnames = NULL, |
| rownames = NULL, caption = NULL, contents = NULL, |
| centering = FALSE, table_env = FALSE) |
| # Tabular |
| kable_info$tabular <- ifelse( |
| grepl("\\\\begin\\{tabular\\}", input), |
| "tabular", "longtable" |
| ) |
| # Booktabs |
| kable_info$booktabs <- grepl("\\\\toprule", input) |
| # Align |
| kable_info$align <- gsub("\\|", "", str_match( |
| input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2]) |
| # valign |
| kable_info$valign <- gsub("\\|", "", str_match( |
| input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2]) |
| # N of columns |
| kable_info$ncol <- nchar(kable_info$align) |
| # Caption |
| kable_info$caption <- str_match(input, "caption\\{(.*?)\\n")[2] |
| # N of rows |
| kable_info$nrow <- str_count(input, "\\\\\n") - |
| # in the dev version (currently as of 11.2015) of knitr, when longtable is |
| # enabled, caption is moved inside the tabular environment. As a result, |
| # the number of rows should be adjusted. |
| ifelse( |
| kable_info$tabular == "longtable" & !is.na(kable_info$caption) & |
| !str_detect(input, "\\\\begin\\{table\\}\\n\\n\\\\caption"), |
| 1,0 |
| ) |
| # Contents |
| kable_info$contents <- str_match_all(input, "\n(.*)\\\\\\\\")[[1]][,2] |
| if (kable_info$tabular == "longtable" & !is.na(kable_info$caption)) { |
| kable_info$contents <- kable_info$contents[-1] |
| } |
| # Column names |
| kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]] |
| # Row names |
| kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*") |
| |
| kable_info$centering <- grepl("\\\\centering", input) |
| |
| kable_info$table_env <- (!is.na(kable_info$caption) & |
| kable_info$tabular != "longtable") |
| return(kable_info) |
| } |
| |
| #' Magic Mirror for html table -------- |
| #' |
| #' @param input The output of kable |
| magic_mirror_html <- function(input){ |
| kable_info <- list(table.attr = NULL, align = NULL, |
| ncol = NULL, nrow = NULL, colnames = NULL, rownames = NULL, |
| caption = NULL, contents = NULL) |
| kable_data <- html_table(read_html(input)) |
| # Caption |
| kable_info$caption <- names(kable_data) |
| # Contents |
| kable_info$contents <- kable_data[[1]] |
| # colnames |
| kable_info$colnames <- str_replace_all( |
| str_trim(names(kable_data[[1]])), "V[0-9]{1,2}", "" |
| ) |
| # rownames |
| kable_info$rownames <- as.character(kable_data[[1]][,1]) |
| if(str_trim(names(kable_data[[1]])[1]) != "V1"){ |
| kable_info$rownames <- c(str_trim(names(kable_data[[1]])[1]), |
| kable_info$rownames)} |
| # ncol |
| kable_info$ncol <- length(kable_info$colnames) |
| # nrow |
| kable_info$nrow <- length(kable_info$rownames) |
| # table.attr |
| kable_info$table.attr <- str_match(input, "<table class = '(.*)'>")[2] |
| # align |
| kable_info$align <- str_match_all( |
| input, 'style=\\"text-align:([^;]*);' |
| )[[1]][,2] |
| kable_info$align <- paste0( |
| str_extract(tail(kable_info$align, kable_info$ncol), "."), collapse = "" |
| ) |
| return(kable_info) |
| } |
| |
| |
| |