blob: 469987f0ea94cd2ada6ae4f5e1233239e03cbbaf [file] [log] [blame]
Hao Zhub1bc0aa2015-11-12 11:23:42 -05001#' Add footnote
2#'
Hao Zhue7c8f702017-10-10 13:22:59 -04003#' @description Add footnote to your favorite kable output.
Hao Zhub1bc0aa2015-11-12 11:23:42 -05004#'
5#' @param input The direct output of your \code{kable} function or your last
6#' \code{kableExtra} function.
7#' @param label A vector of footnotes you want to add. You don't need to add
8#' notations in your notes.
9#' @param notation You can select the format of your footnote notation from
Hao Zhub8b0cae2018-04-11 16:27:13 -040010#' `number`, `alphabet`, `symbol` and `none`.
Hao Zhuf7994dd2017-02-27 16:58:42 -050011#' @param threeparttable Boolean value indicating if a
Hao Zhu78e61222017-05-24 20:53:35 -040012#' \href{https://www.ctan.org/pkg/threeparttable}{threeparttable} scheme should
13#' be used.
Hao Zhu245931c2017-09-01 22:43:56 -040014#' @param escape Logical value controlling if the label needs to be escaped.
15#' Default is TRUE.
Hao Zhu78e61222017-05-24 20:53:35 -040016#'
Hao Zhu9399dcc2020-08-26 17:27:38 -040017#' @examples
18#' \dontrun{
19#' x <- knitr::kable(head(mtcars), "html")
Hao Zhu78e61222017-05-24 20:53:35 -040020#' add_footnote(x, c("footnote 1", "footnote 2"), notation = "symbol")
Hao Zhu9399dcc2020-08-26 17:27:38 -040021#' }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050022#'
23#' @export
Hao Zhue10cfd32017-02-21 16:41:14 -050024add_footnote <- function(input, label = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050025 notation = "alphabet",
Hao Zhu245931c2017-09-01 22:43:56 -040026 threeparttable = FALSE,
27 escape = TRUE) {
Hao Zhue10cfd32017-02-21 16:41:14 -050028 if (is.null(label)) return(input)
Hao Zhu8977a8a2015-11-19 16:52:21 -050029
Hao Zhu9b45a182017-02-27 18:17:46 -050030 if (notation == "alphabet") {
31 notation <- getOption("kable_footnote_notation", "alphabet")
32 }
33 if (!threeparttable) {
34 threeparttable <- getOption("kable_footnote_threeparttable", FALSE)
35 }
36
Hao Zhu9b45a182017-02-27 18:17:46 -050037 table_info <- NULL
38
Hao Zhub8b0cae2018-04-11 16:27:13 -040039 notation <- match.arg(notation, c("alphabet", "number", "symbol", "none"))
40 if (notation == "none") {
41 ids <- rep("", 20)
42 ids.intable <- ids
43 } else {
44 if (notation == "symbol") {
45 notation <- paste0(notation, ".", attr(input, "format"))
46 }
47
48 ids.ops <- read.csv(system.file("symbol_index.csv", package = "kableExtra"))
49 ids <- ids.ops[, notation]
50 ids.intable <- gsub("\\*", "\\\\*", ids)
51 }
52
Hao Zhudb04e302015-11-15 16:57:38 -050053
54 #count the number of items in label and intable notation
Hao Zhue10cfd32017-02-21 16:41:14 -050055 count.label <- length(label)
56 count.intablenote <- sum(str_count(input, "\\[note\\]"))
Hao Zhuc5415632017-02-21 17:16:13 -050057 if (count.intablenote != 0 & count.label != count.intablenote) {
Hao Zhudb04e302015-11-15 16:57:38 -050058 warning(paste("You entered", count.label, "labels but you put",
Hao Zhue10cfd32017-02-21 16:41:14 -050059 count.intablenote, "[note] in your table."))
Hao Zhudb04e302015-11-15 16:57:38 -050060 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050061
Hao Zhu4adea852015-11-16 16:38:34 -050062 export <- input
63
Hao Zhudc4b7142015-11-19 10:37:53 -050064 # Find out if there are any extra in-table notations needed to be corrected
Hao Zhu2203f662015-11-20 11:53:39 -050065 extra.notation <- unique(as.numeric(
Hao Zhudc4b7142015-11-19 10:37:53 -050066 str_extract(
67 str_extract_all(
68 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
69 )[[1]],
Hao Zhu2203f662015-11-20 11:53:39 -050070 "[0-9]{1,2}")))
Hao Zhudc4b7142015-11-19 10:37:53 -050071
Hao Zhuc5415632017-02-21 17:16:13 -050072 # Pandoc ---------------------------
Hao Zhu4adea852015-11-16 16:38:34 -050073 # Footnote solution for markdown and pandoc. It is not perfect as
74 # markdown doesn't support complex table formats but this solution
75 # should be able to satisfy people who don't want to spend extra
Hao Zhue10cfd32017-02-21 16:41:14 -050076 # time to define their `kable` format.
77 if (!attr(input, "format") %in% c("html", "latex")) {
Hao Zhu4adea852015-11-16 16:38:34 -050078 # In table notation
Hao Zhue10cfd32017-02-21 16:41:14 -050079 if (count.intablenote != 0) {
80 for (i in 1:count.intablenote) {
81 replace_note <- paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050082 paste0(rep(" ", 4 - ceiling(i/5)), collapse = ""))
Hao Zhue10cfd32017-02-21 16:41:14 -050083
Hao Zhudb04e302015-11-15 16:57:38 -050084 export[which(str_detect(export, "\\[note\\]"))[1]] <-
Hao Zhue10cfd32017-02-21 16:41:14 -050085 sub("\\[note\\]", replace_note,
86 export[which(str_detect(export, "\\[note\\]"))[1]])
Hao Zhudb04e302015-11-15 16:57:38 -050087 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050088 }
Hao Zhu4adea852015-11-16 16:38:34 -050089 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -050090 for (i in extra.notation) {
Hao Zhu4adea852015-11-16 16:38:34 -050091 export <- gsub(paste0("\\[note", i, "\\]"),
92 paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050093 paste0(rep(" ", 4 - ceiling(i/5)), collapse = "")),
Hao Zhu4adea852015-11-16 16:38:34 -050094 export)
95 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050096
Hao Zhuc5415632017-02-21 17:16:13 -050097 export[length(export) + 1] <- ""
98 export[length(export) + 1] <- "__Note:__"
99 export[length(export) + 1] <- paste0(
Hao Zhudb04e302015-11-15 16:57:38 -0500100 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
Hao Zhuc5415632017-02-21 17:16:13 -0500101 )
102 }
Hao Zhudb04e302015-11-15 16:57:38 -0500103
Hao Zhuc5415632017-02-21 17:16:13 -0500104 # LaTeX Tables --------------------------------
105 if (attr(input, "format") == "latex") {
Hao Zhu2203f662015-11-20 11:53:39 -0500106 # Clean the entry for labels
Hao Zhu245931c2017-09-01 22:43:56 -0400107 if (escape) {
108 label <- escape_latex(label)
Hao Zhu1aff7342018-04-02 18:33:15 -0400109 label <- linebreak(label)
Hao Zhu245931c2017-09-01 22:43:56 -0400110 }
Hao Zhuc5415632017-02-21 17:16:13 -0500111 label <- gsub("\\\\", "\\\\\\\\", label)
Hao Zhu8977a8a2015-11-19 16:52:21 -0500112
Hao Zhu904c0dc2018-04-04 16:14:32 -0400113 export <- enc2utf8(export)
Hao Zhu9b45a182017-02-27 18:17:46 -0500114 table_info <- magic_mirror(input)
115 if (table_info$tabular == "longtable") {
Hao Zhuc5415632017-02-21 17:16:13 -0500116 if (notation != "number") {
117 warning("Notation is set to 'number' and other formats are not supported.")
118 notation <- "number"
Hao Zhudc4b7142015-11-19 10:37:53 -0500119 }
120 # If longtable is used, then use page footnote instead of threeparttable
121 # as it makes more sense to see the footnote at the bottom of page if
122 # table is longer than one page.
Hao Zhuc5415632017-02-21 17:16:13 -0500123 if (threeparttable == T) {
124 warning("Threeparttable does not support longtable.")
125 threeparttable = F
126 }
Hao Zhu4adea852015-11-16 16:38:34 -0500127
Hao Zhudc4b7142015-11-19 10:37:53 -0500128 # Longtable doesn't support footnote in caption directly.
129 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
Hao Zhuc5415632017-02-21 17:16:13 -0500130
131 count.in.caption.note <- 0
Hao Zhu9b45a182017-02-27 18:17:46 -0500132 if (!is.na(table_info$caption)) {
133 count.in.caption.note <- str_count(table_info$caption, "\\[note\\]")
Hao Zhuc5415632017-02-21 17:16:13 -0500134 }
135 if (count.in.caption.note != 0) {
136 caption.footnote <- paste0("\\\\addtocounter{footnote}{-",
137 count.in.caption.note, "}")
138 for (i in 1:count.in.caption.note) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500139 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
140 caption.footnote <- paste0(
Hao Zhuc5415632017-02-21 17:16:13 -0500141 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{",
142 label[i], "}")
Hao Zhudc4b7142015-11-19 10:37:53 -0500143 }
144
Hao Zhuc5415632017-02-21 17:16:13 -0500145 if (str_detect(export, "\\\\toprule")) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500146 export <- sub("\\\\toprule",
147 paste0("\\\\toprule\n", caption.footnote), export)
Hao Zhuc5415632017-02-21 17:16:13 -0500148 } else {
Hao Zhudc4b7142015-11-19 10:37:53 -0500149 export <- sub("\\\\hline",
150 paste0("\\\\hline\n", caption.footnote), export)
151 }
152 }
Hao Zhuc5415632017-02-21 17:16:13 -0500153 for (i in (count.in.caption.note + 1):count.intablenote) {
Hao Zhu4adea852015-11-16 16:38:34 -0500154 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500155 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
156 }
Hao Zhuc5415632017-02-21 17:16:13 -0500157 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500158 export <- gsub(paste0("\\[note", i, "\\]"),
159 paste0("\\\\footnotemark[", i, "]"),
160 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500161 }
Hao Zhuc5415632017-02-21 17:16:13 -0500162 } else {
Hao Zhudb04e302015-11-15 16:57:38 -0500163 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500164 for (i in 1:count.intablenote) {
165 export <- sub("\\[note\\]",
166 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
167 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500168 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500169
Hao Zhudc4b7142015-11-19 10:37:53 -0500170 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500171 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500172 export <- gsub(paste0("\\[note", i, "\\]"),
173 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
174 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500175 }
Hao Zhuc5415632017-02-21 17:16:13 -0500176 if (threeparttable == T) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500177 # generate footer with appropriate symbol
Hao Zhuf7994dd2017-02-27 16:58:42 -0500178 usepackage_latex("threeparttable")
Hao Zhudc4b7142015-11-19 10:37:53 -0500179 footer <- ""
Hao Zhuc5415632017-02-21 17:16:13 -0500180 for (i in 1:count.label) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500181 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
182 }
183
Hao Zhuc5415632017-02-21 17:16:13 -0500184 if (grepl("\\\\caption\\{.*?\\}", export)) {
185 export <- sub("\\\\caption\\{",
186 "\\\\begin{threeparttable}\n\\\\caption{",
187 export)
188 } else {
Hao Zhu245931c2017-09-01 22:43:56 -0400189 export <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"),
190 paste0("\\\\begin{threeparttable}\n\\\\begin{",
191 table_info$tabular, "}"),
Hao Zhuc5415632017-02-21 17:16:13 -0500192 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500193 }
194 export <- gsub(
195 "\\\\end\\{tabular\\}",
Hao Zhuc5415632017-02-21 17:16:13 -0500196 paste0("\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
197 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"),
Hao Zhudc4b7142015-11-19 10:37:53 -0500198 export)
Hao Zhuc5415632017-02-21 17:16:13 -0500199 } else {
200 table.width <- max(nchar(
201 str_replace_all(
Hao Zhu9b45a182017-02-27 18:17:46 -0500202 str_replace_all(table_info$contents, "\\[note\\]", ""),
203 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (table_info$ncol - 1)
Hao Zhuc5415632017-02-21 17:16:13 -0500204 footer <- ""
205 for (i in 1:count.label) {
206 label.wrap <- strwrap(label[i], table.width)
Hao Zhu9b45a182017-02-27 18:17:46 -0500207 footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
Hao Zhuc5415632017-02-21 17:16:13 -0500208 "}{l}{\\\\textsuperscript{", ids[i], "} ",
209 label.wrap[1], "}\\\\\\\\\n")
210 if (length(label.wrap) > 1) {
211 for (j in 2:length(label.wrap)) {
Hao Zhu9b45a182017-02-27 18:17:46 -0500212 footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
Hao Zhuc5415632017-02-21 17:16:13 -0500213 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
Hao Zhudc4b7142015-11-19 10:37:53 -0500214 }
215 }
Hao Zhuc5415632017-02-21 17:16:13 -0500216 }
Hao Zhu9f917482018-01-08 18:09:33 -0500217 export <- gsub(table_info$end_tabular,
218 paste0(footer, "\\\\end{", table_info$tabular, "}"),
Hao Zhuc5415632017-02-21 17:16:13 -0500219 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500220 }
Hao Zhudb04e302015-11-15 16:57:38 -0500221 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500222 }
Hao Zhuc5415632017-02-21 17:16:13 -0500223
224 # HTML Tables -------------------
225 if (attr(input, "format") == "html") {
Hao Zhu2203f662015-11-20 11:53:39 -0500226 # Clean the entry for labels
Hao Zhu9b45a182017-02-27 18:17:46 -0500227 table_info <- magic_mirror(input)
Hao Zhu245931c2017-09-01 22:43:56 -0400228 if (escape) {
229 label <- escape_html(label)
230 }
Hao Zhu2203f662015-11-20 11:53:39 -0500231 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500232 for (i in 1:count.intablenote) {
233 export <- sub("\\[note\\]",
234 paste0("<sup>", ids.intable[i], "</sup>"),
235 export)
Hao Zhu2203f662015-11-20 11:53:39 -0500236 }
237
238 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500239 for (i in extra.notation) {
Hao Zhu2203f662015-11-20 11:53:39 -0500240 export <- gsub(paste0("\\[note", i, "\\]"),
241 paste0("<sup>", ids.intable[i], "</sup>"),
242 export)
243 }
244
245 # Build footer
246 footer <- "<tfoot>\n"
Hao Zhuc5415632017-02-21 17:16:13 -0500247 for (i in 1:count.label) {
Hao Zhu2203f662015-11-20 11:53:39 -0500248 footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ",
249 "colspan='100%'><sup>", ids[i],"</sup> ",
250 label[i], "</td>\n</tr>\n")
251 }
252 footer <- paste0(footer, "</tfoot>\n")
253
254 # Paste footer to the table
255 export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1])
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500256 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400257 attr(export, "kable_meta") <- table_info
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500258 return(export)
259}