blob: be0ea04d7ef3074d66968cee849f1f8a5f3509dc [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 Zhu4adea852015-11-16 16:38:34 -050017add_footnote <- function(input, label = NULL, notation = "alphabet",
18 threeparttable = F) {
Hao Zhub1bc0aa2015-11-12 11:23:42 -050019 if (is.null(label)){return(input)}
20 # Define available id list
21 if (!notation %in% c("number", "alphabet", "symbol")){
Hao Zhu4adea852015-11-16 16:38:34 -050022 warning('Please select your notation within "number", "alphabet" and ',
23 '"symbol". Now add_footnote is using "alphabet" as default.')
Hao Zhub1bc0aa2015-11-12 11:23:42 -050024 }
25 if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))}
26 ids.ops <- data.frame(
27 number = as.character(1:20),
28 alphabet = letters[1:20],
29 symbol.latex = c(
30 "*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P",
31 "**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P",
Hao Zhu4adea852015-11-16 16:38:34 -050032 "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag",
33 "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P",
34 "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag",
35 "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P"
Hao Zhub1bc0aa2015-11-12 11:23:42 -050036 ),
37 symbol.html = c(
38 "*", "&dagger;", "&Dagger;", "&sect;", "&para;",
39 "**", "&dagger;&dagger;", "&Dagger;&Dagger;", "&sect;&sect;", "&para;&para;",
Hao Zhu4adea852015-11-16 16:38:34 -050040 "*", "&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;",
41 "&sect;&sect;&sect;", "&para;&para;&para;",
42 "**", "&dagger;&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;&Dagger;",
43 "&sect;&sect;&sect;&sect;", "&para;&para;&para;&para;"
Hao Zhudb04e302015-11-15 16:57:38 -050044 ),
45 symbol.markdown = c(
46 "\\*", "†", "‡", "§", "¶",
47 "\\*\\*", "††", "‡‡", "§§", "¶¶",
48 "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
49 "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
50 ),
51 symbol.pandoc = c(
52 "\\*", "†", "‡", "§", "¶",
53 "\\*\\*", "††", "‡‡", "§§", "¶¶",
54 "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
55 "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
Hao Zhub1bc0aa2015-11-12 11:23:42 -050056 )
57 )
58 ids <- ids.ops[,notation]
Hao Zhu4adea852015-11-16 16:38:34 -050059 # pandoc cannot recognize ^*^ as * is a special character. We have to use ^\*^
Hao Zhudb04e302015-11-15 16:57:38 -050060 ids.intable <- gsub("\\*", "\\\\*", ids)
Hao Zhudc4b7142015-11-19 10:37:53 -050061 ids.simple <- c(
62 "*", "†", "‡", "§", "¶",
63 "**", "††", "‡‡", "§§", "¶¶",
64 "***", "†††", "‡‡‡", "§§§", "¶¶¶",
65 "****", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
66 )
Hao Zhudb04e302015-11-15 16:57:38 -050067
68 #count the number of items in label and intable notation
69 count.label = length(label)
70 count.intablenoot = sum(str_count(input, "\\[note\\]"))
71 if (count.intablenoot != 0 & count.label != count.intablenoot){
72 warning(paste("You entered", count.label, "labels but you put",
73 count.intablenoot, "[note] in your table."))
74 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050075
Hao Zhu4adea852015-11-16 16:38:34 -050076 export <- input
77
Hao Zhudc4b7142015-11-19 10:37:53 -050078 # Find out if there are any extra in-table notations needed to be corrected
79 extra.notation <- as.numeric(
80 str_extract(
81 str_extract_all(
82 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
83 )[[1]],
84 "[0-9]{1,2}"))
85
86
87
Hao Zhu4adea852015-11-16 16:38:34 -050088 # Footnote solution for markdown and pandoc. It is not perfect as
89 # markdown doesn't support complex table formats but this solution
90 # should be able to satisfy people who don't want to spend extra
91 # time to define their `kable` output.
Hao Zhub1bc0aa2015-11-12 11:23:42 -050092 if(!attr(input, "format") %in% c("html", "latex")){
Hao Zhu4adea852015-11-16 16:38:34 -050093 # In table notation
Hao Zhudb04e302015-11-15 16:57:38 -050094 if(count.intablenoot != 0){
95 for(i in 1:count.intablenoot){
96 export[which(str_detect(export, "\\[note\\]"))[1]] <-
97 sub("\\[note\\]", paste0("^", ids.intable[i], "^",
Hao Zhudc4b7142015-11-19 10:37:53 -050098 paste0(rep(" ", 4 - ceiling(i/5)),
Hao Zhudb04e302015-11-15 16:57:38 -050099 collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]])
100 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500101 }
Hao Zhu4adea852015-11-16 16:38:34 -0500102 # Fix extra in table notation
Hao Zhu4adea852015-11-16 16:38:34 -0500103 for(i in extra.notation){
104 export <- gsub(paste0("\\[note", i, "\\]"),
105 paste0("^", ids.intable[i], "^",
Hao Zhudc4b7142015-11-19 10:37:53 -0500106 paste0(rep(" ", 4 - ceiling(i/5)),
Hao Zhu4adea852015-11-16 16:38:34 -0500107 collapse = "")),
108 export)
109 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500110
Hao Zhudb04e302015-11-15 16:57:38 -0500111 export[length(export)+1] <- ""
112 export[length(export)+1] <- "__Note:__"
113 export[length(export)+1] <- paste0(
114 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
115 )
116 }
117
118 # Generate latex table footnote --------------------------------
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500119 if(attr(input, "format")=="latex"){
Hao Zhu4adea852015-11-16 16:38:34 -0500120 kable_info <- magic_mirror(input)
Hao Zhudc4b7142015-11-19 10:37:53 -0500121 if(kable_info$tabular == "longtable"){
122 if(notation != "number"){
123 warning("Currently, if you enabled longtable in kable, you can only use",
124 " number as your footnote notations. ")
125 }
126 if(threeparttable == T){
127 warning("Currently, threeparttable does not support longtable.")
128 }
129 # If longtable is used, then use page footnote instead of threeparttable
130 # as it makes more sense to see the footnote at the bottom of page if
131 # table is longer than one page.
Hao Zhu4adea852015-11-16 16:38:34 -0500132
Hao Zhudc4b7142015-11-19 10:37:53 -0500133 # Longtable doesn't support footnote in caption directly.
134 # See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
135 count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]")
136 if (count.in.caption.note != 0){
137 # Since caption is the first part of table, we can just start
138 caption.footnote <- paste0("\\\\addtocounter{footnote}{-", count.in.caption.note, "}")
139 for(i in 1:count.in.caption.note){
140 export <- sub("\\[note\\]", "\\\\protect\\\\footnotemark ", export)
141 caption.footnote <- paste0(
142 caption.footnote, "\n\\\\stepcounter{footnote}\\\\footnotetext{", label[i], "}"
143 )
144 }
145
146 if (str_detect(export, "\\\\toprule")){
147 export <- sub("\\\\toprule",
148 paste0("\\\\toprule\n", caption.footnote), export)
149 }else{
150 export <- sub("\\\\hline",
151 paste0("\\\\hline\n", caption.footnote), export)
152 }
153 }
154 for(i in (count.in.caption.note + 1):count.intablenoot){
Hao Zhu4adea852015-11-16 16:38:34 -0500155 export <- sub("\\[note\\]",
Hao Zhudc4b7142015-11-19 10:37:53 -0500156 paste0("\\\\footnote[", i, "]{", label[i], "}"), export)
157 }
158 for(i in extra.notation){
159 export <- gsub(paste0("\\[note", i, "\\]"),
160 paste0("\\\\footnotemark[", i, "]"),
161 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500162 }
163 }else{
Hao Zhudb04e302015-11-15 16:57:38 -0500164 # Replace in-table notation with appropriate symbol
165 for(i in 1:count.intablenoot){
Hao Zhudc4b7142015-11-19 10:37:53 -0500166 export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids.intable[i], "}"), export)
Hao Zhudb04e302015-11-15 16:57:38 -0500167 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500168
Hao Zhudc4b7142015-11-19 10:37:53 -0500169 # Fix extra in table notation
170 for(i in extra.notation){
171 export <- gsub(paste0("\\[note", i, "\\]"),
172 paste0("\\\\textsuperscript{", ids.intable[i], "}"),
173 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500174 }
Hao Zhudc4b7142015-11-19 10:37:53 -0500175 if(threeparttable == T){
176 # generate footer with appropriate symbol
177 footer <- ""
178 for(i in 1:count.label){
179 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
180 }
181
182 if(grepl("\\\\caption\\{.*?\\}", export)){
183 export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export)
184 }else{
185 export <- sub("\\\\begin\\{tabular\\}",
186 "\\\\begin{threeparttable}\n\\\\begin{tabular}", export)
187 }
188 export <- gsub(
189 "\\\\end\\{tabular\\}",
190 paste0(
191 "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
192 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
193 ),
194 export)
195 }else{
196 table.width <- max(nchar(
197 str_replace_all(
198 str_replace_all(kable_info$contents, "\\[note\\]", ""),
199 "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1)
200 footer <- ""
201 for (i in 1:count.label){
202 label.wrap <- strwrap(label[i], table.width)
203 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
204 "}{l}{\\\\textsuperscript{", ids[i], "} ",
205 label.wrap[1], "}\\\\\\\\\n")
206 if(length(label.wrap) > 1){
207 for (j in 2:length(label.wrap)){
208 footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
209 "}{l}{", label.wrap[j], "}\\\\\\\\\n")
210 }
211 }
212 }
213 export <- gsub("\\\\end\\{tabular\\}",
214 paste0(footer, "\\\\end{tabular}"), export)
215 }
Hao Zhudb04e302015-11-15 16:57:38 -0500216 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500217 }
218 if(attr(input, "format")=="html"){
Hao Zhudc4b7142015-11-19 10:37:53 -0500219
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500220 }
221 return(export)
222}
Hao Zhudb04e302015-11-15 16:57:38 -0500223
224
225