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 | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 17 | add_footnote <- function(input, label = NULL, notation = "number", |
| 18 | threeparttable = FALSE) { |
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 | e0a36a8 | 2015-11-23 15:35:20 -0500 | [diff] [blame^] | 41 | "***", "†††", "‡‡‡", |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 42 | "§§§", "¶¶¶", |
Hao Zhu | e0a36a8 | 2015-11-23 15:35:20 -0500 | [diff] [blame^] | 43 | "****", "††††", "‡‡‡‡", |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 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 |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 86 | extra.notation <- unique(as.numeric( |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 87 | str_extract( |
| 88 | str_extract_all( |
| 89 | paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]" |
| 90 | )[[1]], |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 91 | "[0-9]{1,2}"))) |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 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 | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 127 | # Clean the entry for labels |
| 128 | label <- knitr:::escape_latex(label) |
| 129 | label <- gsub("\\\\", "\\\\\\\\", label) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 130 | |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 131 | kable_info <- magic_mirror(input) |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 132 | if(kable_info$tabular == "longtable"){ |
| 133 | if(notation != "number"){ |
| 134 | warning("Currently, if you enabled longtable in kable, you can only use", |
| 135 | " number as your footnote notations. ") |
| 136 | } |
| 137 | if(threeparttable == T){ |
| 138 | warning("Currently, threeparttable does not support longtable.") |
| 139 | } |
| 140 | # If longtable is used, then use page footnote instead of threeparttable |
| 141 | # as it makes more sense to see the footnote at the bottom of page if |
| 142 | # table is longer than one page. |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 143 | |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 144 | # Longtable doesn't support footnote in caption directly. |
| 145 | # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions |
| 146 | count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]") |
| 147 | if (count.in.caption.note != 0){ |
| 148 | # Since caption is the first part of table, we can just start |
| 149 | caption.footnote <- paste0("\\\\addtocounter{footnote}{-", count.in.caption.note, "}") |
| 150 | for(i in 1:count.in.caption.note){ |
| 151 | export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export) |
| 152 | caption.footnote <- paste0( |
| 153 | caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", label[i], "}" |
| 154 | ) |
| 155 | } |
| 156 | |
| 157 | if (str_detect(export, "\\\\toprule")){ |
| 158 | export <- sub("\\\\toprule", |
| 159 | paste0("\\\\toprule\n", caption.footnote), export) |
| 160 | }else{ |
| 161 | export <- sub("\\\\hline", |
| 162 | paste0("\\\\hline\n", caption.footnote), export) |
| 163 | } |
| 164 | } |
| 165 | for(i in (count.in.caption.note + 1):count.intablenoot){ |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 166 | export <- sub("\\[note\\]", |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 167 | paste0("\\\\footnote[", i, "]{", label[i], "}"), export) |
| 168 | } |
| 169 | for(i in extra.notation){ |
| 170 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 171 | paste0("\\\\footnotemark[", i, "]"), |
| 172 | export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 173 | } |
| 174 | }else{ |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 175 | # Replace in-table notation with appropriate symbol |
| 176 | for(i in 1:count.intablenoot){ |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 177 | export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids.intable[i], "}"), export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 178 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 179 | |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 180 | # Fix extra in table notation |
| 181 | for(i in extra.notation){ |
| 182 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 183 | paste0("\\\\textsuperscript{", ids.intable[i], "}"), |
| 184 | export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 185 | } |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 186 | if(threeparttable == T){ |
| 187 | # generate footer with appropriate symbol |
| 188 | footer <- "" |
| 189 | for(i in 1:count.label){ |
| 190 | footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n") |
| 191 | } |
| 192 | |
| 193 | if(grepl("\\\\caption\\{.*?\\}", export)){ |
| 194 | export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export) |
| 195 | }else{ |
| 196 | export <- sub("\\\\begin\\{tabular\\}", |
| 197 | "\\\\begin{threeparttable}\n\\\\begin{tabular}", export) |
| 198 | } |
| 199 | export <- gsub( |
| 200 | "\\\\end\\{tabular\\}", |
| 201 | paste0( |
| 202 | "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n", |
| 203 | footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}" |
| 204 | ), |
| 205 | export) |
| 206 | }else{ |
| 207 | table.width <- max(nchar( |
| 208 | str_replace_all( |
| 209 | str_replace_all(kable_info$contents, "\\[note\\]", ""), |
| 210 | "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1) |
| 211 | footer <- "" |
| 212 | for (i in 1:count.label){ |
| 213 | label.wrap <- strwrap(label[i], table.width) |
| 214 | footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol, |
| 215 | "}{l}{\\\\textsuperscript{", ids[i], "} ", |
| 216 | label.wrap[1], "}\\\\\\\\\n") |
| 217 | if(length(label.wrap) > 1){ |
| 218 | for (j in 2:length(label.wrap)){ |
| 219 | footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol, |
| 220 | "}{l}{", label.wrap[j], "}\\\\\\\\\n") |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | export <- gsub("\\\\end\\{tabular\\}", |
| 225 | paste0(footer, "\\\\end{tabular}"), export) |
| 226 | } |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 227 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 228 | } |
| 229 | if(attr(input, "format")=="html"){ |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 230 | # Clean the entry for labels |
| 231 | label <- knitr:::escape_html(label) |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 232 | |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 233 | # Replace in-table notation with appropriate symbol |
| 234 | for(i in 1:count.intablenoot){ |
| 235 | export <- sub("\\[note\\]", paste0("<sup>", ids.intable[i], "</sup>"), export) |
| 236 | } |
| 237 | |
| 238 | # Fix extra in table notation |
| 239 | for(i in extra.notation){ |
| 240 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 241 | paste0("<sup>", ids.intable[i], "</sup>"), |
| 242 | export) |
| 243 | } |
| 244 | |
| 245 | # Build footer |
| 246 | footer <- "<tfoot>\n" |
| 247 | for(i in 1:count.label){ |
| 248 | footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ", |
| 249 | "colspan='100%'><sup>", ids[i],"</sup> ", |
| 250 | label[i], "</td>\n</tr>\n") |
| 251 | } |
| 252 | footer <- paste0(footer, "</tfoot>\n") |
| 253 | |
| 254 | # Paste footer to the table |
| 255 | export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1]) |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 256 | } |
| 257 | return(export) |
| 258 | } |