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