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 | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 17 | add_footnote <- function(input, label = NULL, notation = "alphabet", |
| 18 | threeparttable = F) { |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 19 | if (is.null(label)){return(input)} |
| 20 | # Define available id list |
| 21 | if (!notation %in% c("number", "alphabet", "symbol")){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 22 | warning('Please select your notation within "number", "alphabet" and ', |
| 23 | '"symbol". Now add_footnote is using "alphabet" as default.') |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 24 | } |
| 25 | if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))} |
| 26 | ids.ops <- data.frame( |
| 27 | number = as.character(1:20), |
| 28 | alphabet = letters[1:20], |
| 29 | symbol.latex = c( |
| 30 | "*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P", |
| 31 | "**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P", |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 32 | "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag", |
| 33 | "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P", |
| 34 | "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag", |
| 35 | "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P" |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 36 | ), |
| 37 | symbol.html = c( |
| 38 | "*", "†", "‡", "§", "¶", |
| 39 | "**", "††", "‡‡", "§§", "¶¶", |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 40 | "*", "†††", "‡‡‡", |
| 41 | "§§§", "¶¶¶", |
| 42 | "**", "††††", "‡‡‡‡", |
| 43 | "§§§§", "¶¶¶¶" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 44 | ), |
| 45 | symbol.markdown = c( |
| 46 | "\\*", "†", "‡", "§", "¶", |
| 47 | "\\*\\*", "††", "‡‡", "§§", "¶¶", |
| 48 | "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶", |
| 49 | "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶" |
| 50 | ), |
| 51 | symbol.pandoc = c( |
| 52 | "\\*", "†", "‡", "§", "¶", |
| 53 | "\\*\\*", "††", "‡‡", "§§", "¶¶", |
| 54 | "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶", |
| 55 | "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶" |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 56 | ) |
| 57 | ) |
| 58 | ids <- ids.ops[,notation] |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 59 | # pandoc cannot recognize ^*^ as * is a special character. We have to use ^\*^ |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 60 | ids.intable <- gsub("\\*", "\\\\*", ids) |
| 61 | |
| 62 | #count the number of items in label and intable notation |
| 63 | count.label = length(label) |
| 64 | count.intablenoot = sum(str_count(input, "\\[note\\]")) |
| 65 | if (count.intablenoot != 0 & count.label != count.intablenoot){ |
| 66 | warning(paste("You entered", count.label, "labels but you put", |
| 67 | count.intablenoot, "[note] in your table.")) |
| 68 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 69 | |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 70 | export <- input |
| 71 | |
| 72 | # Footnote solution for markdown and pandoc. It is not perfect as |
| 73 | # markdown doesn't support complex table formats but this solution |
| 74 | # should be able to satisfy people who don't want to spend extra |
| 75 | # time to define their `kable` output. |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 76 | if(!attr(input, "format") %in% c("html", "latex")){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 77 | # In table notation |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 78 | if(count.intablenoot != 0){ |
| 79 | for(i in 1:count.intablenoot){ |
| 80 | export[which(str_detect(export, "\\[note\\]"))[1]] <- |
| 81 | sub("\\[note\\]", paste0("^", ids.intable[i], "^", |
| 82 | paste0(rep(" ", 4 - nchar(as.character(ids[i]))), |
| 83 | collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]]) |
| 84 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 85 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 86 | # Fix extra in table notation |
| 87 | extra.notation <- as.numeric( |
| 88 | str_extract( |
| 89 | str_extract_all( |
| 90 | paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]" |
| 91 | )[[1]], |
| 92 | "[0-9]{1,2}")) |
| 93 | for(i in extra.notation){ |
| 94 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 95 | paste0("^", ids.intable[i], "^", |
| 96 | paste0(rep(" ", 4 - nchar(as.character(ids[i]))), |
| 97 | collapse = "")), |
| 98 | export) |
| 99 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 100 | |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 101 | export[length(export)+1] <- "" |
| 102 | export[length(export)+1] <- "__Note:__" |
| 103 | export[length(export)+1] <- paste0( |
| 104 | paste0("^", ids[1:length(label)], "^ ", label), collapse = " " |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | # Generate latex table footnote -------------------------------- |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 109 | if(attr(input, "format")=="latex"){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 110 | kable_info <- magic_mirror(input) |
| 111 | if(threeparttable == F | latex.tabular == "longtable"){ |
| 112 | |
| 113 | } |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 114 | # If longtable is used, then use page footnote instead of threeparttable |
| 115 | # as it makes more sense to see the footnote at the bottom of page if |
| 116 | # table is longer than one page. |
| 117 | if(grepl("\\\\begin\\{longtable\\}", input)){ |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 118 | for(i in 1:count.intablenoot){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 119 | export <- sub("\\[note\\]", |
| 120 | paste0("\\\\footnote[", ids[i], "]{", label[i], "}"), export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 121 | } |
| 122 | }else{ |
| 123 | # Regular cases other than longtable |
| 124 | # generate footer with appropriate symbol |
| 125 | footer <- "" |
| 126 | for(i in 1:count.label){ |
| 127 | footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n") |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 130 | # Replace in-table notation with appropriate symbol |
| 131 | for(i in 1:count.intablenoot){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 132 | export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 133 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 134 | |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 135 | if(grepl("\\\\caption\\{.*?\\}", export)){ |
| 136 | export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 137 | }else{ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame^] | 138 | export <- sub("\\\\begin\\{tabular\\}", |
| 139 | "\\\\begin{threeparttable}\n\\\\begin{tabular}", export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 140 | } |
| 141 | export <- gsub( |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 142 | "\\\\end\\{tabular\\}", |
| 143 | paste0( |
| 144 | "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n", |
| 145 | footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 146 | ), |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 147 | export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 148 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 149 | } |
| 150 | if(attr(input, "format")=="html"){ |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 151 | } |
| 152 | return(export) |
| 153 | } |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 154 | |
| 155 | |
| 156 | |