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". |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 15 | #' @param threeparttable Boolean value indicating if a |
| 16 | #' \href{https://www.ctan.org/pkg/threeparttable}{threeparttable} scheme should be used. |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 17 | #' |
| 18 | #' @export |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 19 | add_footnote <- function(input, label = NULL, |
| 20 | notation = c("alphabet", "number", "symbol"), |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 21 | threeparttable = FALSE) { |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 22 | if (is.null(label)) return(input) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 23 | |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 24 | notation <- match.arg(notation) |
| 25 | if (notation == "symbol") { |
| 26 | notation <- paste0(notation, ".", attr(input, "format")) |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 27 | } |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 28 | |
| 29 | ids.ops <- read.csv(system.file("symbol_index.csv", package = "kableExtra")) |
| 30 | ids <- ids.ops[, notation] |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 31 | ids.intable <- gsub("\\*", "\\\\*", ids) |
| 32 | |
| 33 | #count the number of items in label and intable notation |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 34 | count.label <- length(label) |
| 35 | count.intablenote <- sum(str_count(input, "\\[note\\]")) |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 36 | if (count.intablenote != 0 & count.label != count.intablenote) { |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 37 | warning(paste("You entered", count.label, "labels but you put", |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 38 | count.intablenote, "[note] in your table.")) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 39 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 40 | |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 41 | export <- input |
| 42 | |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 43 | # 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] | 44 | extra.notation <- unique(as.numeric( |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 45 | str_extract( |
| 46 | str_extract_all( |
| 47 | paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]" |
| 48 | )[[1]], |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 49 | "[0-9]{1,2}"))) |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 50 | |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 51 | # Pandoc --------------------------- |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 52 | # Footnote solution for markdown and pandoc. It is not perfect as |
| 53 | # markdown doesn't support complex table formats but this solution |
| 54 | # should be able to satisfy people who don't want to spend extra |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 55 | # time to define their `kable` format. |
| 56 | if (!attr(input, "format") %in% c("html", "latex")) { |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 57 | # In table notation |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 58 | if (count.intablenote != 0) { |
| 59 | for (i in 1:count.intablenote) { |
| 60 | replace_note <- paste0("^", ids.intable[i], "^", |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 61 | paste0(rep(" ", 4 - ceiling(i/5)), collapse = "")) |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 62 | |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 63 | export[which(str_detect(export, "\\[note\\]"))[1]] <- |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 64 | sub("\\[note\\]", replace_note, |
| 65 | export[which(str_detect(export, "\\[note\\]"))[1]]) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 66 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 67 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 68 | # Fix extra in table notation |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 69 | for (i in extra.notation) { |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 70 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 71 | paste0("^", ids.intable[i], "^", |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 72 | paste0(rep(" ", 4 - ceiling(i/5)), collapse = "")), |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 73 | export) |
| 74 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 75 | |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 76 | export[length(export) + 1] <- "" |
| 77 | export[length(export) + 1] <- "__Note:__" |
| 78 | export[length(export) + 1] <- paste0( |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 79 | paste0("^", ids[1:length(label)], "^ ", label), collapse = " " |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 80 | ) |
| 81 | } |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 82 | |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 83 | # LaTeX Tables -------------------------------- |
| 84 | if (attr(input, "format") == "latex") { |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 85 | # Clean the entry for labels |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 86 | label <- escape_latex(label) |
| 87 | label <- gsub("\\\\", "\\\\\\\\", label) |
Hao Zhu | 8977a8a | 2015-11-19 16:52:21 -0500 | [diff] [blame] | 88 | |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 89 | kable_info <- magic_mirror(input) |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 90 | if (kable_info$tabular == "longtable") { |
| 91 | if (notation != "number") { |
| 92 | warning("Notation is set to 'number' and other formats are not supported.") |
| 93 | notation <- "number" |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 94 | } |
| 95 | # If longtable is used, then use page footnote instead of threeparttable |
| 96 | # as it makes more sense to see the footnote at the bottom of page if |
| 97 | # table is longer than one page. |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 98 | if (threeparttable == T) { |
| 99 | warning("Threeparttable does not support longtable.") |
| 100 | threeparttable = F |
| 101 | } |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 102 | |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 103 | # Longtable doesn't support footnote in caption directly. |
| 104 | # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 105 | |
| 106 | count.in.caption.note <- 0 |
| 107 | if (!is.na(kable_info$caption)) { |
| 108 | count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]") |
| 109 | } |
| 110 | if (count.in.caption.note != 0) { |
| 111 | caption.footnote <- paste0("\\\\addtocounter{footnote}{-", |
| 112 | count.in.caption.note, "}") |
| 113 | for (i in 1:count.in.caption.note) { |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 114 | export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export) |
| 115 | caption.footnote <- paste0( |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 116 | caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", |
| 117 | label[i], "}") |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 120 | if (str_detect(export, "\\\\toprule")) { |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 121 | export <- sub("\\\\toprule", |
| 122 | paste0("\\\\toprule\n", caption.footnote), export) |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 123 | } else { |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 124 | export <- sub("\\\\hline", |
| 125 | paste0("\\\\hline\n", caption.footnote), export) |
| 126 | } |
| 127 | } |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 128 | for (i in (count.in.caption.note + 1):count.intablenote) { |
Hao Zhu | 4adea85 | 2015-11-16 16:38:34 -0500 | [diff] [blame] | 129 | export <- sub("\\[note\\]", |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 130 | paste0("\\\\footnote[", i, "]{", label[i], "}"), export) |
| 131 | } |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 132 | for (i in extra.notation) { |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 133 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 134 | paste0("\\\\footnotemark[", i, "]"), |
| 135 | export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 136 | } |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 137 | } else { |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 138 | # Replace in-table notation with appropriate symbol |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 139 | for (i in 1:count.intablenote) { |
| 140 | export <- sub("\\[note\\]", |
| 141 | paste0("\\\\textsuperscript{", ids.intable[i], "}"), |
| 142 | export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 143 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 144 | |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 145 | # Fix extra in table notation |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 146 | for (i in extra.notation) { |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 147 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 148 | paste0("\\\\textsuperscript{", ids.intable[i], "}"), |
| 149 | export) |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 150 | } |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 151 | if (threeparttable == T) { |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 152 | # generate footer with appropriate symbol |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 153 | usepackage_latex("threeparttable") |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 154 | footer <- "" |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 155 | for (i in 1:count.label) { |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 156 | footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n") |
| 157 | } |
| 158 | |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 159 | if (grepl("\\\\caption\\{.*?\\}", export)) { |
| 160 | export <- sub("\\\\caption\\{", |
| 161 | "\\\\begin{threeparttable}\n\\\\caption{", |
| 162 | export) |
| 163 | } else { |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 164 | export <- sub("\\\\begin\\{tabular\\}", |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 165 | "\\\\begin{threeparttable}\n\\\\begin{tabular}", |
| 166 | export) |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 167 | } |
| 168 | export <- gsub( |
| 169 | "\\\\end\\{tabular\\}", |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 170 | paste0("\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n", |
| 171 | footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"), |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 172 | export) |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 173 | } else { |
| 174 | table.width <- max(nchar( |
| 175 | str_replace_all( |
| 176 | str_replace_all(kable_info$contents, "\\[note\\]", ""), |
| 177 | "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1) |
| 178 | footer <- "" |
| 179 | for (i in 1:count.label) { |
| 180 | label.wrap <- strwrap(label[i], table.width) |
| 181 | footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol, |
| 182 | "}{l}{\\\\textsuperscript{", ids[i], "} ", |
| 183 | label.wrap[1], "}\\\\\\\\\n") |
| 184 | if (length(label.wrap) > 1) { |
| 185 | for (j in 2:length(label.wrap)) { |
| 186 | footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol, |
| 187 | "}{l}{", label.wrap[j], "}\\\\\\\\\n") |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 188 | } |
| 189 | } |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 190 | } |
| 191 | export <- gsub("\\\\end\\{tabular\\}", |
| 192 | paste0(footer, "\\\\end{tabular}"), |
| 193 | export) |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 194 | } |
Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame] | 195 | } |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 196 | } |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 197 | |
| 198 | # HTML Tables ------------------- |
| 199 | if (attr(input, "format") == "html") { |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 200 | # Clean the entry for labels |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 201 | label <- escape_html(label) |
Hao Zhu | dc4b714 | 2015-11-19 10:37:53 -0500 | [diff] [blame] | 202 | |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 203 | # Replace in-table notation with appropriate symbol |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 204 | for (i in 1:count.intablenote) { |
| 205 | export <- sub("\\[note\\]", |
| 206 | paste0("<sup>", ids.intable[i], "</sup>"), |
| 207 | export) |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | # Fix extra in table notation |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 211 | for (i in extra.notation) { |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 212 | export <- gsub(paste0("\\[note", i, "\\]"), |
| 213 | paste0("<sup>", ids.intable[i], "</sup>"), |
| 214 | export) |
| 215 | } |
| 216 | |
| 217 | # Build footer |
| 218 | footer <- "<tfoot>\n" |
Hao Zhu | c541563 | 2017-02-21 17:16:13 -0500 | [diff] [blame] | 219 | for (i in 1:count.label) { |
Hao Zhu | 2203f66 | 2015-11-20 11:53:39 -0500 | [diff] [blame] | 220 | footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ", |
| 221 | "colspan='100%'><sup>", ids[i],"</sup> ", |
| 222 | label[i], "</td>\n</tr>\n") |
| 223 | } |
| 224 | footer <- paste0(footer, "</tfoot>\n") |
| 225 | |
| 226 | # Paste footer to the table |
| 227 | export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1]) |
Hao Zhu | b1bc0aa | 2015-11-12 11:23:42 -0500 | [diff] [blame] | 228 | } |
| 229 | return(export) |
| 230 | } |