blob: 5db0778f8b953ee8d004e49ffda5c29bb5d6f319 [file] [log] [blame]
Hao Zhub1bc0aa2015-11-12 11:23:42 -05001#' 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 Beasley70aa3b22016-01-11 11:42:27 -060015#' @param threeparttable Boolean value indicating if a \href{https://www.ctan.org/pkg/threeparttable}{threeparttable} scheme should be used.
Hao Zhub1bc0aa2015-11-12 11:23:42 -050016#'
17#' @export
Hao Zhue10cfd32017-02-21 16:41:14 -050018add_footnote <- function(input, label = NULL,
19 notation = c("alphabet", "number", "symbol"),
Hao Zhu2203f662015-11-20 11:53:39 -050020 threeparttable = FALSE) {
Hao Zhue10cfd32017-02-21 16:41:14 -050021 if (is.null(label)) return(input)
Hao Zhu8977a8a2015-11-19 16:52:21 -050022
Hao Zhue10cfd32017-02-21 16:41:14 -050023 notation <- match.arg(notation)
24 if (notation == "symbol") {
25 notation <- paste0(notation, ".", attr(input, "format"))
Hao Zhub1bc0aa2015-11-12 11:23:42 -050026 }
Hao Zhue10cfd32017-02-21 16:41:14 -050027
28 ids.ops <- read.csv(system.file("symbol_index.csv", package = "kableExtra"))
29 ids <- ids.ops[, notation]
Hao Zhudb04e302015-11-15 16:57:38 -050030 ids.intable <- gsub("\\*", "\\\\*", ids)
31
32 #count the number of items in label and intable notation
Hao Zhue10cfd32017-02-21 16:41:14 -050033 count.label <- length(label)
34 count.intablenote <- sum(str_count(input, "\\[note\\]"))
Hao Zhuc5415632017-02-21 17:16:13 -050035 if (count.intablenote != 0 & count.label != count.intablenote) {
Hao Zhudb04e302015-11-15 16:57:38 -050036 warning(paste("You entered", count.label, "labels but you put",
Hao Zhue10cfd32017-02-21 16:41:14 -050037 count.intablenote, "[note] in your table."))
Hao Zhudb04e302015-11-15 16:57:38 -050038 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050039
Hao Zhu4adea852015-11-16 16:38:34 -050040 export <- input
41
Hao Zhudc4b7142015-11-19 10:37:53 -050042 # Find out if there are any extra in-table notations needed to be corrected
Hao Zhu2203f662015-11-20 11:53:39 -050043 extra.notation <- unique(as.numeric(
Hao Zhudc4b7142015-11-19 10:37:53 -050044 str_extract(
45 str_extract_all(
46 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
47 )[[1]],
Hao Zhu2203f662015-11-20 11:53:39 -050048 "[0-9]{1,2}")))
Hao Zhudc4b7142015-11-19 10:37:53 -050049
Hao Zhuc5415632017-02-21 17:16:13 -050050 # Pandoc ---------------------------
Hao Zhu4adea852015-11-16 16:38:34 -050051 # 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 Zhue10cfd32017-02-21 16:41:14 -050054 # time to define their `kable` format.
55 if (!attr(input, "format") %in% c("html", "latex")) {
Hao Zhu4adea852015-11-16 16:38:34 -050056 # In table notation
Hao Zhue10cfd32017-02-21 16:41:14 -050057 if (count.intablenote != 0) {
58 for (i in 1:count.intablenote) {
59 replace_note <- paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050060 paste0(rep(" ", 4 - ceiling(i/5)), collapse = ""))
Hao Zhue10cfd32017-02-21 16:41:14 -050061
Hao Zhudb04e302015-11-15 16:57:38 -050062 export[which(str_detect(export, "\\[note\\]"))[1]] <-
Hao Zhue10cfd32017-02-21 16:41:14 -050063 sub("\\[note\\]", replace_note,
64 export[which(str_detect(export, "\\[note\\]"))[1]])
Hao Zhudb04e302015-11-15 16:57:38 -050065 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050066 }
Hao Zhu4adea852015-11-16 16:38:34 -050067 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -050068 for (i in extra.notation) {
Hao Zhu4adea852015-11-16 16:38:34 -050069 export <- gsub(paste0("\\[note", i, "\\]"),
70 paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050071 paste0(rep(" ", 4 - ceiling(i/5)), collapse = "")),
Hao Zhu4adea852015-11-16 16:38:34 -050072 export)
73 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050074
Hao Zhuc5415632017-02-21 17:16:13 -050075 export[length(export) + 1] <- ""
76 export[length(export) + 1] <- "__Note:__"
77 export[length(export) + 1] <- paste0(
Hao Zhudb04e302015-11-15 16:57:38 -050078 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
Hao Zhuc5415632017-02-21 17:16:13 -050079 )
80 }
Hao Zhudb04e302015-11-15 16:57:38 -050081
Hao Zhuc5415632017-02-21 17:16:13 -050082 # LaTeX Tables --------------------------------
83 if (attr(input, "format") == "latex") {
Hao Zhu2203f662015-11-20 11:53:39 -050084 # Clean the entry for labels
Hao Zhuc5415632017-02-21 17:16:13 -050085 label <- escape_latex(label)
86 label <- gsub("\\\\", "\\\\\\\\", label)
Hao Zhu8977a8a2015-11-19 16:52:21 -050087
Hao Zhu4adea852015-11-16 16:38:34 -050088 kable_info <- magic_mirror(input)
Hao Zhuc5415632017-02-21 17:16:13 -050089 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 Zhudc4b7142015-11-19 10:37:53 -050093 }
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 Zhuc5415632017-02-21 17:16:13 -050097 if (threeparttable == T) {
98 warning("Threeparttable does not support longtable.")
99 threeparttable = F
100 }
Hao Zhu4adea852015-11-16 16:38:34 -0500101
Hao Zhudc4b7142015-11-19 10:37:53 -0500102 # Longtable doesn't support footnote in caption directly.
103 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
Hao Zhuc5415632017-02-21 17:16:13 -0500104
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 Zhudc4b7142015-11-19 10:37:53 -0500113 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
114 caption.footnote <- paste0(
Hao Zhuc5415632017-02-21 17:16:13 -0500115 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{",
116 label[i], "}")
Hao Zhudc4b7142015-11-19 10:37:53 -0500117 }
118
Hao Zhuc5415632017-02-21 17:16:13 -0500119 if (str_detect(export, "\\\\toprule")) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500120 export <- sub("\\\\toprule",
121 paste0("\\\\toprule\n", caption.footnote), export)
Hao Zhuc5415632017-02-21 17:16:13 -0500122 } else {
Hao Zhudc4b7142015-11-19 10:37:53 -0500123 export <- sub("\\\\hline",
124 paste0("\\\\hline\n", caption.footnote), export)
125 }
126 }
Hao Zhuc5415632017-02-21 17:16:13 -0500127 for (i in (count.in.caption.note + 1):count.intablenote) {
Hao Zhu4adea852015-11-16 16:38:34 -0500128 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500129 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
130 }
Hao Zhuc5415632017-02-21 17:16:13 -0500131 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500132 export <- gsub(paste0("\\[note", i, "\\]"),
133 paste0("\\\\footnotemark[", i, "]"),
134 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500135 }
Hao Zhuc5415632017-02-21 17:16:13 -0500136 } else {
Hao Zhudb04e302015-11-15 16:57:38 -0500137 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500138 for (i in 1:count.intablenote) {
139 export <- sub("\\[note\\]",
140 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
141 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500142 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500143
Hao Zhudc4b7142015-11-19 10:37:53 -0500144 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500145 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500146 export <- gsub(paste0("\\[note", i, "\\]"),
147 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
148 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500149 }
Hao Zhuc5415632017-02-21 17:16:13 -0500150 if (threeparttable == T) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500151 # generate footer with appropriate symbol
152 footer <- ""
Hao Zhuc5415632017-02-21 17:16:13 -0500153 for (i in 1:count.label) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500154 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
155 }
156
Hao Zhuc5415632017-02-21 17:16:13 -0500157 if (grepl("\\\\caption\\{.*?\\}", export)) {
158 export <- sub("\\\\caption\\{",
159 "\\\\begin{threeparttable}\n\\\\caption{",
160 export)
161 } else {
Hao Zhudc4b7142015-11-19 10:37:53 -0500162 export <- sub("\\\\begin\\{tabular\\}",
Hao Zhuc5415632017-02-21 17:16:13 -0500163 "\\\\begin{threeparttable}\n\\\\begin{tabular}",
164 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500165 }
166 export <- gsub(
167 "\\\\end\\{tabular\\}",
Hao Zhuc5415632017-02-21 17:16:13 -0500168 paste0("\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
169 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"),
Hao Zhudc4b7142015-11-19 10:37:53 -0500170 export)
Hao Zhuc5415632017-02-21 17:16:13 -0500171 } 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 Zhudc4b7142015-11-19 10:37:53 -0500186 }
187 }
Hao Zhuc5415632017-02-21 17:16:13 -0500188 }
189 export <- gsub("\\\\end\\{tabular\\}",
190 paste0(footer, "\\\\end{tabular}"),
191 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500192 }
Hao Zhudb04e302015-11-15 16:57:38 -0500193 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500194 }
Hao Zhuc5415632017-02-21 17:16:13 -0500195
196 # HTML Tables -------------------
197 if (attr(input, "format") == "html") {
Hao Zhu2203f662015-11-20 11:53:39 -0500198 # Clean the entry for labels
Hao Zhuc5415632017-02-21 17:16:13 -0500199 label <- escape_html(label)
Hao Zhudc4b7142015-11-19 10:37:53 -0500200
Hao Zhu2203f662015-11-20 11:53:39 -0500201 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500202 for (i in 1:count.intablenote) {
203 export <- sub("\\[note\\]",
204 paste0("<sup>", ids.intable[i], "</sup>"),
205 export)
Hao Zhu2203f662015-11-20 11:53:39 -0500206 }
207
208 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500209 for (i in extra.notation) {
Hao Zhu2203f662015-11-20 11:53:39 -0500210 export <- gsub(paste0("\\[note", i, "\\]"),
211 paste0("<sup>", ids.intable[i], "</sup>"),
212 export)
213 }
214
215 # Build footer
216 footer <- "<tfoot>\n"
Hao Zhuc5415632017-02-21 17:16:13 -0500217 for (i in 1:count.label) {
Hao Zhu2203f662015-11-20 11:53:39 -0500218 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 Zhub1bc0aa2015-11-12 11:23:42 -0500226 }
227 return(export)
228}