blob: 73d18f5b053d91ad7822152ab3d3719a815ece00 [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 Zhu2203f662015-11-20 11:53:39 -050018add_footnote <- function(input, label = NULL, notation = "number",
19 threeparttable = FALSE) {
Hao Zhub1bc0aa2015-11-12 11:23:42 -050020 if (is.null(label)){return(input)}
Hao Zhu8977a8a2015-11-19 16:52:21 -050021
Hao Zhub1bc0aa2015-11-12 11:23:42 -050022 # Define available id list
23 if (!notation %in% c("number", "alphabet", "symbol")){
Hao Zhu4adea852015-11-16 16:38:34 -050024 warning('Please select your notation within "number", "alphabet" and ',
25 '"symbol". Now add_footnote is using "alphabet" as default.')
Hao Zhub1bc0aa2015-11-12 11:23:42 -050026 }
27 if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))}
28 ids.ops <- data.frame(
29 number = as.character(1:20),
30 alphabet = letters[1:20],
31 symbol.latex = c(
32 "*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P",
33 "**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P",
Hao Zhu4adea852015-11-16 16:38:34 -050034 "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag",
35 "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P",
36 "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag",
37 "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P"
Hao Zhub1bc0aa2015-11-12 11:23:42 -050038 ),
39 symbol.html = c(
40 "*", "&dagger;", "&Dagger;", "&sect;", "&para;",
41 "**", "&dagger;&dagger;", "&Dagger;&Dagger;", "&sect;&sect;", "&para;&para;",
Hao Zhue0a36a82015-11-23 15:35:20 -050042 "***", "&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;",
Hao Zhu4adea852015-11-16 16:38:34 -050043 "&sect;&sect;&sect;", "&para;&para;&para;",
Hao Zhue0a36a82015-11-23 15:35:20 -050044 "****", "&dagger;&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;&Dagger;",
Hao Zhu4adea852015-11-16 16:38:34 -050045 "&sect;&sect;&sect;&sect;", "&para;&para;&para;&para;"
Hao Zhudb04e302015-11-15 16:57:38 -050046 ),
47 symbol.markdown = c(
Hao Zhu8977a8a2015-11-19 16:52:21 -050048 "\\*", "\u2020", "\u2021", "\u00A7", "\u00B6",
49 "\\*\\*", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
50 "\\*\\*\\*", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
51 "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
52 "\\*\\*\\*\\*", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
53 "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
Hao Zhudb04e302015-11-15 16:57:38 -050054 ),
55 symbol.pandoc = c(
Hao Zhu8977a8a2015-11-19 16:52:21 -050056 "\\*", "\u2020", "\u2021", "\u00A7", "\u00B6",
57 "\\*\\*", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
58 "\\*\\*\\*", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
59 "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
60 "\\*\\*\\*\\*", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
61 "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
Hao Zhub1bc0aa2015-11-12 11:23:42 -050062 )
63 )
64 ids <- ids.ops[,notation]
Hao Zhu4adea852015-11-16 16:38:34 -050065 # pandoc cannot recognize ^*^ as * is a special character. We have to use ^\*^
Hao Zhudb04e302015-11-15 16:57:38 -050066 ids.intable <- gsub("\\*", "\\\\*", ids)
Hao Zhudc4b7142015-11-19 10:37:53 -050067 ids.simple <- c(
Hao Zhu8977a8a2015-11-19 16:52:21 -050068 "*", "\u2020", "\u2021", "\u00A7", "\u00B6",
69 "**", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
70 "***", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
71 "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
72 "****", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
73 "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
Hao Zhudc4b7142015-11-19 10:37:53 -050074 )
Hao Zhudb04e302015-11-15 16:57:38 -050075
76 #count the number of items in label and intable notation
77 count.label = length(label)
78 count.intablenoot = sum(str_count(input, "\\[note\\]"))
79 if (count.intablenoot != 0 & count.label != count.intablenoot){
80 warning(paste("You entered", count.label, "labels but you put",
81 count.intablenoot, "[note] in your table."))
82 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050083
Hao Zhu4adea852015-11-16 16:38:34 -050084 export <- input
85
Hao Zhudc4b7142015-11-19 10:37:53 -050086 # Find out if there are any extra in-table notations needed to be corrected
Hao Zhu2203f662015-11-20 11:53:39 -050087 extra.notation <- unique(as.numeric(
Hao Zhudc4b7142015-11-19 10:37:53 -050088 str_extract(
89 str_extract_all(
90 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
91 )[[1]],
Hao Zhu2203f662015-11-20 11:53:39 -050092 "[0-9]{1,2}")))
Hao Zhudc4b7142015-11-19 10:37:53 -050093
94
95
Hao Zhu4adea852015-11-16 16:38:34 -050096 # Footnote solution for markdown and pandoc. It is not perfect as
97 # markdown doesn't support complex table formats but this solution
98 # should be able to satisfy people who don't want to spend extra
99 # time to define their `kable` output.
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500100 if(!attr(input, "format") %in% c("html", "latex")){
Hao Zhu4adea852015-11-16 16:38:34 -0500101 # In table notation
Hao Zhudb04e302015-11-15 16:57:38 -0500102 if(count.intablenoot != 0){
103 for(i in 1:count.intablenoot){
104 export[which(str_detect(export, "\\[note\\]"))[1]] <-
105 sub("\\[note\\]", paste0("^", ids.intable[i], "^",
Hao Zhudc4b7142015-11-19 10:37:53 -0500106 paste0(rep(" ", 4 - ceiling(i/5)),
Hao Zhudb04e302015-11-15 16:57:38 -0500107 collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]])
108 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500109 }
Hao Zhu4adea852015-11-16 16:38:34 -0500110 # Fix extra in table notation
Hao Zhu4adea852015-11-16 16:38:34 -0500111 for(i in extra.notation){
112 export <- gsub(paste0("\\[note", i, "\\]"),
113 paste0("^", ids.intable[i], "^",
Hao Zhudc4b7142015-11-19 10:37:53 -0500114 paste0(rep(" ", 4 - ceiling(i/5)),
Hao Zhu4adea852015-11-16 16:38:34 -0500115 collapse = "")),
116 export)
117 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500118
Hao Zhudb04e302015-11-15 16:57:38 -0500119 export[length(export)+1] <- ""
120 export[length(export)+1] <- "__Note:__"
121 export[length(export)+1] <- paste0(
122 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
123 )
124 }
125
126 # Generate latex table footnote --------------------------------
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500127 if(attr(input, "format")=="latex"){
Hao Zhu2203f662015-11-20 11:53:39 -0500128 # Clean the entry for labels
129 label <- knitr:::escape_latex(label)
130 label <- gsub("\\\\", "\\\\\\\\", label)
Hao Zhu8977a8a2015-11-19 16:52:21 -0500131
Hao Zhu4adea852015-11-16 16:38:34 -0500132 kable_info <- magic_mirror(input)
Hao Zhudc4b7142015-11-19 10:37:53 -0500133 if(kable_info$tabular == "longtable"){
134 if(notation != "number"){
135 warning("Currently, if you enabled longtable in kable, you can only use",
136 " number as your footnote notations. ")
137 }
138 if(threeparttable == T){
139 warning("Currently, threeparttable does not support longtable.")
140 }
141 # If longtable is used, then use page footnote instead of threeparttable
142 # as it makes more sense to see the footnote at the bottom of page if
143 # table is longer than one page.
Hao Zhu4adea852015-11-16 16:38:34 -0500144
Hao Zhudc4b7142015-11-19 10:37:53 -0500145 # Longtable doesn't support footnote in caption directly.
146 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
147 count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]")
148 if (count.in.caption.note != 0){
149 # Since caption is the first part of table, we can just start
150 caption.footnote <- paste0("\\\\addtocounter{footnote}{-", count.in.caption.note, "}")
151 for(i in 1:count.in.caption.note){
152 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
153 caption.footnote <- paste0(
154 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", label[i], "}"
155 )
156 }
157
158 if (str_detect(export, "\\\\toprule")){
159 export <- sub("\\\\toprule",
160 paste0("\\\\toprule\n", caption.footnote), export)
161 }else{
162 export <- sub("\\\\hline",
163 paste0("\\\\hline\n", caption.footnote), export)
164 }
165 }
166 for(i in (count.in.caption.note + 1):count.intablenoot){
Hao Zhu4adea852015-11-16 16:38:34 -0500167 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500168 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
169 }
170 for(i in extra.notation){
171 export <- gsub(paste0("\\[note", i, "\\]"),
172 paste0("\\\\footnotemark[", i, "]"),
173 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500174 }
175 }else{
Hao Zhudb04e302015-11-15 16:57:38 -0500176 # Replace in-table notation with appropriate symbol
177 for(i in 1:count.intablenoot){
Hao Zhudc4b7142015-11-19 10:37:53 -0500178 export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids.intable[i], "}"), export)
Hao Zhudb04e302015-11-15 16:57:38 -0500179 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500180
Hao Zhudc4b7142015-11-19 10:37:53 -0500181 # Fix extra in table notation
182 for(i in extra.notation){
183 export <- gsub(paste0("\\[note", i, "\\]"),
184 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
185 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500186 }
Hao Zhudc4b7142015-11-19 10:37:53 -0500187 if(threeparttable == T){
188 # generate footer with appropriate symbol
189 footer <- ""
190 for(i in 1:count.label){
191 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
192 }
193
194 if(grepl("\\\\caption\\{.*?\\}", export)){
195 export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export)
196 }else{
197 export <- sub("\\\\begin\\{tabular\\}",
198 "\\\\begin{threeparttable}\n\\\\begin{tabular}", export)
199 }
200 export <- gsub(
201 "\\\\end\\{tabular\\}",
202 paste0(
203 "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
204 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
205 ),
206 export)
207 }else{
208 table.width <- max(nchar(
209 str_replace_all(
210 str_replace_all(kable_info$contents, "\\[note\\]", ""),
211 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1)
212 footer <- ""
213 for (i in 1:count.label){
214 label.wrap <- strwrap(label[i], table.width)
215 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
216 "}{l}{\\\\textsuperscript{", ids[i], "} ",
217 label.wrap[1], "}\\\\\\\\\n")
218 if(length(label.wrap) > 1){
219 for (j in 2:length(label.wrap)){
220 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
221 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
222 }
223 }
224 }
225 export <- gsub("\\\\end\\{tabular\\}",
226 paste0(footer, "\\\\end{tabular}"), export)
227 }
Hao Zhudb04e302015-11-15 16:57:38 -0500228 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500229 }
230 if(attr(input, "format")=="html"){
Hao Zhu2203f662015-11-20 11:53:39 -0500231 # Clean the entry for labels
232 label <- knitr:::escape_html(label)
Hao Zhudc4b7142015-11-19 10:37:53 -0500233
Hao Zhu2203f662015-11-20 11:53:39 -0500234 # Replace in-table notation with appropriate symbol
235 for(i in 1:count.intablenoot){
236 export <- sub("\\[note\\]", paste0("<sup>", ids.intable[i], "</sup>"), export)
237 }
238
239 # Fix extra in table notation
240 for(i in extra.notation){
241 export <- gsub(paste0("\\[note", i, "\\]"),
242 paste0("<sup>", ids.intable[i], "</sup>"),
243 export)
244 }
245
246 # Build footer
247 footer <- "<tfoot>\n"
248 for(i in 1:count.label){
249 footer <- paste0(footer, "<tr>\n<td style = 'padding: 0; border:0;' ",
250 "colspan='100%'><sup>", ids[i],"</sup> ",
251 label[i], "</td>\n</tr>\n")
252 }
253 footer <- paste0(footer, "</tfoot>\n")
254
255 # Paste footer to the table
256 export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1])
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500257 }
258 return(export)
259}