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