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