blob: d849779be4544a0c066ff6b1d5c66cf9b8fc2346 [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".
15#'
16#' @export
Hao Zhu2203f662015-11-20 11:53:39 -050017add_footnote <- function(input, label = NULL, notation = "number",
18 threeparttable = FALSE) {
Hao Zhub1bc0aa2015-11-12 11:23:42 -050019 if (is.null(label)){return(input)}
Hao Zhu8977a8a2015-11-19 16:52:21 -050020
Hao Zhub1bc0aa2015-11-12 11:23:42 -050021 # Define available id list
22 if (!notation %in% c("number", "alphabet", "symbol")){
Hao Zhu4adea852015-11-16 16:38:34 -050023 warning('Please select your notation within "number", "alphabet" and ',
24 '"symbol". Now add_footnote is using "alphabet" as default.')
Hao Zhub1bc0aa2015-11-12 11:23:42 -050025 }
26 if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))}
27 ids.ops <- data.frame(
28 number = as.character(1:20),
29 alphabet = letters[1:20],
30 symbol.latex = c(
31 "*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P",
32 "**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P",
Hao Zhu4adea852015-11-16 16:38:34 -050033 "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag",
34 "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P",
35 "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag",
36 "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P"
Hao Zhub1bc0aa2015-11-12 11:23:42 -050037 ),
38 symbol.html = c(
39 "*", "&dagger;", "&Dagger;", "&sect;", "&para;",
40 "**", "&dagger;&dagger;", "&Dagger;&Dagger;", "&sect;&sect;", "&para;&para;",
Hao Zhu4adea852015-11-16 16:38:34 -050041 "*", "&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;",
42 "&sect;&sect;&sect;", "&para;&para;&para;",
43 "**", "&dagger;&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;&Dagger;",
44 "&sect;&sect;&sect;&sect;", "&para;&para;&para;&para;"
Hao Zhudb04e302015-11-15 16:57:38 -050045 ),
46 symbol.markdown = c(
Hao Zhu8977a8a2015-11-19 16:52:21 -050047 "\\*", "\u2020", "\u2021", "\u00A7", "\u00B6",
48 "\\*\\*", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
49 "\\*\\*\\*", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
50 "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
51 "\\*\\*\\*\\*", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
52 "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
Hao Zhudb04e302015-11-15 16:57:38 -050053 ),
54 symbol.pandoc = c(
Hao Zhu8977a8a2015-11-19 16:52:21 -050055 "\\*", "\u2020", "\u2021", "\u00A7", "\u00B6",
56 "\\*\\*", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
57 "\\*\\*\\*", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
58 "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
59 "\\*\\*\\*\\*", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
60 "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
Hao Zhub1bc0aa2015-11-12 11:23:42 -050061 )
62 )
63 ids <- ids.ops[,notation]
Hao Zhu4adea852015-11-16 16:38:34 -050064 # pandoc cannot recognize ^*^ as * is a special character. We have to use ^\*^
Hao Zhudb04e302015-11-15 16:57:38 -050065 ids.intable <- gsub("\\*", "\\\\*", ids)
Hao Zhudc4b7142015-11-19 10:37:53 -050066 ids.simple <- c(
Hao Zhu8977a8a2015-11-19 16:52:21 -050067 "*", "\u2020", "\u2021", "\u00A7", "\u00B6",
68 "**", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
69 "***", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
70 "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
71 "****", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
72 "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
Hao Zhudc4b7142015-11-19 10:37:53 -050073 )
Hao Zhudb04e302015-11-15 16:57:38 -050074
75 #count the number of items in label and intable notation
76 count.label = length(label)
77 count.intablenoot = sum(str_count(input, "\\[note\\]"))
78 if (count.intablenoot != 0 & count.label != count.intablenoot){
79 warning(paste("You entered", count.label, "labels but you put",
80 count.intablenoot, "[note] in your table."))
81 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050082
Hao Zhu4adea852015-11-16 16:38:34 -050083 export <- input
84
Hao Zhudc4b7142015-11-19 10:37:53 -050085 # Find out if there are any extra in-table notations needed to be corrected
Hao Zhu2203f662015-11-20 11:53:39 -050086 extra.notation <- unique(as.numeric(
Hao Zhudc4b7142015-11-19 10:37:53 -050087 str_extract(
88 str_extract_all(
89 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
90 )[[1]],
Hao Zhu2203f662015-11-20 11:53:39 -050091 "[0-9]{1,2}")))
Hao Zhudc4b7142015-11-19 10:37:53 -050092
93
94
Hao Zhu4adea852015-11-16 16:38:34 -050095 # Footnote solution for markdown and pandoc. It is not perfect as
96 # markdown doesn't support complex table formats but this solution
97 # should be able to satisfy people who don't want to spend extra
98 # time to define their `kable` output.
Hao Zhub1bc0aa2015-11-12 11:23:42 -050099 if(!attr(input, "format") %in% c("html", "latex")){
Hao Zhu4adea852015-11-16 16:38:34 -0500100 # In table notation
Hao Zhudb04e302015-11-15 16:57:38 -0500101 if(count.intablenoot != 0){
102 for(i in 1:count.intablenoot){
103 export[which(str_detect(export, "\\[note\\]"))[1]] <-
104 sub("\\[note\\]", paste0("^", ids.intable[i], "^",
Hao Zhudc4b7142015-11-19 10:37:53 -0500105 paste0(rep(" ", 4 - ceiling(i/5)),
Hao Zhudb04e302015-11-15 16:57:38 -0500106 collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]])
107 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500108 }
Hao Zhu4adea852015-11-16 16:38:34 -0500109 # Fix extra in table notation
Hao Zhu4adea852015-11-16 16:38:34 -0500110 for(i in extra.notation){
111 export <- gsub(paste0("\\[note", i, "\\]"),
112 paste0("^", ids.intable[i], "^",
Hao Zhudc4b7142015-11-19 10:37:53 -0500113 paste0(rep(" ", 4 - ceiling(i/5)),
Hao Zhu4adea852015-11-16 16:38:34 -0500114 collapse = "")),
115 export)
116 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500117
Hao Zhudb04e302015-11-15 16:57:38 -0500118 export[length(export)+1] <- ""
119 export[length(export)+1] <- "__Note:__"
120 export[length(export)+1] <- paste0(
121 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
122 )
123 }
124
125 # Generate latex table footnote --------------------------------
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500126 if(attr(input, "format")=="latex"){
Hao Zhu2203f662015-11-20 11:53:39 -0500127 # Clean the entry for labels
128 label <- knitr:::escape_latex(label)
129 label <- gsub("\\\\", "\\\\\\\\", label)
Hao Zhu8977a8a2015-11-19 16:52:21 -0500130
Hao Zhu4adea852015-11-16 16:38:34 -0500131 kable_info <- magic_mirror(input)
Hao Zhudc4b7142015-11-19 10:37:53 -0500132 if(kable_info$tabular == "longtable"){
133 if(notation != "number"){
134 warning("Currently, if you enabled longtable in kable, you can only use",
135 " number as your footnote notations. ")
136 }
137 if(threeparttable == T){
138 warning("Currently, threeparttable does not support longtable.")
139 }
140 # If longtable is used, then use page footnote instead of threeparttable
141 # as it makes more sense to see the footnote at the bottom of page if
142 # table is longer than one page.
Hao Zhu4adea852015-11-16 16:38:34 -0500143
Hao Zhudc4b7142015-11-19 10:37:53 -0500144 # Longtable doesn't support footnote in caption directly.
145 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
146 count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]")
147 if (count.in.caption.note != 0){
148 # Since caption is the first part of table, we can just start
149 caption.footnote <- paste0("\\\\addtocounter{footnote}{-", count.in.caption.note, "}")
150 for(i in 1:count.in.caption.note){
151 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
152 caption.footnote <- paste0(
153 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", label[i], "}"
154 )
155 }
156
157 if (str_detect(export, "\\\\toprule")){
158 export <- sub("\\\\toprule",
159 paste0("\\\\toprule\n", caption.footnote), export)
160 }else{
161 export <- sub("\\\\hline",
162 paste0("\\\\hline\n", caption.footnote), export)
163 }
164 }
165 for(i in (count.in.caption.note + 1):count.intablenoot){
Hao Zhu4adea852015-11-16 16:38:34 -0500166 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500167 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
168 }
169 for(i in extra.notation){
170 export <- gsub(paste0("\\[note", i, "\\]"),
171 paste0("\\\\footnotemark[", i, "]"),
172 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500173 }
174 }else{
Hao Zhudb04e302015-11-15 16:57:38 -0500175 # Replace in-table notation with appropriate symbol
176 for(i in 1:count.intablenoot){
Hao Zhudc4b7142015-11-19 10:37:53 -0500177 export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids.intable[i], "}"), export)
Hao Zhudb04e302015-11-15 16:57:38 -0500178 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500179
Hao Zhudc4b7142015-11-19 10:37:53 -0500180 # Fix extra in table notation
181 for(i in extra.notation){
182 export <- gsub(paste0("\\[note", i, "\\]"),
183 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
184 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500185 }
Hao Zhudc4b7142015-11-19 10:37:53 -0500186 if(threeparttable == T){
187 # generate footer with appropriate symbol
188 footer <- ""
189 for(i in 1:count.label){
190 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
191 }
192
193 if(grepl("\\\\caption\\{.*?\\}", export)){
194 export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export)
195 }else{
196 export <- sub("\\\\begin\\{tabular\\}",
197 "\\\\begin{threeparttable}\n\\\\begin{tabular}", export)
198 }
199 export <- gsub(
200 "\\\\end\\{tabular\\}",
201 paste0(
202 "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
203 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
204 ),
205 export)
206 }else{
207 table.width <- max(nchar(
208 str_replace_all(
209 str_replace_all(kable_info$contents, "\\[note\\]", ""),
210 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1)
211 footer <- ""
212 for (i in 1:count.label){
213 label.wrap <- strwrap(label[i], table.width)
214 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
215 "}{l}{\\\\textsuperscript{", ids[i], "} ",
216 label.wrap[1], "}\\\\\\\\\n")
217 if(length(label.wrap) > 1){
218 for (j in 2:length(label.wrap)){
219 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
220 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
221 }
222 }
223 }
224 export <- gsub("\\\\end\\{tabular\\}",
225 paste0(footer, "\\\\end{tabular}"), export)
226 }
Hao Zhudb04e302015-11-15 16:57:38 -0500227 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500228 }
229 if(attr(input, "format")=="html"){
Hao Zhu2203f662015-11-20 11:53:39 -0500230 # Clean the entry for labels
231 label <- knitr:::escape_html(label)
Hao Zhudc4b7142015-11-19 10:37:53 -0500232
Hao Zhu2203f662015-11-20 11:53:39 -0500233 # Replace in-table notation with appropriate symbol
234 for(i in 1:count.intablenoot){
235 export <- sub("\\[note\\]", paste0("<sup>", ids.intable[i], "</sup>"), export)
236 }
237
238 # Fix extra in table notation
239 for(i in extra.notation){
240 export <- gsub(paste0("\\[note", i, "\\]"),
241 paste0("<sup>", ids.intable[i], "</sup>"),
242 export)
243 }
244
245 # Build footer
246 footer <- "<tfoot>\n"
247 for(i in 1:count.label){
248 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 }
257 return(export)
258}