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