blob: e995af1f537500d0cada5529046e09f1855fa0c7 [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 Zhu8977a8a2015-11-19 16:52:21 -050017add_footnote <- function(input, label = NULL, notation = "alphabet", escape = T,
Hao Zhu4adea852015-11-16 16:38:34 -050018 threeparttable = F) {
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
86 extra.notation <- as.numeric(
87 str_extract(
88 str_extract_all(
89 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
90 )[[1]],
91 "[0-9]{1,2}"))
92
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 Zhu8977a8a2015-11-19 16:52:21 -0500127 # Clean the entry for labels when escape is enabled
128 if (escape = T){label <- knitr:::escape_latex(label)}
129
Hao Zhu4adea852015-11-16 16:38:34 -0500130 kable_info <- magic_mirror(input)
Hao Zhudc4b7142015-11-19 10:37:53 -0500131 if(kable_info$tabular == "longtable"){
132 if(notation != "number"){
133 warning("Currently, if you enabled longtable in kable, you can only use",
134 " number as your footnote notations. ")
135 }
136 if(threeparttable == T){
137 warning("Currently, threeparttable does not support longtable.")
138 }
139 # If longtable is used, then use page footnote instead of threeparttable
140 # as it makes more sense to see the footnote at the bottom of page if
141 # table is longer than one page.
Hao Zhu4adea852015-11-16 16:38:34 -0500142
Hao Zhudc4b7142015-11-19 10:37:53 -0500143 # Longtable doesn't support footnote in caption directly.
144 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
145 count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]")
146 if (count.in.caption.note != 0){
147 # Since caption is the first part of table, we can just start
148 caption.footnote <- paste0("\\\\addtocounter{footnote}{-", count.in.caption.note, "}")
149 for(i in 1:count.in.caption.note){
150 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
151 caption.footnote <- paste0(
152 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", label[i], "}"
153 )
154 }
155
156 if (str_detect(export, "\\\\toprule")){
157 export <- sub("\\\\toprule",
158 paste0("\\\\toprule\n", caption.footnote), export)
159 }else{
160 export <- sub("\\\\hline",
161 paste0("\\\\hline\n", caption.footnote), export)
162 }
163 }
164 for(i in (count.in.caption.note + 1):count.intablenoot){
Hao Zhu4adea852015-11-16 16:38:34 -0500165 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500166 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
167 }
168 for(i in extra.notation){
169 export <- gsub(paste0("\\[note", i, "\\]"),
170 paste0("\\\\footnotemark[", i, "]"),
171 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500172 }
173 }else{
Hao Zhudb04e302015-11-15 16:57:38 -0500174 # Replace in-table notation with appropriate symbol
175 for(i in 1:count.intablenoot){
Hao Zhudc4b7142015-11-19 10:37:53 -0500176 export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids.intable[i], "}"), export)
Hao Zhudb04e302015-11-15 16:57:38 -0500177 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500178
Hao Zhudc4b7142015-11-19 10:37:53 -0500179 # Fix extra in table notation
180 for(i in extra.notation){
181 export <- gsub(paste0("\\[note", i, "\\]"),
182 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
183 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500184 }
Hao Zhudc4b7142015-11-19 10:37:53 -0500185 if(threeparttable == T){
186 # generate footer with appropriate symbol
187 footer <- ""
188 for(i in 1:count.label){
189 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
190 }
191
192 if(grepl("\\\\caption\\{.*?\\}", export)){
193 export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export)
194 }else{
195 export <- sub("\\\\begin\\{tabular\\}",
196 "\\\\begin{threeparttable}\n\\\\begin{tabular}", export)
197 }
198 export <- gsub(
199 "\\\\end\\{tabular\\}",
200 paste0(
201 "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
202 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
203 ),
204 export)
205 }else{
206 table.width <- max(nchar(
207 str_replace_all(
208 str_replace_all(kable_info$contents, "\\[note\\]", ""),
209 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1)
210 footer <- ""
211 for (i in 1:count.label){
212 label.wrap <- strwrap(label[i], table.width)
213 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
214 "}{l}{\\\\textsuperscript{", ids[i], "} ",
215 label.wrap[1], "}\\\\\\\\\n")
216 if(length(label.wrap) > 1){
217 for (j in 2:length(label.wrap)){
218 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
219 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
220 }
221 }
222 }
223 export <- gsub("\\\\end\\{tabular\\}",
224 paste0(footer, "\\\\end{tabular}"), export)
225 }
Hao Zhudb04e302015-11-15 16:57:38 -0500226 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500227 }
228 if(attr(input, "format")=="html"){
Hao Zhudc4b7142015-11-19 10:37:53 -0500229
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500230 }
231 return(export)
232}