blob: 49e37cbcc271a046999f55809e6e8e513d673cf6 [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#'
17#' @examples x <- knitr::kable(head(mtcars), "html")
18#' add_footnote(x, c("footnote 1", "footnote 2"), notation = "symbol")
Hao Zhub1bc0aa2015-11-12 11:23:42 -050019#'
20#' @export
Hao Zhue10cfd32017-02-21 16:41:14 -050021add_footnote <- function(input, label = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050022 notation = "alphabet",
Hao Zhu245931c2017-09-01 22:43:56 -040023 threeparttable = FALSE,
24 escape = TRUE) {
Hao Zhue10cfd32017-02-21 16:41:14 -050025 if (is.null(label)) return(input)
Hao Zhu8977a8a2015-11-19 16:52:21 -050026
Hao Zhu9b45a182017-02-27 18:17:46 -050027 if (notation == "alphabet") {
28 notation <- getOption("kable_footnote_notation", "alphabet")
29 }
30 if (!threeparttable) {
31 threeparttable <- getOption("kable_footnote_threeparttable", FALSE)
32 }
33
Hao Zhu9b45a182017-02-27 18:17:46 -050034 table_info <- NULL
35
Hao Zhub8b0cae2018-04-11 16:27:13 -040036 notation <- match.arg(notation, c("alphabet", "number", "symbol", "none"))
37 if (notation == "none") {
38 ids <- rep("", 20)
39 ids.intable <- ids
40 } else {
41 if (notation == "symbol") {
42 notation <- paste0(notation, ".", attr(input, "format"))
43 }
44
45 ids.ops <- read.csv(system.file("symbol_index.csv", package = "kableExtra"))
46 ids <- ids.ops[, notation]
47 ids.intable <- gsub("\\*", "\\\\*", ids)
48 }
49
Hao Zhudb04e302015-11-15 16:57:38 -050050
51 #count the number of items in label and intable notation
Hao Zhue10cfd32017-02-21 16:41:14 -050052 count.label <- length(label)
53 count.intablenote <- sum(str_count(input, "\\[note\\]"))
Hao Zhuc5415632017-02-21 17:16:13 -050054 if (count.intablenote != 0 & count.label != count.intablenote) {
Hao Zhudb04e302015-11-15 16:57:38 -050055 warning(paste("You entered", count.label, "labels but you put",
Hao Zhue10cfd32017-02-21 16:41:14 -050056 count.intablenote, "[note] in your table."))
Hao Zhudb04e302015-11-15 16:57:38 -050057 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050058
Hao Zhu4adea852015-11-16 16:38:34 -050059 export <- input
60
Hao Zhudc4b7142015-11-19 10:37:53 -050061 # Find out if there are any extra in-table notations needed to be corrected
Hao Zhu2203f662015-11-20 11:53:39 -050062 extra.notation <- unique(as.numeric(
Hao Zhudc4b7142015-11-19 10:37:53 -050063 str_extract(
64 str_extract_all(
65 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
66 )[[1]],
Hao Zhu2203f662015-11-20 11:53:39 -050067 "[0-9]{1,2}")))
Hao Zhudc4b7142015-11-19 10:37:53 -050068
Hao Zhuc5415632017-02-21 17:16:13 -050069 # Pandoc ---------------------------
Hao Zhu4adea852015-11-16 16:38:34 -050070 # Footnote solution for markdown and pandoc. It is not perfect as
71 # markdown doesn't support complex table formats but this solution
72 # should be able to satisfy people who don't want to spend extra
Hao Zhue10cfd32017-02-21 16:41:14 -050073 # time to define their `kable` format.
74 if (!attr(input, "format") %in% c("html", "latex")) {
Hao Zhu4adea852015-11-16 16:38:34 -050075 # In table notation
Hao Zhue10cfd32017-02-21 16:41:14 -050076 if (count.intablenote != 0) {
77 for (i in 1:count.intablenote) {
78 replace_note <- paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050079 paste0(rep(" ", 4 - ceiling(i/5)), collapse = ""))
Hao Zhue10cfd32017-02-21 16:41:14 -050080
Hao Zhudb04e302015-11-15 16:57:38 -050081 export[which(str_detect(export, "\\[note\\]"))[1]] <-
Hao Zhue10cfd32017-02-21 16:41:14 -050082 sub("\\[note\\]", replace_note,
83 export[which(str_detect(export, "\\[note\\]"))[1]])
Hao Zhudb04e302015-11-15 16:57:38 -050084 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050085 }
Hao Zhu4adea852015-11-16 16:38:34 -050086 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -050087 for (i in extra.notation) {
Hao Zhu4adea852015-11-16 16:38:34 -050088 export <- gsub(paste0("\\[note", i, "\\]"),
89 paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050090 paste0(rep(" ", 4 - ceiling(i/5)), collapse = "")),
Hao Zhu4adea852015-11-16 16:38:34 -050091 export)
92 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050093
Hao Zhuc5415632017-02-21 17:16:13 -050094 export[length(export) + 1] <- ""
95 export[length(export) + 1] <- "__Note:__"
96 export[length(export) + 1] <- paste0(
Hao Zhudb04e302015-11-15 16:57:38 -050097 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
Hao Zhuc5415632017-02-21 17:16:13 -050098 )
99 }
Hao Zhudb04e302015-11-15 16:57:38 -0500100
Hao Zhuc5415632017-02-21 17:16:13 -0500101 # LaTeX Tables --------------------------------
102 if (attr(input, "format") == "latex") {
Hao Zhu2203f662015-11-20 11:53:39 -0500103 # Clean the entry for labels
Hao Zhu245931c2017-09-01 22:43:56 -0400104 if (escape) {
105 label <- escape_latex(label)
Hao Zhu1aff7342018-04-02 18:33:15 -0400106 label <- linebreak(label)
Hao Zhu245931c2017-09-01 22:43:56 -0400107 }
Hao Zhuc5415632017-02-21 17:16:13 -0500108 label <- gsub("\\\\", "\\\\\\\\", label)
Hao Zhu8977a8a2015-11-19 16:52:21 -0500109
Hao Zhu904c0dc2018-04-04 16:14:32 -0400110 export <- enc2utf8(export)
Hao Zhu9b45a182017-02-27 18:17:46 -0500111 table_info <- magic_mirror(input)
112 if (table_info$tabular == "longtable") {
Hao Zhuc5415632017-02-21 17:16:13 -0500113 if (notation != "number") {
114 warning("Notation is set to 'number' and other formats are not supported.")
115 notation <- "number"
Hao Zhudc4b7142015-11-19 10:37:53 -0500116 }
117 # If longtable is used, then use page footnote instead of threeparttable
118 # as it makes more sense to see the footnote at the bottom of page if
119 # table is longer than one page.
Hao Zhuc5415632017-02-21 17:16:13 -0500120 if (threeparttable == T) {
121 warning("Threeparttable does not support longtable.")
122 threeparttable = F
123 }
Hao Zhu4adea852015-11-16 16:38:34 -0500124
Hao Zhudc4b7142015-11-19 10:37:53 -0500125 # Longtable doesn't support footnote in caption directly.
126 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
Hao Zhuc5415632017-02-21 17:16:13 -0500127
128 count.in.caption.note <- 0
Hao Zhu9b45a182017-02-27 18:17:46 -0500129 if (!is.na(table_info$caption)) {
130 count.in.caption.note <- str_count(table_info$caption, "\\[note\\]")
Hao Zhuc5415632017-02-21 17:16:13 -0500131 }
132 if (count.in.caption.note != 0) {
133 caption.footnote <- paste0("\\\\addtocounter{footnote}{-",
134 count.in.caption.note, "}")
135 for (i in 1:count.in.caption.note) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500136 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
137 caption.footnote <- paste0(
Hao Zhuc5415632017-02-21 17:16:13 -0500138 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{",
139 label[i], "}")
Hao Zhudc4b7142015-11-19 10:37:53 -0500140 }
141
Hao Zhuc5415632017-02-21 17:16:13 -0500142 if (str_detect(export, "\\\\toprule")) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500143 export <- sub("\\\\toprule",
144 paste0("\\\\toprule\n", caption.footnote), export)
Hao Zhuc5415632017-02-21 17:16:13 -0500145 } else {
Hao Zhudc4b7142015-11-19 10:37:53 -0500146 export <- sub("\\\\hline",
147 paste0("\\\\hline\n", caption.footnote), export)
148 }
149 }
Hao Zhuc5415632017-02-21 17:16:13 -0500150 for (i in (count.in.caption.note + 1):count.intablenote) {
Hao Zhu4adea852015-11-16 16:38:34 -0500151 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500152 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
153 }
Hao Zhuc5415632017-02-21 17:16:13 -0500154 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500155 export <- gsub(paste0("\\[note", i, "\\]"),
156 paste0("\\\\footnotemark[", i, "]"),
157 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500158 }
Hao Zhuc5415632017-02-21 17:16:13 -0500159 } else {
Hao Zhudb04e302015-11-15 16:57:38 -0500160 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500161 for (i in 1:count.intablenote) {
162 export <- sub("\\[note\\]",
163 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
164 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500165 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500166
Hao Zhudc4b7142015-11-19 10:37:53 -0500167 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500168 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500169 export <- gsub(paste0("\\[note", i, "\\]"),
170 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
171 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500172 }
Hao Zhuc5415632017-02-21 17:16:13 -0500173 if (threeparttable == T) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500174 # generate footer with appropriate symbol
Hao Zhuf7994dd2017-02-27 16:58:42 -0500175 usepackage_latex("threeparttable")
Hao Zhudc4b7142015-11-19 10:37:53 -0500176 footer <- ""
Hao Zhuc5415632017-02-21 17:16:13 -0500177 for (i in 1:count.label) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500178 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
179 }
180
Hao Zhuc5415632017-02-21 17:16:13 -0500181 if (grepl("\\\\caption\\{.*?\\}", export)) {
182 export <- sub("\\\\caption\\{",
183 "\\\\begin{threeparttable}\n\\\\caption{",
184 export)
185 } else {
Hao Zhu245931c2017-09-01 22:43:56 -0400186 export <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"),
187 paste0("\\\\begin{threeparttable}\n\\\\begin{",
188 table_info$tabular, "}"),
Hao Zhuc5415632017-02-21 17:16:13 -0500189 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500190 }
191 export <- gsub(
192 "\\\\end\\{tabular\\}",
Hao Zhuc5415632017-02-21 17:16:13 -0500193 paste0("\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
194 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"),
Hao Zhudc4b7142015-11-19 10:37:53 -0500195 export)
Hao Zhuc5415632017-02-21 17:16:13 -0500196 } else {
197 table.width <- max(nchar(
198 str_replace_all(
Hao Zhu9b45a182017-02-27 18:17:46 -0500199 str_replace_all(table_info$contents, "\\[note\\]", ""),
200 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (table_info$ncol - 1)
Hao Zhuc5415632017-02-21 17:16:13 -0500201 footer <- ""
202 for (i in 1:count.label) {
203 label.wrap <- strwrap(label[i], table.width)
Hao Zhu9b45a182017-02-27 18:17:46 -0500204 footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
Hao Zhuc5415632017-02-21 17:16:13 -0500205 "}{l}{\\\\textsuperscript{", ids[i], "} ",
206 label.wrap[1], "}\\\\\\\\\n")
207 if (length(label.wrap) > 1) {
208 for (j in 2:length(label.wrap)) {
Hao Zhu9b45a182017-02-27 18:17:46 -0500209 footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
Hao Zhuc5415632017-02-21 17:16:13 -0500210 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
Hao Zhudc4b7142015-11-19 10:37:53 -0500211 }
212 }
Hao Zhuc5415632017-02-21 17:16:13 -0500213 }
Hao Zhu9f917482018-01-08 18:09:33 -0500214 export <- gsub(table_info$end_tabular,
215 paste0(footer, "\\\\end{", table_info$tabular, "}"),
Hao Zhuc5415632017-02-21 17:16:13 -0500216 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500217 }
Hao Zhudb04e302015-11-15 16:57:38 -0500218 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500219 }
Hao Zhuc5415632017-02-21 17:16:13 -0500220
221 # HTML Tables -------------------
222 if (attr(input, "format") == "html") {
Hao Zhu2203f662015-11-20 11:53:39 -0500223 # Clean the entry for labels
Hao Zhu9b45a182017-02-27 18:17:46 -0500224 table_info <- magic_mirror(input)
Hao Zhu245931c2017-09-01 22:43:56 -0400225 if (escape) {
226 label <- escape_html(label)
227 }
Hao Zhu2203f662015-11-20 11:53:39 -0500228 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500229 for (i in 1:count.intablenote) {
230 export <- sub("\\[note\\]",
231 paste0("<sup>", ids.intable[i], "</sup>"),
232 export)
Hao Zhu2203f662015-11-20 11:53:39 -0500233 }
234
235 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500236 for (i in extra.notation) {
Hao Zhu2203f662015-11-20 11:53:39 -0500237 export <- gsub(paste0("\\[note", i, "\\]"),
238 paste0("<sup>", ids.intable[i], "</sup>"),
239 export)
240 }
241
242 # Build footer
243 footer <- "<tfoot>\n"
Hao Zhuc5415632017-02-21 17:16:13 -0500244 for (i in 1:count.label) {
Hao Zhu2203f662015-11-20 11:53:39 -0500245 footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ",
246 "colspan='100%'><sup>", ids[i],"</sup> ",
247 label[i], "</td>\n</tr>\n")
248 }
249 footer <- paste0(footer, "</tfoot>\n")
250
251 # Paste footer to the table
252 export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1])
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500253 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400254 attr(export, "kable_meta") <- table_info
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500255 return(export)
256}