blob: feb12693fef37d8cbac071eb0c9a1a088336426f [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 Zhu6a076462017-03-01 12:59:01 -050010#' `number`, `alphabet` and `symbol`.
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
34 notation <- match.arg(notation, c("alphabet", "number", "symbol"))
Hao Zhue10cfd32017-02-21 16:41:14 -050035 if (notation == "symbol") {
36 notation <- paste0(notation, ".", attr(input, "format"))
Hao Zhub1bc0aa2015-11-12 11:23:42 -050037 }
Hao Zhue10cfd32017-02-21 16:41:14 -050038
Hao Zhu9b45a182017-02-27 18:17:46 -050039 table_info <- NULL
40
Hao Zhue10cfd32017-02-21 16:41:14 -050041 ids.ops <- read.csv(system.file("symbol_index.csv", package = "kableExtra"))
42 ids <- ids.ops[, notation]
Hao Zhudb04e302015-11-15 16:57:38 -050043 ids.intable <- gsub("\\*", "\\\\*", ids)
44
45 #count the number of items in label and intable notation
Hao Zhue10cfd32017-02-21 16:41:14 -050046 count.label <- length(label)
47 count.intablenote <- sum(str_count(input, "\\[note\\]"))
Hao Zhuc5415632017-02-21 17:16:13 -050048 if (count.intablenote != 0 & count.label != count.intablenote) {
Hao Zhudb04e302015-11-15 16:57:38 -050049 warning(paste("You entered", count.label, "labels but you put",
Hao Zhue10cfd32017-02-21 16:41:14 -050050 count.intablenote, "[note] in your table."))
Hao Zhudb04e302015-11-15 16:57:38 -050051 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050052
Hao Zhu4adea852015-11-16 16:38:34 -050053 export <- input
54
Hao Zhudc4b7142015-11-19 10:37:53 -050055 # Find out if there are any extra in-table notations needed to be corrected
Hao Zhu2203f662015-11-20 11:53:39 -050056 extra.notation <- unique(as.numeric(
Hao Zhudc4b7142015-11-19 10:37:53 -050057 str_extract(
58 str_extract_all(
59 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
60 )[[1]],
Hao Zhu2203f662015-11-20 11:53:39 -050061 "[0-9]{1,2}")))
Hao Zhudc4b7142015-11-19 10:37:53 -050062
Hao Zhuc5415632017-02-21 17:16:13 -050063 # Pandoc ---------------------------
Hao Zhu4adea852015-11-16 16:38:34 -050064 # Footnote solution for markdown and pandoc. It is not perfect as
65 # markdown doesn't support complex table formats but this solution
66 # should be able to satisfy people who don't want to spend extra
Hao Zhue10cfd32017-02-21 16:41:14 -050067 # time to define their `kable` format.
68 if (!attr(input, "format") %in% c("html", "latex")) {
Hao Zhu4adea852015-11-16 16:38:34 -050069 # In table notation
Hao Zhue10cfd32017-02-21 16:41:14 -050070 if (count.intablenote != 0) {
71 for (i in 1:count.intablenote) {
72 replace_note <- paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050073 paste0(rep(" ", 4 - ceiling(i/5)), collapse = ""))
Hao Zhue10cfd32017-02-21 16:41:14 -050074
Hao Zhudb04e302015-11-15 16:57:38 -050075 export[which(str_detect(export, "\\[note\\]"))[1]] <-
Hao Zhue10cfd32017-02-21 16:41:14 -050076 sub("\\[note\\]", replace_note,
77 export[which(str_detect(export, "\\[note\\]"))[1]])
Hao Zhudb04e302015-11-15 16:57:38 -050078 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050079 }
Hao Zhu4adea852015-11-16 16:38:34 -050080 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -050081 for (i in extra.notation) {
Hao Zhu4adea852015-11-16 16:38:34 -050082 export <- gsub(paste0("\\[note", i, "\\]"),
83 paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050084 paste0(rep(" ", 4 - ceiling(i/5)), collapse = "")),
Hao Zhu4adea852015-11-16 16:38:34 -050085 export)
86 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050087
Hao Zhuc5415632017-02-21 17:16:13 -050088 export[length(export) + 1] <- ""
89 export[length(export) + 1] <- "__Note:__"
90 export[length(export) + 1] <- paste0(
Hao Zhudb04e302015-11-15 16:57:38 -050091 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
Hao Zhuc5415632017-02-21 17:16:13 -050092 )
93 }
Hao Zhudb04e302015-11-15 16:57:38 -050094
Hao Zhuc5415632017-02-21 17:16:13 -050095 # LaTeX Tables --------------------------------
96 if (attr(input, "format") == "latex") {
Hao Zhu2203f662015-11-20 11:53:39 -050097 # Clean the entry for labels
Hao Zhu245931c2017-09-01 22:43:56 -040098 if (escape) {
99 label <- escape_latex(label)
Hao Zhu1aff7342018-04-02 18:33:15 -0400100 label <- linebreak(label)
Hao Zhu245931c2017-09-01 22:43:56 -0400101 }
Hao Zhuc5415632017-02-21 17:16:13 -0500102 label <- gsub("\\\\", "\\\\\\\\", label)
Hao Zhu8977a8a2015-11-19 16:52:21 -0500103
Hao Zhu904c0dc2018-04-04 16:14:32 -0400104 export <- enc2utf8(export)
Hao Zhu9b45a182017-02-27 18:17:46 -0500105 table_info <- magic_mirror(input)
106 if (table_info$tabular == "longtable") {
Hao Zhuc5415632017-02-21 17:16:13 -0500107 if (notation != "number") {
108 warning("Notation is set to 'number' and other formats are not supported.")
109 notation <- "number"
Hao Zhudc4b7142015-11-19 10:37:53 -0500110 }
111 # If longtable is used, then use page footnote instead of threeparttable
112 # as it makes more sense to see the footnote at the bottom of page if
113 # table is longer than one page.
Hao Zhuc5415632017-02-21 17:16:13 -0500114 if (threeparttable == T) {
115 warning("Threeparttable does not support longtable.")
116 threeparttable = F
117 }
Hao Zhu4adea852015-11-16 16:38:34 -0500118
Hao Zhudc4b7142015-11-19 10:37:53 -0500119 # Longtable doesn't support footnote in caption directly.
120 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
Hao Zhuc5415632017-02-21 17:16:13 -0500121
122 count.in.caption.note <- 0
Hao Zhu9b45a182017-02-27 18:17:46 -0500123 if (!is.na(table_info$caption)) {
124 count.in.caption.note <- str_count(table_info$caption, "\\[note\\]")
Hao Zhuc5415632017-02-21 17:16:13 -0500125 }
126 if (count.in.caption.note != 0) {
127 caption.footnote <- paste0("\\\\addtocounter{footnote}{-",
128 count.in.caption.note, "}")
129 for (i in 1:count.in.caption.note) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500130 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
131 caption.footnote <- paste0(
Hao Zhuc5415632017-02-21 17:16:13 -0500132 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{",
133 label[i], "}")
Hao Zhudc4b7142015-11-19 10:37:53 -0500134 }
135
Hao Zhuc5415632017-02-21 17:16:13 -0500136 if (str_detect(export, "\\\\toprule")) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500137 export <- sub("\\\\toprule",
138 paste0("\\\\toprule\n", caption.footnote), export)
Hao Zhuc5415632017-02-21 17:16:13 -0500139 } else {
Hao Zhudc4b7142015-11-19 10:37:53 -0500140 export <- sub("\\\\hline",
141 paste0("\\\\hline\n", caption.footnote), export)
142 }
143 }
Hao Zhuc5415632017-02-21 17:16:13 -0500144 for (i in (count.in.caption.note + 1):count.intablenote) {
Hao Zhu4adea852015-11-16 16:38:34 -0500145 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500146 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
147 }
Hao Zhuc5415632017-02-21 17:16:13 -0500148 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500149 export <- gsub(paste0("\\[note", i, "\\]"),
150 paste0("\\\\footnotemark[", i, "]"),
151 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500152 }
Hao Zhuc5415632017-02-21 17:16:13 -0500153 } else {
Hao Zhudb04e302015-11-15 16:57:38 -0500154 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500155 for (i in 1:count.intablenote) {
156 export <- sub("\\[note\\]",
157 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
158 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500159 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500160
Hao Zhudc4b7142015-11-19 10:37:53 -0500161 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500162 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500163 export <- gsub(paste0("\\[note", i, "\\]"),
164 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
165 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500166 }
Hao Zhuc5415632017-02-21 17:16:13 -0500167 if (threeparttable == T) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500168 # generate footer with appropriate symbol
Hao Zhuf7994dd2017-02-27 16:58:42 -0500169 usepackage_latex("threeparttable")
Hao Zhudc4b7142015-11-19 10:37:53 -0500170 footer <- ""
Hao Zhuc5415632017-02-21 17:16:13 -0500171 for (i in 1:count.label) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500172 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
173 }
174
Hao Zhuc5415632017-02-21 17:16:13 -0500175 if (grepl("\\\\caption\\{.*?\\}", export)) {
176 export <- sub("\\\\caption\\{",
177 "\\\\begin{threeparttable}\n\\\\caption{",
178 export)
179 } else {
Hao Zhu245931c2017-09-01 22:43:56 -0400180 export <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"),
181 paste0("\\\\begin{threeparttable}\n\\\\begin{",
182 table_info$tabular, "}"),
Hao Zhuc5415632017-02-21 17:16:13 -0500183 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500184 }
185 export <- gsub(
186 "\\\\end\\{tabular\\}",
Hao Zhuc5415632017-02-21 17:16:13 -0500187 paste0("\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
188 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"),
Hao Zhudc4b7142015-11-19 10:37:53 -0500189 export)
Hao Zhuc5415632017-02-21 17:16:13 -0500190 } else {
191 table.width <- max(nchar(
192 str_replace_all(
Hao Zhu9b45a182017-02-27 18:17:46 -0500193 str_replace_all(table_info$contents, "\\[note\\]", ""),
194 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (table_info$ncol - 1)
Hao Zhuc5415632017-02-21 17:16:13 -0500195 footer <- ""
196 for (i in 1:count.label) {
197 label.wrap <- strwrap(label[i], table.width)
Hao Zhu9b45a182017-02-27 18:17:46 -0500198 footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
Hao Zhuc5415632017-02-21 17:16:13 -0500199 "}{l}{\\\\textsuperscript{", ids[i], "} ",
200 label.wrap[1], "}\\\\\\\\\n")
201 if (length(label.wrap) > 1) {
202 for (j in 2:length(label.wrap)) {
Hao Zhu9b45a182017-02-27 18:17:46 -0500203 footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
Hao Zhuc5415632017-02-21 17:16:13 -0500204 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
Hao Zhudc4b7142015-11-19 10:37:53 -0500205 }
206 }
Hao Zhuc5415632017-02-21 17:16:13 -0500207 }
Hao Zhu9f917482018-01-08 18:09:33 -0500208 export <- gsub(table_info$end_tabular,
209 paste0(footer, "\\\\end{", table_info$tabular, "}"),
Hao Zhuc5415632017-02-21 17:16:13 -0500210 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500211 }
Hao Zhudb04e302015-11-15 16:57:38 -0500212 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500213 }
Hao Zhuc5415632017-02-21 17:16:13 -0500214
215 # HTML Tables -------------------
216 if (attr(input, "format") == "html") {
Hao Zhu2203f662015-11-20 11:53:39 -0500217 # Clean the entry for labels
Hao Zhu9b45a182017-02-27 18:17:46 -0500218 table_info <- magic_mirror(input)
Hao Zhu245931c2017-09-01 22:43:56 -0400219 if (escape) {
220 label <- escape_html(label)
221 }
Hao Zhu2203f662015-11-20 11:53:39 -0500222 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500223 for (i in 1:count.intablenote) {
224 export <- sub("\\[note\\]",
225 paste0("<sup>", ids.intable[i], "</sup>"),
226 export)
Hao Zhu2203f662015-11-20 11:53:39 -0500227 }
228
229 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500230 for (i in extra.notation) {
Hao Zhu2203f662015-11-20 11:53:39 -0500231 export <- gsub(paste0("\\[note", i, "\\]"),
232 paste0("<sup>", ids.intable[i], "</sup>"),
233 export)
234 }
235
236 # Build footer
237 footer <- "<tfoot>\n"
Hao Zhuc5415632017-02-21 17:16:13 -0500238 for (i in 1:count.label) {
Hao Zhu2203f662015-11-20 11:53:39 -0500239 footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ",
240 "colspan='100%'><sup>", ids[i],"</sup> ",
241 label[i], "</td>\n</tr>\n")
242 }
243 footer <- paste0(footer, "</tfoot>\n")
244
245 # Paste footer to the table
246 export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1])
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500247 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400248 attr(export, "kable_meta") <- table_info
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500249 return(export)
250}