blob: eeb55e6a923a20039fbb3e98e79be8dd1dd0fa35 [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 Zhudc4b7142015-11-19 10:37:53 -050031 ids.simple <- c(
Hao Zhu8977a8a2015-11-19 16:52:21 -050032 "*", "\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"
Hao Zhudc4b7142015-11-19 10:37:53 -050038 )
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\\]"))
43 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
58
59
Hao Zhu4adea852015-11-16 16:38:34 -050060 # Footnote solution for markdown and pandoc. It is not perfect as
61 # markdown doesn't support complex table formats but this solution
62 # should be able to satisfy people who don't want to spend extra
Hao Zhue10cfd32017-02-21 16:41:14 -050063 # time to define their `kable` format.
64 if (!attr(input, "format") %in% c("html", "latex")) {
Hao Zhu4adea852015-11-16 16:38:34 -050065 # In table notation
Hao Zhue10cfd32017-02-21 16:41:14 -050066 if (count.intablenote != 0) {
67 for (i in 1:count.intablenote) {
68 replace_note <- paste0("^", ids.intable[i], "^",
69 paste0(rep(" ", 4 - ceiling(i/5)), collapse = ""))
70
Hao Zhudb04e302015-11-15 16:57:38 -050071 export[which(str_detect(export, "\\[note\\]"))[1]] <-
Hao Zhue10cfd32017-02-21 16:41:14 -050072 sub("\\[note\\]", replace_note,
73 export[which(str_detect(export, "\\[note\\]"))[1]])
Hao Zhudb04e302015-11-15 16:57:38 -050074 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050075 }
Hao Zhu4adea852015-11-16 16:38:34 -050076 # Fix extra in table notation
Hao Zhu4adea852015-11-16 16:38:34 -050077 for(i in extra.notation){
78 export <- gsub(paste0("\\[note", i, "\\]"),
79 paste0("^", ids.intable[i], "^",
Hao Zhudc4b7142015-11-19 10:37:53 -050080 paste0(rep(" ", 4 - ceiling(i/5)),
Hao Zhu4adea852015-11-16 16:38:34 -050081 collapse = "")),
82 export)
83 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050084
Hao Zhudb04e302015-11-15 16:57:38 -050085 export[length(export)+1] <- ""
86 export[length(export)+1] <- "__Note:__"
87 export[length(export)+1] <- paste0(
88 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
89 )
90 }
91
92 # Generate latex table footnote --------------------------------
Hao Zhub1bc0aa2015-11-12 11:23:42 -050093 if(attr(input, "format")=="latex"){
Hao Zhu2203f662015-11-20 11:53:39 -050094 # Clean the entry for labels
95 label <- knitr:::escape_latex(label)
96 label <- gsub("\\\\", "\\\\\\\\", label)
Hao Zhu8977a8a2015-11-19 16:52:21 -050097
Hao Zhu4adea852015-11-16 16:38:34 -050098 kable_info <- magic_mirror(input)
Hao Zhudc4b7142015-11-19 10:37:53 -050099 if(kable_info$tabular == "longtable"){
100 if(notation != "number"){
101 warning("Currently, if you enabled longtable in kable, you can only use",
102 " number as your footnote notations. ")
103 }
104 if(threeparttable == T){
105 warning("Currently, threeparttable does not support longtable.")
106 }
107 # If longtable is used, then use page footnote instead of threeparttable
108 # as it makes more sense to see the footnote at the bottom of page if
109 # table is longer than one page.
Hao Zhu4adea852015-11-16 16:38:34 -0500110
Hao Zhudc4b7142015-11-19 10:37:53 -0500111 # Longtable doesn't support footnote in caption directly.
112 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
113 count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]")
114 if (count.in.caption.note != 0){
115 # Since caption is the first part of table, we can just start
116 caption.footnote <- paste0("\\\\addtocounter{footnote}{-", count.in.caption.note, "}")
117 for(i in 1:count.in.caption.note){
118 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
119 caption.footnote <- paste0(
120 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", label[i], "}"
121 )
122 }
123
124 if (str_detect(export, "\\\\toprule")){
125 export <- sub("\\\\toprule",
126 paste0("\\\\toprule\n", caption.footnote), export)
127 }else{
128 export <- sub("\\\\hline",
129 paste0("\\\\hline\n", caption.footnote), export)
130 }
131 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500132 for(i in (count.in.caption.note + 1):count.intablenote){
Hao Zhu4adea852015-11-16 16:38:34 -0500133 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500134 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
135 }
136 for(i in extra.notation){
137 export <- gsub(paste0("\\[note", i, "\\]"),
138 paste0("\\\\footnotemark[", i, "]"),
139 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500140 }
141 }else{
Hao Zhudb04e302015-11-15 16:57:38 -0500142 # Replace in-table notation with appropriate symbol
Hao Zhue10cfd32017-02-21 16:41:14 -0500143 for(i in 1:count.intablenote){
Hao Zhudc4b7142015-11-19 10:37:53 -0500144 export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids.intable[i], "}"), export)
Hao Zhudb04e302015-11-15 16:57:38 -0500145 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500146
Hao Zhudc4b7142015-11-19 10:37:53 -0500147 # Fix extra in table notation
148 for(i in extra.notation){
149 export <- gsub(paste0("\\[note", i, "\\]"),
150 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
151 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500152 }
Hao Zhudc4b7142015-11-19 10:37:53 -0500153 if(threeparttable == T){
154 # generate footer with appropriate symbol
155 footer <- ""
156 for(i in 1:count.label){
157 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
158 }
159
160 if(grepl("\\\\caption\\{.*?\\}", export)){
161 export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export)
162 }else{
163 export <- sub("\\\\begin\\{tabular\\}",
164 "\\\\begin{threeparttable}\n\\\\begin{tabular}", export)
165 }
166 export <- gsub(
167 "\\\\end\\{tabular\\}",
168 paste0(
169 "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
170 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
171 ),
172 export)
173 }else{
174 table.width <- max(nchar(
175 str_replace_all(
176 str_replace_all(kable_info$contents, "\\[note\\]", ""),
177 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1)
178 footer <- ""
179 for (i in 1:count.label){
180 label.wrap <- strwrap(label[i], table.width)
181 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
182 "}{l}{\\\\textsuperscript{", ids[i], "} ",
183 label.wrap[1], "}\\\\\\\\\n")
184 if(length(label.wrap) > 1){
185 for (j in 2:length(label.wrap)){
186 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
187 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
188 }
189 }
190 }
191 export <- gsub("\\\\end\\{tabular\\}",
192 paste0(footer, "\\\\end{tabular}"), export)
193 }
Hao Zhudb04e302015-11-15 16:57:38 -0500194 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500195 }
196 if(attr(input, "format")=="html"){
Hao Zhu2203f662015-11-20 11:53:39 -0500197 # Clean the entry for labels
198 label <- knitr:::escape_html(label)
Hao Zhudc4b7142015-11-19 10:37:53 -0500199
Hao Zhu2203f662015-11-20 11:53:39 -0500200 # Replace in-table notation with appropriate symbol
Hao Zhue10cfd32017-02-21 16:41:14 -0500201 for(i in 1:count.intablenote){
Hao Zhu2203f662015-11-20 11:53:39 -0500202 export <- sub("\\[note\\]", paste0("<sup>", ids.intable[i], "</sup>"), export)
203 }
204
205 # Fix extra in table notation
206 for(i in extra.notation){
207 export <- gsub(paste0("\\[note", i, "\\]"),
208 paste0("<sup>", ids.intable[i], "</sup>"),
209 export)
210 }
211
212 # Build footer
213 footer <- "<tfoot>\n"
214 for(i in 1:count.label){
215 footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ",
216 "colspan='100%'><sup>", ids[i],"</sup> ",
217 label[i], "</td>\n</tr>\n")
218 }
219 footer <- paste0(footer, "</tfoot>\n")
220
221 # Paste footer to the table
222 export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1])
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500223 }
224 return(export)
225}