Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 1 | #' Add footnote |
| 2 | #' |
| 3 | #' @description Add footnote to your favorite kable output. So far this function |
| 4 | #' only works when you define \code{format} in your kable function or in the |
| 5 | #' global knitr option \code{knitr.table.format}. In latex, we are using the |
| 6 | #' \code{threeparttable} package so you need to import this package in your |
| 7 | #' \code{YAML} header. |
| 8 | #' |
| 9 | #' @param input The direct output of your \code{kable} function or your last |
| 10 | #' \code{kableExtra} function. |
| 11 | #' @param label A vector of footnotes you want to add. You don't need to add |
| 12 | #' notations in your notes. |
| 13 | #' @param notation You can select the format of your footnote notation from |
| 14 | #' "number", "alphabet" and "symbol". |
| 15 | #' |
| 16 | #' @export |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 17 | add_footnote <- function(input, label = NULL, notation = "alphabet", threeparttable = F) { |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 18 | if (is.null(label)){return(input)} |
| 19 | # Define available id list |
| 20 | if (!notation %in% c("number", "alphabet", "symbol")){ |
| 21 | warning('Please select your notation within "number", "alphabet" and "symbol". Now add_footnote is using "alphabet" as default.') |
| 22 | } |
| 23 | if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))} |
| 24 | ids.ops <- data.frame( |
| 25 | number = as.character(1:20), |
| 26 | alphabet = letters[1:20], |
| 27 | symbol.latex = c( |
| 28 | "*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P", |
| 29 | "**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P", |
| 30 | "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag", "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P", |
| 31 | "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag", "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P" |
| 32 | ), |
| 33 | symbol.html = c( |
| 34 | "*", "†", "‡", "§", "¶", |
| 35 | "**", "††", "‡‡", "§§", "¶¶", |
| 36 | "*", "†††", "‡‡‡", "§§§", "¶¶¶", |
| 37 | "**", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 38 | ), |
| 39 | symbol.markdown = c( |
| 40 | "\\*", "†", "‡", "§", "¶", |
| 41 | "\\*\\*", "††", "‡‡", "§§", "¶¶", |
| 42 | "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶", |
| 43 | "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶" |
| 44 | ), |
| 45 | symbol.pandoc = c( |
| 46 | "\\*", "†", "‡", "§", "¶", |
| 47 | "\\*\\*", "††", "‡‡", "§§", "¶¶", |
| 48 | "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶", |
| 49 | "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶" |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 50 | ) |
| 51 | ) |
| 52 | ids <- ids.ops[,notation] |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 53 | ids.intable <- gsub("\\*", "\\\\*", ids) |
| 54 | |
| 55 | #count the number of items in label and intable notation |
| 56 | count.label = length(label) |
| 57 | count.intablenoot = sum(str_count(input, "\\[note\\]")) |
| 58 | if (count.intablenoot != 0 & count.label != count.intablenoot){ |
| 59 | warning(paste("You entered", count.label, "labels but you put", |
| 60 | count.intablenoot, "[note] in your table.")) |
| 61 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 62 | |
| 63 | if(!attr(input, "format") %in% c("html", "latex")){ |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 64 | export <- input |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 65 | if(count.intablenoot != 0){ |
| 66 | for(i in 1:count.intablenoot){ |
| 67 | export[which(str_detect(export, "\\[note\\]"))[1]] <- |
| 68 | sub("\\[note\\]", paste0("^", ids.intable[i], "^", |
| 69 | paste0(rep(" ", 4 - nchar(as.character(ids[i]))), |
| 70 | collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]]) |
| 71 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 74 | export[length(export)+1] <- "" |
| 75 | export[length(export)+1] <- "__Note:__" |
| 76 | export[length(export)+1] <- paste0( |
| 77 | paste0("^", ids[1:length(label)], "^ ", label), collapse = " " |
| 78 | ) |
| 79 | } |
| 80 | |
| 81 | # Generate latex table footnote -------------------------------- |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 82 | if(attr(input, "format")=="latex"){ |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 83 | # If longtable is used, then use page footnote instead of threeparttable |
| 84 | # as it makes more sense to see the footnote at the bottom of page if |
| 85 | # table is longer than one page. |
| 86 | if(grepl("\\\\begin\\{longtable\\}", input)){ |
| 87 | |
| 88 | for(i in 1:count.intablenoot){ |
| 89 | input <- sub("\\[note\\]", paste0("\\\\footnote[", ids[i], "]{", label[i], "}"), input) |
| 90 | } |
| 91 | }else{ |
| 92 | # Regular cases other than longtable |
| 93 | # generate footer with appropriate symbol |
| 94 | footer <- "" |
| 95 | for(i in 1:count.label){ |
| 96 | footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n") |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 99 | # Replace in-table notation with appropriate symbol |
| 100 | for(i in 1:count.intablenoot){ |
| 101 | input <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), input) |
| 102 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 103 | |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 104 | if(grepl("\\\\caption\\{.*?\\}", input)){ |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 105 | export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", input) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 106 | }else{ |
| 107 | export <- sub("\\\\begin\\{tabular\\}", "\\\\begin{threeparttable}\n\\\\begin{tabular}", input) |
| 108 | } |
| 109 | export <- gsub( |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 110 | "\\\\end\\{tabular\\}", |
| 111 | paste0( |
| 112 | "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n", |
| 113 | footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 114 | ), |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 115 | export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 116 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 117 | } |
| 118 | if(attr(input, "format")=="html"){ |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 119 | export <- input |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 120 | } |
| 121 | return(export) |
| 122 | } |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 123 | |
| 124 | |
| 125 | |