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 | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame^] | 17 | add_footnote <- function(input, label = NULL, notation = "alphabet", escape = T, |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 18 | threeparttable = F) { |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 19 | if (is.null(label)){return(input)} |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame^] | 20 | |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 21 | # Define available id list |
| 22 | if (!notation %in% c("number", "alphabet", "symbol")){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 23 | warning('Please select your notation within "number", "alphabet" and ', |
| 24 | '"symbol". Now add_footnote is using "alphabet" as default.') |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 25 | } |
| 26 | if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))} |
| 27 | ids.ops <- data.frame( |
| 28 | number = as.character(1:20), |
| 29 | alphabet = letters[1:20], |
| 30 | symbol.latex = c( |
| 31 | "*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P", |
| 32 | "**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P", |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 33 | "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag", |
| 34 | "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P", |
| 35 | "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag", |
| 36 | "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P" |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 37 | ), |
| 38 | symbol.html = c( |
| 39 | "*", "†", "‡", "§", "¶", |
| 40 | "**", "††", "‡‡", "§§", "¶¶", |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 41 | "*", "†††", "‡‡‡", |
| 42 | "§§§", "¶¶¶", |
| 43 | "**", "††††", "‡‡‡‡", |
| 44 | "§§§§", "¶¶¶¶" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 45 | ), |
| 46 | symbol.markdown = c( |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame^] | 47 | "\\*", "\u2020", "\u2021", "\u00A7", "\u00B6", |
| 48 | "\\*\\*", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6", |
| 49 | "\\*\\*\\*", "\u2020\u2020\u2020", "\u2021\u2021\u2021", |
| 50 | "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6", |
| 51 | "\\*\\*\\*\\*", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021", |
| 52 | "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6" |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 53 | ), |
| 54 | symbol.pandoc = c( |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame^] | 55 | "\\*", "\u2020", "\u2021", "\u00A7", "\u00B6", |
| 56 | "\\*\\*", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6", |
| 57 | "\\*\\*\\*", "\u2020\u2020\u2020", "\u2021\u2021\u2021", |
| 58 | "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6", |
| 59 | "\\*\\*\\*\\*", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021", |
| 60 | "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6" |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 61 | ) |
| 62 | ) |
| 63 | ids <- ids.ops[,notation] |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 64 | # pandoc cannot recognize ^*^ as * is a special character. We have to use ^\*^ |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 65 | ids.intable <- gsub("\\*", "\\\\*", ids) |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 66 | ids.simple <- c( |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame^] | 67 | "*", "\u2020", "\u2021", "\u00A7", "\u00B6", |
| 68 | "**", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6", |
| 69 | "***", "\u2020\u2020\u2020", "\u2021\u2021\u2021", |
| 70 | "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6", |
| 71 | "****", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021", |
| 72 | "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6" |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 73 | ) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 74 | |
| 75 | #count the number of items in label and intable notation |
| 76 | count.label = length(label) |
| 77 | count.intablenoot = sum(str_count(input, "\\[note\\]")) |
| 78 | if (count.intablenoot != 0 & count.label != count.intablenoot){ |
| 79 | warning(paste("You entered", count.label, "labels but you put", |
| 80 | count.intablenoot, "[note] in your table.")) |
| 81 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 82 | |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 83 | export <- input |
| 84 | |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 85 | # Find out if there are any extra in-table notations needed to be corrected |
| 86 | extra.notation <- as.numeric( |
| 87 | str_extract( |
| 88 | str_extract_all( |
| 89 | paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]" |
| 90 | )[[1]], |
| 91 | "[0-9]{1,2}")) |
| 92 | |
| 93 | |
| 94 | |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 95 | # Footnote solution for markdown and pandoc. It is not perfect as |
| 96 | # markdown doesn't support complex table formats but this solution |
| 97 | # should be able to satisfy people who don't want to spend extra |
| 98 | # time to define their `kable` output. |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 99 | if(!attr(input, "format") %in% c("html", "latex")){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 100 | # In table notation |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 101 | if(count.intablenoot != 0){ |
| 102 | for(i in 1:count.intablenoot){ |
| 103 | export[which(str_detect(export, "\\[note\\]"))[1]] <- |
| 104 | sub("\\[note\\]", paste0("^", ids.intable[i], "^", |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 105 | paste0(rep(" ", 4 - ceiling(i/5)), |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 106 | collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]]) |
| 107 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 108 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 109 | # Fix extra in table notation |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 110 | for(i in extra.notation){ |
| 111 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 112 | paste0("^", ids.intable[i], "^", |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 113 | paste0(rep(" ", 4 - ceiling(i/5)), |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 114 | collapse = "")), |
| 115 | export) |
| 116 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 117 | |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 118 | export[length(export)+1] <- "" |
| 119 | export[length(export)+1] <- "__Note:__" |
| 120 | export[length(export)+1] <- paste0( |
| 121 | paste0("^", ids[1:length(label)], "^ ", label), collapse = " " |
| 122 | ) |
| 123 | } |
| 124 | |
| 125 | # Generate latex table footnote -------------------------------- |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 126 | if(attr(input, "format")=="latex"){ |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame^] | 127 | # Clean the entry for labels when escape is enabled |
| 128 | if (escape = T){label <- knitr:::escape_latex(label)} |
| 129 | |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 130 | kable_info <- magic_mirror(input) |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 131 | if(kable_info$tabular == "longtable"){ |
| 132 | if(notation != "number"){ |
| 133 | warning("Currently, if you enabled longtable in kable, you can only use", |
| 134 | " number as your footnote notations. ") |
| 135 | } |
| 136 | if(threeparttable == T){ |
| 137 | warning("Currently, threeparttable does not support longtable.") |
| 138 | } |
| 139 | # If longtable is used, then use page footnote instead of threeparttable |
| 140 | # as it makes more sense to see the footnote at the bottom of page if |
| 141 | # table is longer than one page. |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 142 | |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 143 | # Longtable doesn't support footnote in caption directly. |
| 144 | # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions |
| 145 | count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]") |
| 146 | if (count.in.caption.note != 0){ |
| 147 | # Since caption is the first part of table, we can just start |
| 148 | caption.footnote <- paste0("\\\\addtocounter{footnote}{-", count.in.caption.note, "}") |
| 149 | for(i in 1:count.in.caption.note){ |
| 150 | export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export) |
| 151 | caption.footnote <- paste0( |
| 152 | caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", label[i], "}" |
| 153 | ) |
| 154 | } |
| 155 | |
| 156 | if (str_detect(export, "\\\\toprule")){ |
| 157 | export <- sub("\\\\toprule", |
| 158 | paste0("\\\\toprule\n", caption.footnote), export) |
| 159 | }else{ |
| 160 | export <- sub("\\\\hline", |
| 161 | paste0("\\\\hline\n", caption.footnote), export) |
| 162 | } |
| 163 | } |
| 164 | for(i in (count.in.caption.note + 1):count.intablenoot){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 165 | export <- sub("\\[note\\]", |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 166 | paste0("\\\\footnote[", i, "]{", label[i], "}"), export) |
| 167 | } |
| 168 | for(i in extra.notation){ |
| 169 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 170 | paste0("\\\\footnotemark[", i, "]"), |
| 171 | export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 172 | } |
| 173 | }else{ |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 174 | # Replace in-table notation with appropriate symbol |
| 175 | for(i in 1:count.intablenoot){ |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 176 | export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids.intable[i], "}"), export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 177 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 178 | |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 179 | # Fix extra in table notation |
| 180 | for(i in extra.notation){ |
| 181 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 182 | paste0("\\\\textsuperscript{", ids.intable[i], "}"), |
| 183 | export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 184 | } |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 185 | if(threeparttable == T){ |
| 186 | # generate footer with appropriate symbol |
| 187 | footer <- "" |
| 188 | for(i in 1:count.label){ |
| 189 | footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n") |
| 190 | } |
| 191 | |
| 192 | if(grepl("\\\\caption\\{.*?\\}", export)){ |
| 193 | export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export) |
| 194 | }else{ |
| 195 | export <- sub("\\\\begin\\{tabular\\}", |
| 196 | "\\\\begin{threeparttable}\n\\\\begin{tabular}", export) |
| 197 | } |
| 198 | export <- gsub( |
| 199 | "\\\\end\\{tabular\\}", |
| 200 | paste0( |
| 201 | "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n", |
| 202 | footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}" |
| 203 | ), |
| 204 | export) |
| 205 | }else{ |
| 206 | table.width <- max(nchar( |
| 207 | str_replace_all( |
| 208 | str_replace_all(kable_info$contents, "\\[note\\]", ""), |
| 209 | "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1) |
| 210 | footer <- "" |
| 211 | for (i in 1:count.label){ |
| 212 | label.wrap <- strwrap(label[i], table.width) |
| 213 | footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol, |
| 214 | "}{l}{\\\\textsuperscript{", ids[i], "} ", |
| 215 | label.wrap[1], "}\\\\\\\\\n") |
| 216 | if(length(label.wrap) > 1){ |
| 217 | for (j in 2:length(label.wrap)){ |
| 218 | footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol, |
| 219 | "}{l}{", label.wrap[j], "}\\\\\\\\\n") |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | export <- gsub("\\\\end\\{tabular\\}", |
| 224 | paste0(footer, "\\\\end{tabular}"), export) |
| 225 | } |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 226 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 227 | } |
| 228 | if(attr(input, "format")=="html"){ |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 229 | |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 230 | } |
| 231 | return(export) |
| 232 | } |