blob: 121066bace3690f4ef786d737807960a561f5d38 [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)
Hao Zhuc5415632017-02-21 17:16:13 -050031 # ids.simple <- c(
32 # "*", "\u2020", "\u2021", "\u00A7", "\u00B6",
33 # "**", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
34 # "***", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
35 # "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
36 # "****", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
37 # "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
38 # )
Hao Zhudb04e302015-11-15 16:57:38 -050039
40 #count the number of items in label and intable notation
Hao Zhue10cfd32017-02-21 16:41:14 -050041 count.label <- length(label)
42 count.intablenote <- sum(str_count(input, "\\[note\\]"))
Hao Zhuc5415632017-02-21 17:16:13 -050043 if (count.intablenote != 0 & count.label != count.intablenote) {
Hao Zhudb04e302015-11-15 16:57:38 -050044 warning(paste("You entered", count.label, "labels but you put",
Hao Zhue10cfd32017-02-21 16:41:14 -050045 count.intablenote, "[note] in your table."))
Hao Zhudb04e302015-11-15 16:57:38 -050046 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050047
Hao Zhu4adea852015-11-16 16:38:34 -050048 export <- input
49
Hao Zhudc4b7142015-11-19 10:37:53 -050050 # Find out if there are any extra in-table notations needed to be corrected
Hao Zhu2203f662015-11-20 11:53:39 -050051 extra.notation <- unique(as.numeric(
Hao Zhudc4b7142015-11-19 10:37:53 -050052 str_extract(
53 str_extract_all(
54 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
55 )[[1]],
Hao Zhu2203f662015-11-20 11:53:39 -050056 "[0-9]{1,2}")))
Hao Zhudc4b7142015-11-19 10:37:53 -050057
Hao Zhuc5415632017-02-21 17:16:13 -050058 # Pandoc ---------------------------
Hao Zhu4adea852015-11-16 16:38:34 -050059 # Footnote solution for markdown and pandoc. It is not perfect as
60 # markdown doesn't support complex table formats but this solution
61 # should be able to satisfy people who don't want to spend extra
Hao Zhue10cfd32017-02-21 16:41:14 -050062 # time to define their `kable` format.
63 if (!attr(input, "format") %in% c("html", "latex")) {
Hao Zhu4adea852015-11-16 16:38:34 -050064 # In table notation
Hao Zhue10cfd32017-02-21 16:41:14 -050065 if (count.intablenote != 0) {
66 for (i in 1:count.intablenote) {
67 replace_note <- paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050068 paste0(rep(" ", 4 - ceiling(i/5)), collapse = ""))
Hao Zhue10cfd32017-02-21 16:41:14 -050069
Hao Zhudb04e302015-11-15 16:57:38 -050070 export[which(str_detect(export, "\\[note\\]"))[1]] <-
Hao Zhue10cfd32017-02-21 16:41:14 -050071 sub("\\[note\\]", replace_note,
72 export[which(str_detect(export, "\\[note\\]"))[1]])
Hao Zhudb04e302015-11-15 16:57:38 -050073 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050074 }
Hao Zhu4adea852015-11-16 16:38:34 -050075 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -050076 for (i in extra.notation) {
Hao Zhu4adea852015-11-16 16:38:34 -050077 export <- gsub(paste0("\\[note", i, "\\]"),
78 paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050079 paste0(rep(" ", 4 - ceiling(i/5)), collapse = "")),
Hao Zhu4adea852015-11-16 16:38:34 -050080 export)
81 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050082
Hao Zhuc5415632017-02-21 17:16:13 -050083 export[length(export) + 1] <- ""
84 export[length(export) + 1] <- "__Note:__"
85 export[length(export) + 1] <- paste0(
Hao Zhudb04e302015-11-15 16:57:38 -050086 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
Hao Zhuc5415632017-02-21 17:16:13 -050087 )
88 }
Hao Zhudb04e302015-11-15 16:57:38 -050089
Hao Zhuc5415632017-02-21 17:16:13 -050090 # LaTeX Tables --------------------------------
91 if (attr(input, "format") == "latex") {
Hao Zhu2203f662015-11-20 11:53:39 -050092 # Clean the entry for labels
Hao Zhuc5415632017-02-21 17:16:13 -050093 label <- escape_latex(label)
94 label <- gsub("\\\\", "\\\\\\\\", label)
Hao Zhu8977a8a2015-11-19 16:52:21 -050095
Hao Zhu4adea852015-11-16 16:38:34 -050096 kable_info <- magic_mirror(input)
Hao Zhuc5415632017-02-21 17:16:13 -050097 if (kable_info$tabular == "longtable") {
98 if (notation != "number") {
99 warning("Notation is set to 'number' and other formats are not supported.")
100 notation <- "number"
Hao Zhudc4b7142015-11-19 10:37:53 -0500101 }
102 # If longtable is used, then use page footnote instead of threeparttable
103 # as it makes more sense to see the footnote at the bottom of page if
104 # table is longer than one page.
Hao Zhuc5415632017-02-21 17:16:13 -0500105 if (threeparttable == T) {
106 warning("Threeparttable does not support longtable.")
107 threeparttable = F
108 }
Hao Zhu4adea852015-11-16 16:38:34 -0500109
Hao Zhudc4b7142015-11-19 10:37:53 -0500110 # Longtable doesn't support footnote in caption directly.
111 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
Hao Zhuc5415632017-02-21 17:16:13 -0500112
113 count.in.caption.note <- 0
114 if (!is.na(kable_info$caption)) {
115 count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]")
116 }
117 if (count.in.caption.note != 0) {
118 caption.footnote <- paste0("\\\\addtocounter{footnote}{-",
119 count.in.caption.note, "}")
120 for (i in 1:count.in.caption.note) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500121 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
122 caption.footnote <- paste0(
Hao Zhuc5415632017-02-21 17:16:13 -0500123 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{",
124 label[i], "}")
Hao Zhudc4b7142015-11-19 10:37:53 -0500125 }
126
Hao Zhuc5415632017-02-21 17:16:13 -0500127 if (str_detect(export, "\\\\toprule")) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500128 export <- sub("\\\\toprule",
129 paste0("\\\\toprule\n", caption.footnote), export)
Hao Zhuc5415632017-02-21 17:16:13 -0500130 } else {
Hao Zhudc4b7142015-11-19 10:37:53 -0500131 export <- sub("\\\\hline",
132 paste0("\\\\hline\n", caption.footnote), export)
133 }
134 }
Hao Zhuc5415632017-02-21 17:16:13 -0500135 for (i in (count.in.caption.note + 1):count.intablenote) {
Hao Zhu4adea852015-11-16 16:38:34 -0500136 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500137 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
138 }
Hao Zhuc5415632017-02-21 17:16:13 -0500139 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500140 export <- gsub(paste0("\\[note", i, "\\]"),
141 paste0("\\\\footnotemark[", i, "]"),
142 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500143 }
Hao Zhuc5415632017-02-21 17:16:13 -0500144 } else {
Hao Zhudb04e302015-11-15 16:57:38 -0500145 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500146 for (i in 1:count.intablenote) {
147 export <- sub("\\[note\\]",
148 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
149 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500150 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500151
Hao Zhudc4b7142015-11-19 10:37:53 -0500152 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500153 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500154 export <- gsub(paste0("\\[note", i, "\\]"),
155 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
156 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500157 }
Hao Zhuc5415632017-02-21 17:16:13 -0500158 if (threeparttable == T) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500159 # generate footer with appropriate symbol
160 footer <- ""
Hao Zhuc5415632017-02-21 17:16:13 -0500161 for (i in 1:count.label) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500162 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
163 }
164
Hao Zhuc5415632017-02-21 17:16:13 -0500165 if (grepl("\\\\caption\\{.*?\\}", export)) {
166 export <- sub("\\\\caption\\{",
167 "\\\\begin{threeparttable}\n\\\\caption{",
168 export)
169 } else {
Hao Zhudc4b7142015-11-19 10:37:53 -0500170 export <- sub("\\\\begin\\{tabular\\}",
Hao Zhuc5415632017-02-21 17:16:13 -0500171 "\\\\begin{threeparttable}\n\\\\begin{tabular}",
172 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500173 }
174 export <- gsub(
175 "\\\\end\\{tabular\\}",
Hao Zhuc5415632017-02-21 17:16:13 -0500176 paste0("\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
177 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"),
Hao Zhudc4b7142015-11-19 10:37:53 -0500178 export)
Hao Zhuc5415632017-02-21 17:16:13 -0500179 } else {
180 table.width <- max(nchar(
181 str_replace_all(
182 str_replace_all(kable_info$contents, "\\[note\\]", ""),
183 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1)
184 footer <- ""
185 for (i in 1:count.label) {
186 label.wrap <- strwrap(label[i], table.width)
187 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
188 "}{l}{\\\\textsuperscript{", ids[i], "} ",
189 label.wrap[1], "}\\\\\\\\\n")
190 if (length(label.wrap) > 1) {
191 for (j in 2:length(label.wrap)) {
192 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
193 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
Hao Zhudc4b7142015-11-19 10:37:53 -0500194 }
195 }
Hao Zhuc5415632017-02-21 17:16:13 -0500196 }
197 export <- gsub("\\\\end\\{tabular\\}",
198 paste0(footer, "\\\\end{tabular}"),
199 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500200 }
Hao Zhudb04e302015-11-15 16:57:38 -0500201 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500202 }
Hao Zhuc5415632017-02-21 17:16:13 -0500203
204 # HTML Tables -------------------
205 if (attr(input, "format") == "html") {
Hao Zhu2203f662015-11-20 11:53:39 -0500206 # Clean the entry for labels
Hao Zhuc5415632017-02-21 17:16:13 -0500207 label <- escape_html(label)
Hao Zhudc4b7142015-11-19 10:37:53 -0500208
Hao Zhu2203f662015-11-20 11:53:39 -0500209 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500210 for (i in 1:count.intablenote) {
211 export <- sub("\\[note\\]",
212 paste0("<sup>", ids.intable[i], "</sup>"),
213 export)
Hao Zhu2203f662015-11-20 11:53:39 -0500214 }
215
216 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500217 for (i in extra.notation) {
Hao Zhu2203f662015-11-20 11:53:39 -0500218 export <- gsub(paste0("\\[note", i, "\\]"),
219 paste0("<sup>", ids.intable[i], "</sup>"),
220 export)
221 }
222
223 # Build footer
224 footer <- "<tfoot>\n"
Hao Zhuc5415632017-02-21 17:16:13 -0500225 for (i in 1:count.label) {
Hao Zhu2203f662015-11-20 11:53:39 -0500226 footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ",
227 "colspan='100%'><sup>", ids[i],"</sup> ",
228 label[i], "</td>\n</tr>\n")
229 }
230 footer <- paste0(footer, "</tfoot>\n")
231
232 # Paste footer to the table
233 export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1])
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500234 }
235 return(export)
236}