blob: abb618abc0ed4ac4a521c89df80fb610f00a1f05 [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
Hao Zhu6a076462017-03-01 12:59:01 -050014#' `number`, `alphabet` and `symbol`.
Hao Zhuf7994dd2017-02-27 16:58:42 -050015#' @param threeparttable Boolean value indicating if a
Hao Zhu78e61222017-05-24 20:53:35 -040016#' \href{https://www.ctan.org/pkg/threeparttable}{threeparttable} scheme should
17#' be used.
18#'
19#' @examples x <- knitr::kable(head(mtcars), "html")
20#' add_footnote(x, c("footnote 1", "footnote 2"), notation = "symbol")
Hao Zhub1bc0aa2015-11-12 11:23:42 -050021#'
22#' @export
Hao Zhue10cfd32017-02-21 16:41:14 -050023add_footnote <- function(input, label = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050024 notation = "alphabet",
Hao Zhu2203f662015-11-20 11:53:39 -050025 threeparttable = FALSE) {
Hao Zhue10cfd32017-02-21 16:41:14 -050026 if (is.null(label)) return(input)
Hao Zhu8977a8a2015-11-19 16:52:21 -050027
Hao Zhu9b45a182017-02-27 18:17:46 -050028 if (notation == "alphabet") {
29 notation <- getOption("kable_footnote_notation", "alphabet")
30 }
31 if (!threeparttable) {
32 threeparttable <- getOption("kable_footnote_threeparttable", FALSE)
33 }
34
35 notation <- match.arg(notation, c("alphabet", "number", "symbol"))
Hao Zhue10cfd32017-02-21 16:41:14 -050036 if (notation == "symbol") {
37 notation <- paste0(notation, ".", attr(input, "format"))
Hao Zhub1bc0aa2015-11-12 11:23:42 -050038 }
Hao Zhue10cfd32017-02-21 16:41:14 -050039
Hao Zhu9b45a182017-02-27 18:17:46 -050040 table_info <- NULL
41
Hao Zhue10cfd32017-02-21 16:41:14 -050042 ids.ops <- read.csv(system.file("symbol_index.csv", package = "kableExtra"))
43 ids <- ids.ops[, notation]
Hao Zhudb04e302015-11-15 16:57:38 -050044 ids.intable <- gsub("\\*", "\\\\*", ids)
45
46 #count the number of items in label and intable notation
Hao Zhue10cfd32017-02-21 16:41:14 -050047 count.label <- length(label)
48 count.intablenote <- sum(str_count(input, "\\[note\\]"))
Hao Zhuc5415632017-02-21 17:16:13 -050049 if (count.intablenote != 0 & count.label != count.intablenote) {
Hao Zhudb04e302015-11-15 16:57:38 -050050 warning(paste("You entered", count.label, "labels but you put",
Hao Zhue10cfd32017-02-21 16:41:14 -050051 count.intablenote, "[note] in your table."))
Hao Zhudb04e302015-11-15 16:57:38 -050052 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050053
Hao Zhu4adea852015-11-16 16:38:34 -050054 export <- input
55
Hao Zhudc4b7142015-11-19 10:37:53 -050056 # Find out if there are any extra in-table notations needed to be corrected
Hao Zhu2203f662015-11-20 11:53:39 -050057 extra.notation <- unique(as.numeric(
Hao Zhudc4b7142015-11-19 10:37:53 -050058 str_extract(
59 str_extract_all(
60 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
61 )[[1]],
Hao Zhu2203f662015-11-20 11:53:39 -050062 "[0-9]{1,2}")))
Hao Zhudc4b7142015-11-19 10:37:53 -050063
Hao Zhuc5415632017-02-21 17:16:13 -050064 # Pandoc ---------------------------
Hao Zhu4adea852015-11-16 16:38:34 -050065 # Footnote solution for markdown and pandoc. It is not perfect as
66 # markdown doesn't support complex table formats but this solution
67 # should be able to satisfy people who don't want to spend extra
Hao Zhue10cfd32017-02-21 16:41:14 -050068 # time to define their `kable` format.
69 if (!attr(input, "format") %in% c("html", "latex")) {
Hao Zhu4adea852015-11-16 16:38:34 -050070 # In table notation
Hao Zhue10cfd32017-02-21 16:41:14 -050071 if (count.intablenote != 0) {
72 for (i in 1:count.intablenote) {
73 replace_note <- paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050074 paste0(rep(" ", 4 - ceiling(i/5)), collapse = ""))
Hao Zhue10cfd32017-02-21 16:41:14 -050075
Hao Zhudb04e302015-11-15 16:57:38 -050076 export[which(str_detect(export, "\\[note\\]"))[1]] <-
Hao Zhue10cfd32017-02-21 16:41:14 -050077 sub("\\[note\\]", replace_note,
78 export[which(str_detect(export, "\\[note\\]"))[1]])
Hao Zhudb04e302015-11-15 16:57:38 -050079 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050080 }
Hao Zhu4adea852015-11-16 16:38:34 -050081 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -050082 for (i in extra.notation) {
Hao Zhu4adea852015-11-16 16:38:34 -050083 export <- gsub(paste0("\\[note", i, "\\]"),
84 paste0("^", ids.intable[i], "^",
Hao Zhuc5415632017-02-21 17:16:13 -050085 paste0(rep(" ", 4 - ceiling(i/5)), collapse = "")),
Hao Zhu4adea852015-11-16 16:38:34 -050086 export)
87 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050088
Hao Zhuc5415632017-02-21 17:16:13 -050089 export[length(export) + 1] <- ""
90 export[length(export) + 1] <- "__Note:__"
91 export[length(export) + 1] <- paste0(
Hao Zhudb04e302015-11-15 16:57:38 -050092 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
Hao Zhuc5415632017-02-21 17:16:13 -050093 )
94 }
Hao Zhudb04e302015-11-15 16:57:38 -050095
Hao Zhuc5415632017-02-21 17:16:13 -050096 # LaTeX Tables --------------------------------
97 if (attr(input, "format") == "latex") {
Hao Zhu2203f662015-11-20 11:53:39 -050098 # Clean the entry for labels
Hao Zhuc5415632017-02-21 17:16:13 -050099 label <- escape_latex(label)
100 label <- gsub("\\\\", "\\\\\\\\", label)
Hao Zhu8977a8a2015-11-19 16:52:21 -0500101
Hao Zhud2c0f732017-08-26 10:40:14 -0400102 export <- enc2utf8(export)
Hao Zhu9b45a182017-02-27 18:17:46 -0500103 table_info <- magic_mirror(input)
104 if (table_info$tabular == "longtable") {
Hao Zhuc5415632017-02-21 17:16:13 -0500105 if (notation != "number") {
106 warning("Notation is set to 'number' and other formats are not supported.")
107 notation <- "number"
Hao Zhudc4b7142015-11-19 10:37:53 -0500108 }
109 # If longtable is used, then use page footnote instead of threeparttable
110 # as it makes more sense to see the footnote at the bottom of page if
111 # table is longer than one page.
Hao Zhuc5415632017-02-21 17:16:13 -0500112 if (threeparttable == T) {
113 warning("Threeparttable does not support longtable.")
114 threeparttable = F
115 }
Hao Zhu4adea852015-11-16 16:38:34 -0500116
Hao Zhudc4b7142015-11-19 10:37:53 -0500117 # Longtable doesn't support footnote in caption directly.
118 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
Hao Zhuc5415632017-02-21 17:16:13 -0500119
120 count.in.caption.note <- 0
Hao Zhu9b45a182017-02-27 18:17:46 -0500121 if (!is.na(table_info$caption)) {
122 count.in.caption.note <- str_count(table_info$caption, "\\[note\\]")
Hao Zhuc5415632017-02-21 17:16:13 -0500123 }
124 if (count.in.caption.note != 0) {
125 caption.footnote <- paste0("\\\\addtocounter{footnote}{-",
126 count.in.caption.note, "}")
127 for (i in 1:count.in.caption.note) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500128 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
129 caption.footnote <- paste0(
Hao Zhuc5415632017-02-21 17:16:13 -0500130 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{",
131 label[i], "}")
Hao Zhudc4b7142015-11-19 10:37:53 -0500132 }
133
Hao Zhuc5415632017-02-21 17:16:13 -0500134 if (str_detect(export, "\\\\toprule")) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500135 export <- sub("\\\\toprule",
136 paste0("\\\\toprule\n", caption.footnote), export)
Hao Zhuc5415632017-02-21 17:16:13 -0500137 } else {
Hao Zhudc4b7142015-11-19 10:37:53 -0500138 export <- sub("\\\\hline",
139 paste0("\\\\hline\n", caption.footnote), export)
140 }
141 }
Hao Zhuc5415632017-02-21 17:16:13 -0500142 for (i in (count.in.caption.note + 1):count.intablenote) {
Hao Zhu4adea852015-11-16 16:38:34 -0500143 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500144 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
145 }
Hao Zhuc5415632017-02-21 17:16:13 -0500146 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500147 export <- gsub(paste0("\\[note", i, "\\]"),
148 paste0("\\\\footnotemark[", i, "]"),
149 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500150 }
Hao Zhuc5415632017-02-21 17:16:13 -0500151 } else {
Hao Zhudb04e302015-11-15 16:57:38 -0500152 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500153 for (i in 1:count.intablenote) {
154 export <- sub("\\[note\\]",
155 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
156 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500157 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500158
Hao Zhudc4b7142015-11-19 10:37:53 -0500159 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500160 for (i in extra.notation) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500161 export <- gsub(paste0("\\[note", i, "\\]"),
162 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
163 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500164 }
Hao Zhuc5415632017-02-21 17:16:13 -0500165 if (threeparttable == T) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500166 # generate footer with appropriate symbol
Hao Zhuf7994dd2017-02-27 16:58:42 -0500167 usepackage_latex("threeparttable")
Hao Zhudc4b7142015-11-19 10:37:53 -0500168 footer <- ""
Hao Zhuc5415632017-02-21 17:16:13 -0500169 for (i in 1:count.label) {
Hao Zhudc4b7142015-11-19 10:37:53 -0500170 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
171 }
172
Hao Zhuc5415632017-02-21 17:16:13 -0500173 if (grepl("\\\\caption\\{.*?\\}", export)) {
174 export <- sub("\\\\caption\\{",
175 "\\\\begin{threeparttable}\n\\\\caption{",
176 export)
177 } else {
Hao Zhudc4b7142015-11-19 10:37:53 -0500178 export <- sub("\\\\begin\\{tabular\\}",
Hao Zhuc5415632017-02-21 17:16:13 -0500179 "\\\\begin{threeparttable}\n\\\\begin{tabular}",
180 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500181 }
182 export <- gsub(
183 "\\\\end\\{tabular\\}",
Hao Zhuc5415632017-02-21 17:16:13 -0500184 paste0("\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
185 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"),
Hao Zhudc4b7142015-11-19 10:37:53 -0500186 export)
Hao Zhuc5415632017-02-21 17:16:13 -0500187 } else {
188 table.width <- max(nchar(
189 str_replace_all(
Hao Zhu9b45a182017-02-27 18:17:46 -0500190 str_replace_all(table_info$contents, "\\[note\\]", ""),
191 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (table_info$ncol - 1)
Hao Zhuc5415632017-02-21 17:16:13 -0500192 footer <- ""
193 for (i in 1:count.label) {
194 label.wrap <- strwrap(label[i], table.width)
Hao Zhu9b45a182017-02-27 18:17:46 -0500195 footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
Hao Zhuc5415632017-02-21 17:16:13 -0500196 "}{l}{\\\\textsuperscript{", ids[i], "} ",
197 label.wrap[1], "}\\\\\\\\\n")
198 if (length(label.wrap) > 1) {
199 for (j in 2:length(label.wrap)) {
Hao Zhu9b45a182017-02-27 18:17:46 -0500200 footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
Hao Zhuc5415632017-02-21 17:16:13 -0500201 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
Hao Zhudc4b7142015-11-19 10:37:53 -0500202 }
203 }
Hao Zhuc5415632017-02-21 17:16:13 -0500204 }
205 export <- gsub("\\\\end\\{tabular\\}",
206 paste0(footer, "\\\\end{tabular}"),
207 export)
Hao Zhudc4b7142015-11-19 10:37:53 -0500208 }
Hao Zhudb04e302015-11-15 16:57:38 -0500209 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500210 }
Hao Zhuc5415632017-02-21 17:16:13 -0500211
212 # HTML Tables -------------------
213 if (attr(input, "format") == "html") {
Hao Zhu2203f662015-11-20 11:53:39 -0500214 # Clean the entry for labels
Hao Zhu9b45a182017-02-27 18:17:46 -0500215 table_info <- magic_mirror(input)
Hao Zhuc5415632017-02-21 17:16:13 -0500216 label <- escape_html(label)
Hao Zhu2203f662015-11-20 11:53:39 -0500217 # Replace in-table notation with appropriate symbol
Hao Zhuc5415632017-02-21 17:16:13 -0500218 for (i in 1:count.intablenote) {
219 export <- sub("\\[note\\]",
220 paste0("<sup>", ids.intable[i], "</sup>"),
221 export)
Hao Zhu2203f662015-11-20 11:53:39 -0500222 }
223
224 # Fix extra in table notation
Hao Zhuc5415632017-02-21 17:16:13 -0500225 for (i in extra.notation) {
Hao Zhu2203f662015-11-20 11:53:39 -0500226 export <- gsub(paste0("\\[note", i, "\\]"),
227 paste0("<sup>", ids.intable[i], "</sup>"),
228 export)
229 }
230
231 # Build footer
232 footer <- "<tfoot>\n"
Hao Zhuc5415632017-02-21 17:16:13 -0500233 for (i in 1:count.label) {
Hao Zhu2203f662015-11-20 11:53:39 -0500234 footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ",
235 "colspan='100%'><sup>", ids[i],"</sup> ",
236 label[i], "</td>\n</tr>\n")
237 }
238 footer <- paste0(footer, "</tfoot>\n")
239
240 # Paste footer to the table
241 export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1])
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500242 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400243 attr(export, "kable_meta") <- table_info
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500244 return(export)
245}