Function magic_mirror and updated READ.ME
diff --git a/R/footnote.R b/R/footnote.R
index 8724141..e35a9f1 100644
--- a/R/footnote.R
+++ b/R/footnote.R
@@ -14,11 +14,13 @@
#' "number", "alphabet" and "symbol".
#'
#' @export
-add_footnote <- function(input, label = NULL, notation = "alphabet", threeparttable = F) {
+add_footnote <- function(input, label = NULL, notation = "alphabet",
+ threeparttable = F) {
if (is.null(label)){return(input)}
# Define available id list
if (!notation %in% c("number", "alphabet", "symbol")){
- warning('Please select your notation within "number", "alphabet" and "symbol". Now add_footnote is using "alphabet" as default.')
+ warning('Please select your notation within "number", "alphabet" and ',
+ '"symbol". Now add_footnote is using "alphabet" as default.')
}
if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))}
ids.ops <- data.frame(
@@ -27,14 +29,18 @@
symbol.latex = c(
"*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P",
"**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P",
- "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag", "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P",
- "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag", "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P"
+ "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag",
+ "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P",
+ "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag",
+ "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P"
),
symbol.html = c(
"*", "†", "‡", "§", "¶",
"**", "††", "‡‡", "§§", "¶¶",
- "*", "†††", "‡‡‡", "§§§", "¶¶¶",
- "**", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
+ "*", "†††", "‡‡‡",
+ "§§§", "¶¶¶",
+ "**", "††††", "‡‡‡‡",
+ "§§§§", "¶¶¶¶"
),
symbol.markdown = c(
"\\*", "†", "‡", "§", "¶",
@@ -50,6 +56,7 @@
)
)
ids <- ids.ops[,notation]
+ # pandoc cannot recognize ^*^ as * is a special character. We have to use ^\*^
ids.intable <- gsub("\\*", "\\\\*", ids)
#count the number of items in label and intable notation
@@ -60,8 +67,14 @@
count.intablenoot, "[note] in your table."))
}
+ export <- input
+
+ # Footnote solution for markdown and pandoc. It is not perfect as
+ # markdown doesn't support complex table formats but this solution
+ # should be able to satisfy people who don't want to spend extra
+ # time to define their `kable` output.
if(!attr(input, "format") %in% c("html", "latex")){
- export <- input
+ # In table notation
if(count.intablenoot != 0){
for(i in 1:count.intablenoot){
export[which(str_detect(export, "\\[note\\]"))[1]] <-
@@ -70,6 +83,20 @@
collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]])
}
}
+ # Fix extra in table notation
+ extra.notation <- as.numeric(
+ str_extract(
+ str_extract_all(
+ paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
+ )[[1]],
+ "[0-9]{1,2}"))
+ for(i in extra.notation){
+ export <- gsub(paste0("\\[note", i, "\\]"),
+ paste0("^", ids.intable[i], "^",
+ paste0(rep(" ", 4 - nchar(as.character(ids[i]))),
+ collapse = "")),
+ export)
+ }
export[length(export)+1] <- ""
export[length(export)+1] <- "__Note:__"
@@ -80,13 +107,17 @@
# Generate latex table footnote --------------------------------
if(attr(input, "format")=="latex"){
+ kable_info <- magic_mirror(input)
+ if(threeparttable == F | latex.tabular == "longtable"){
+
+ }
# If longtable is used, then use page footnote instead of threeparttable
# as it makes more sense to see the footnote at the bottom of page if
# table is longer than one page.
if(grepl("\\\\begin\\{longtable\\}", input)){
-
for(i in 1:count.intablenoot){
- input <- sub("\\[note\\]", paste0("\\\\footnote[", ids[i], "]{", label[i], "}"), input)
+ export <- sub("\\[note\\]",
+ paste0("\\\\footnote[", ids[i], "]{", label[i], "}"), export)
}
}else{
# Regular cases other than longtable
@@ -98,13 +129,14 @@
# Replace in-table notation with appropriate symbol
for(i in 1:count.intablenoot){
- input <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), input)
+ export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), export)
}
- if(grepl("\\\\caption\\{.*?\\}", input)){
- export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", input)
+ if(grepl("\\\\caption\\{.*?\\}", export)){
+ export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export)
}else{
- export <- sub("\\\\begin\\{tabular\\}", "\\\\begin{threeparttable}\n\\\\begin{tabular}", input)
+ export <- sub("\\\\begin\\{tabular\\}",
+ "\\\\begin{threeparttable}\n\\\\begin{tabular}", export)
}
export <- gsub(
"\\\\end\\{tabular\\}",
@@ -116,7 +148,6 @@
}
}
if(attr(input, "format")=="html"){
- export <- input
}
return(export)
}
diff --git a/R/magic_mirror.R b/R/magic_mirror.R
index 0fa726c..af77b50 100644
--- a/R/magic_mirror.R
+++ b/R/magic_mirror.R
@@ -5,24 +5,46 @@
#' @export
magic_mirror <- function(input){
- if(!"knitr_kable" %in% attr(input, "format")){
+ 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"){
- magic_mirror_latex(input)
+ kable_info <- magic_mirror_latex(input)
}
if (kable_format == "html"){
- magic_mirror_html(input)
+ kable_info <- magic_mirror_html(input)
}
+ return(kable_info)
}
#' Magic mirror for latex tables
magic_mirror_latex <- function(input){
- # kable will put a begin{table} shell if caption is not NULL
- caption <- ifelse(
- str_detect(input, "\\\\caption\\{.*?\\}"),
- str_match(input, "caption\\{(.*?)\\}")[2], NULL
+ kable_info <- list(tabular = NULL, booktabs = NULL, align = NULL,
+ ncol=NULL, nrow=NULL, colnames = NULL, rownames = NULL,
+ caption = NULL, contents = NULL)
+ # Tabular
+ kable_info$tabular <- ifelse(
+ grepl("\\\\begin\\{tabular\\}", input),
+ "tabular", "longtable"
)
+ # Booktabs
+ kable_info$booktabs <- ifelse(grepl("\\\\toprule", input), TRUE, FALSE)
+ # Align
+ kable_info$align <- gsub("\\|", "", str_match(
+ input, paste0("\\\\begin\\{", kable_info$tabular,"\\}\\{(.*?)\\}"))[2])
+ # N of columns
+ kable_info$ncol <- nchar(kable_info$align)
+ # N of rows
+ kable_info$nrow <- str_count(input, "\\\\\n")
+ # Caption
+ kable_info$caption <- str_match(input, "caption\\{(.*?)\\}")[2]
+ # Contents
+ kable_info$contents <- str_match_all(input, "\n(.*)\\\\\\\\")[[1]][,2]
+ # Column names
+ kable_info$colnames <- str_split(kable_info$contents[1], " \\& ")[[1]]
+ # Row names
+ kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*")
+ return(kable_info)
}