blob: 59478c3702459f98ce62b06aa092e78ff9e8b995 [file] [log] [blame]
Hao Zhucdd7f922018-01-08 11:39:40 -05001#' Add footnote (new)
Hao Zhu8dd65a92018-01-05 20:40:27 -05002#'
Hao Zhue0782ab2018-01-09 13:24:13 -05003#' @description `footnote` provides a more flexible way to add footnote. You
4#' can add mutiple sets of footnote using differeny notation system. It is
5#' also possible to specify footnote section header one by one and print
6#' footnotes as a chunk of texts.
7#'
8#' @param kable_input HTML or LaTeX table generated by `knitr::kable`
9#' @param general Text for general footnote comments. Footnotes in this section
10#' won't be labeled with any notations
11#' @param number A vector of footnote texts. Footnotes here will be numbered.
12#' There is no upper cap for the number of footnotes here
13#' @param alphabet A vector of footnote texts, Footnotes here will be labeled
14#' with abc. The vector here should not have more than 26 elements.
15#' @param symbol A vector of footnote texts, Footnotes here will be labeled
16#' with special symbols. The vector here should not have more than 20 elements.
17#' @param footnote_order The order of how to arrange `general`, `number`,
18#' `alphabet` and `symbol`.
19#' @param footnote_as_chunk T/F value. Default is FALSE. It controls whether
20#' the footnotes should be printed in a chunk (without line break).
21#' @param escape T/F value. It controls whether the contents and titles should
22#' be escaped against HTML or LaTeX. Default is TRUE.
Hao Zhu17814c72018-01-10 11:32:14 -050023#' @param threeparttable T/F value for whether to use LaTeX package
24#' threeparttable. Threeparttable will force the width of caption and
25#' footnotes be the width of the original table. It's useful when you have
26#' long paragraph of footnotes.
Augusto Magalhãesf17fa462019-03-05 12:55:21 +090027#' @param fixed_small_size T/F When you want to keep the footnote small after
28#' specifying large font size with the kable_styling() (e.g. ideal font for headers
Hao Zhu72917f92019-03-15 18:41:42 -040029#' and table content with small font in footnotes).
Hao Zhue0782ab2018-01-09 13:24:13 -050030#' @param general_title Section header for general footnotes. Default is
31#' "Note: ".
32#' @param number_title Section header for number footnotes. Default is "".
33#' @param alphabet_title Section header for alphabet footnotes. Default is "".
34#' @param symbol_title Section header for symbol footnotes. Default is "".
Hao Zhu25efb132018-05-20 22:00:56 -040035#' @param title_format Choose from "italic"(default), "bold" and "underline".
36#' Multiple options are possible.
Hao Zhu149faed2018-10-23 16:36:22 -040037#' @param symbol_manual User can manually supply a vector of either html or
38#' latex symbols. For example, `symbol_manual = c('*', '\\\\dag', '\\\\ddag')`.`
Hao Zhu465fc652018-05-20 20:02:36 -040039#'
Hao Zhub1e2e3d2018-01-09 13:31:42 -050040#' @examples dt <- mtcars[1:5, 1:5]
Hao Zhu593f57e2018-01-09 13:30:01 -050041#' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b"))
42#'
Hao Zhu8dd65a92018-01-05 20:40:27 -050043#' @export
Hao Zhucdd7f922018-01-08 11:39:40 -050044footnote <- function(kable_input,
Hao Zhu1ac13ad2018-01-08 16:12:24 -050045 general = NULL,
46 number = NULL,
47 alphabet = NULL,
48 symbol = NULL,
49 footnote_order = c("general", "number",
50 "alphabet", "symbol"),
51 footnote_as_chunk = FALSE,
Hao Zhue0782ab2018-01-09 13:24:13 -050052 escape = TRUE,
Hao Zhu17814c72018-01-10 11:32:14 -050053 threeparttable = FALSE,
Augusto Magalhãesf17fa462019-03-05 12:55:21 +090054 fixed_small_size = FALSE,
Hao Zhu1ac13ad2018-01-08 16:12:24 -050055 general_title = "Note: ",
56 number_title = "",
57 alphabet_title = "",
Hao Zhu25efb132018-05-20 22:00:56 -040058 symbol_title = "",
Hao Zhu149faed2018-10-23 16:36:22 -040059 title_format = "italic",
60 symbol_manual = NULL
Hao Zhu8dd65a92018-01-05 20:40:27 -050061) {
62 kable_format <- attr(kable_input, "format")
63 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050064 warning("Please specify format in kable. kableExtra can customize either ",
65 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
66 "for details.")
Hao Zhu8dd65a92018-01-05 20:40:27 -050067 return(kable_input)
68 }
Hao Zhu8dd65a92018-01-05 20:40:27 -050069 if (length(alphabet) > 26) {
70 alphabet <- alphabet[1:26]
71 warning("Please don't use more than 26 footnotes in table_footnote ",
72 "alphabet. Use number instead.")
73 }
74 if (length(symbol) > 20) {
75 symbol <- symbol[1:20]
76 warning("Please don't use more than 20 footnotes in table_footnote ",
77 "symbol. Use number instead.")
78 }
Hao Zhue0782ab2018-01-09 13:24:13 -050079 footnote_titles <- list(
80 general = general_title, number = number_title,
81 alphabet = alphabet_title, symbol = symbol_title
82 )
83 footnote_contents <- list(
84 general = general, number = number, alphabet = alphabet, symbol = symbol
85 )
86 notnull <- names(footnote_contents)[!sapply(footnote_contents, is.null)]
87 if (length(notnull) == 0) {return(kable_input)}
Hao Zhu8dd65a92018-01-05 20:40:27 -050088 footnote_order <- footnote_order[footnote_order %in% notnull]
89 footnote_titles <- footnote_titles[footnote_order]
90 footnote_contents <- footnote_contents[footnote_order]
Hao Zhue0782ab2018-01-09 13:24:13 -050091 if (escape) {
92 if (kable_format == "html") {
93 footnote_contents <- lapply(footnote_contents, escape_html)
94 footnote_titles <- lapply(footnote_titles, escape_html)
95 } else {
Hao Zhud4630872018-03-26 11:26:36 -040096 footnote_contents <- lapply(footnote_contents, escape_latex2)
Hao Zhu1aff7342018-04-02 18:33:15 -040097 footnote_contents <- lapply(footnote_contents, linebreak)
Hao Zhud4630872018-03-26 11:26:36 -040098 footnote_titles <- lapply(footnote_titles, escape_latex2)
Hao Zhu1aff7342018-04-02 18:33:15 -040099 footnote_titles <- lapply(footnote_titles, linebreak)
Hao Zhue0782ab2018-01-09 13:24:13 -0500100 }
101 }
Hao Zhu25efb132018-05-20 22:00:56 -0400102 title_format <- match.arg(title_format, c("italic", "bold", "underline"),
103 several.ok = TRUE)
104 footnote_titles <- lapply(footnote_titles, footnote_title_format,
105 kable_format, title_format)
Hao Zhu8dd65a92018-01-05 20:40:27 -0500106 footnote_table <- footnote_table_maker(
Hao Zhu149faed2018-10-23 16:36:22 -0400107 kable_format, footnote_titles, footnote_contents, symbol_manual
Hao Zhu8dd65a92018-01-05 20:40:27 -0500108 )
109 if (kable_format == "html") {
Hao Zhucdd7f922018-01-08 11:39:40 -0500110 return(footnote_html(kable_input, footnote_table, footnote_as_chunk))
Hao Zhu8dd65a92018-01-05 20:40:27 -0500111 }
Hao Zhu19c4fa52018-01-09 12:01:14 -0500112 if (kable_format == "latex") {
Hao Zhu17814c72018-01-10 11:32:14 -0500113 return(footnote_latex(kable_input, footnote_table, footnote_as_chunk,
Hao Zhu72917f92019-03-15 18:41:42 -0400114 threeparttable, fixed_small_size))
Hao Zhu19c4fa52018-01-09 12:01:14 -0500115 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500116}
117
Hao Zhu25efb132018-05-20 22:00:56 -0400118footnote_title_format <- function(x, format, title_format) {
119 if (x == "") return(x)
120 if (format == "html") {
121 title_style <- ""
122 if ("italic" %in% title_format) {
123 title_style <- paste0(title_style, "font-style: italic;")
124 }
125 if ("bold" %in% title_format) {
126 title_style <- paste0(title_style, "font-weight: bold;")
127 }
128 if ("underline" %in% title_format) {
129 title_style <- paste0(title_style, "text-decoration: underline;")
130 }
131 return(paste0(
132 '<span style="', title_style, '">', x, '</span>'
133 ))
134 } else {
135 if ("italic" %in% title_format) {
136 x <- paste0("\\\\textit\\{", x, "\\}")
137 }
138 if ("bold" %in% title_format) {
139 x <- paste0("\\\\textbf\\{", x, "\\}")
140 }
141 if ("underline" %in% title_format) {
142 x <- paste0("\\\\underline\\{", x, "\\}")
143 }
144 return(x)
145 }
146}
147
Hao Zhu149faed2018-10-23 16:36:22 -0400148footnote_table_maker <- function(format, footnote_titles, footnote_contents,
149 symbol_manual) {
150 if (is.null(symbol_manual)) {
151 number_index <- read.csv(system.file("symbol_index.csv",
152 package = "kableExtra"))
153 if (format == "latex") {
154 symbol_index <- number_index$symbol.latex
155 } else {
156 symbol_index <- number_index$symbol.html
157 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500158 } else {
Hao Zhu149faed2018-10-23 16:36:22 -0400159 symbol_index <- symbol_manual
Hao Zhu8dd65a92018-01-05 20:40:27 -0500160 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500161
Hao Zhu149faed2018-10-23 16:36:22 -0400162
Hao Zhu8dd65a92018-01-05 20:40:27 -0500163 if (!is.null(footnote_contents$general)) {
164 footnote_contents$general <- data.frame(
165 index = "",
Hao Zhubab692d2018-01-09 17:49:55 -0500166 footnote = footnote_contents$general
Hao Zhu8dd65a92018-01-05 20:40:27 -0500167 )
168 }
169 if (!is.null(footnote_contents$number)) {
170 footnote_contents$number <- data.frame(
171 index = as.character(1:length(footnote_contents$number)),
172 footnote = footnote_contents$number
173 )
174 }
175 if (!is.null(footnote_contents$alphabet)) {
176 footnote_contents$alphabet <- data.frame(
177 index = letters[1:length(footnote_contents$alphabet)],
178 footnote = footnote_contents$alphabet
179 )
180 }
181 if (!is.null(footnote_contents$symbol)) {
182 footnote_contents$symbol <- data.frame(
183 index = symbol_index[1:length(footnote_contents$symbol)],
184 footnote = footnote_contents$symbol
185 )
186 }
187
188 out <- list()
189 out$contents <- footnote_contents
190 out$titles <- footnote_titles
191 return(out)
192}
193
194# HTML
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500195footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500196 kable_attrs <- attributes(kable_input)
197 kable_xml <- read_kable_as_xml(kable_input)
198
Hao Zhucdd7f922018-01-08 11:39:40 -0500199 new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk)
Hao Zhu8dd65a92018-01-05 20:40:27 -0500200 xml_add_child(kable_xml, new_html_footnote)
201
202 out <- as_kable_xml(kable_xml)
203 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500204 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu8dd65a92018-01-05 20:40:27 -0500205 return(out)
206}
207
Hao Zhucdd7f922018-01-08 11:39:40 -0500208html_tfoot_maker <- function(footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500209 footnote_types <- names(footnote_table$contents)
210 footnote_text <- c()
211 for (i in footnote_types) {
Hao Zhucdd7f922018-01-08 11:39:40 -0500212 footnote_text <- c(footnote_text, html_tfoot_maker_(
Hao Zhu8dd65a92018-01-05 20:40:27 -0500213 footnote_table$contents[[i]], footnote_table$titles[[i]], i,
214 footnote_as_chunk))
215 }
216 footnote_text <- paste0(
217 "<tfoot>", paste0(footnote_text, collapse = ""), "</tfoot>"
218 )
219 footnote_node <- read_html(footnote_text, options = c("RECOVER", "NOERROR"))
220 return(xml_child(xml_child(footnote_node, 1), 1))
221}
222
Hao Zhucdd7f922018-01-08 11:39:40 -0500223html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500224 footnote_text <- apply(ft_contents, 1, function(x) {
225 paste0('<sup>', x[1], '</sup> ', x[2])
226 })
227 if (ft_title != "") {
Hao Zhu25efb132018-05-20 22:00:56 -0400228 title_text <- ft_title
Hao Zhu8dd65a92018-01-05 20:40:27 -0500229 footnote_text <- c(title_text, footnote_text)
230 }
231 if (!ft_chunk) {
232 footnote_text <- paste0(
233 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
234 footnote_text, '</td></tr>'
235 )
236 } else {
237 footnote_text <- paste0(
238 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
Hao Zhucdd7f922018-01-08 11:39:40 -0500239 paste0(footnote_text, collapse = " "),
Hao Zhu8dd65a92018-01-05 20:40:27 -0500240 '</td></tr>'
241 )
242 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500243 return(footnote_text)
244}
Hao Zhucdd7f922018-01-08 11:39:40 -0500245
246# LaTeX
Hao Zhu17814c72018-01-10 11:32:14 -0500247footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk,
Hao Zhu72917f92019-03-15 18:41:42 -0400248 threeparttable, fixed_small_size) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500249 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -0400250 out <- solve_enc(kable_input)
Hao Zhu17814c72018-01-10 11:32:14 -0500251
Hao Zhu19c4fa52018-01-09 12:01:14 -0500252 footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk,
Hao Zhu17814c72018-01-10 11:32:14 -0500253 table_info$ncol, threeparttable)
254 if (threeparttable) {
Hao Zhu23bde3a2018-03-28 16:00:55 -0400255 if (table_info$tabular %in% c("longtable", "longtabu") ) {
Hao Zhu17814c72018-01-10 11:32:14 -0500256 out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"),
Hao Zhu27c7c852018-03-26 16:18:23 -0400257 paste0("\\\\begin{ThreePartTable}\n\\\\begin{TableNotes}",
258 ifelse(footnote_as_chunk, "[para]", ""),
Augusto Magalhãesf17fa462019-03-05 12:55:21 +0900259 ifelse(fixed_small_size,"\n\\\\small\n","\n"), footnote_text,
Hao Zhu27c7c852018-03-26 16:18:23 -0400260 "\n\\\\end{TableNotes}\n\\\\begin{",
Hao Zhu17814c72018-01-10 11:32:14 -0500261 table_info$tabular, "}"),
262 out)
Hao Zhu23bde3a2018-03-28 16:00:55 -0400263 out <- sub(paste0("\\\\end\\{",table_info$tabular, "\\}"),
264 paste0("\\\\end{", table_info$tabular,
265 "}\n\\\\end{ThreePartTable}"),
266 out)
267 if (table_info$booktabs) {
268 out <- sub("\\\\bottomrule", "\\\\bottomrule\n\\\\insertTableNotes", out)
269 } else {
270 out <- sub("\\\\hline\n\\\\end\\{longtable\\}",
271 "\\\\hline\n\\\\insertTableNotes\n\\\\end\\{longtable\\}",
272 out)
273 }
Hao Zhu27c7c852018-03-26 16:18:23 -0400274 } else {
Hao Zhu23bde3a2018-03-28 16:00:55 -0400275 if (table_info$tabular == "tabu") {
276 stop("Please use `longtable = T` in your kable function. ",
277 "Full width threeparttable only works with longtable.")
278 }
279 out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"),
280 paste0("\\\\begin{threeparttable}\n\\\\begin{",
281 table_info$tabular, "}"),
282 out)
283 out <- sub(table_info$end_tabular,
284 paste0("\\\\end{", table_info$tabular,
285 "}\n\\\\begin{tablenotes}",
286 ifelse(footnote_as_chunk, "[para]", ""),
Augusto Magalhãesf17fa462019-03-05 12:55:21 +0900287 ifelse(fixed_small_size,"\n\\\\small\n","\n"), footnote_text,
Hao Zhu23bde3a2018-03-28 16:00:55 -0400288 "\n\\\\end{tablenotes}\n\\\\end{threeparttable}"),
Hao Zhu27c7c852018-03-26 16:18:23 -0400289 out)
290 }
Hao Zhu17814c72018-01-10 11:32:14 -0500291 } else {
Hao Zhu23bde3a2018-03-28 16:00:55 -0400292 if (table_info$booktabs) {
293 out <- sub("\\\\bottomrule",
294 paste0("\\\\bottomrule\n", footnote_text), out)
295 } else {
296 out <- sub(table_info$end_tabular,
297 paste0(footnote_text, "\n\\\\end{", table_info$tabular, "}"),
298 out)
299 }
Hao Zhu17814c72018-01-10 11:32:14 -0500300 }
301
Hao Zhu19c4fa52018-01-09 12:01:14 -0500302 out <- structure(out, format = "latex", class = "knitr_kable")
303 attr(out, "kable_meta") <- table_info
304 return(out)
Hao Zhu19c4fa52018-01-09 12:01:14 -0500305}
Hao Zhucdd7f922018-01-08 11:39:40 -0500306
Hao Zhu17814c72018-01-10 11:32:14 -0500307latex_tfoot_maker <- function(footnote_table, footnote_as_chunk, ncol,
308 threeparttable) {
Hao Zhu19c4fa52018-01-09 12:01:14 -0500309 footnote_types <- names(footnote_table$contents)
310 footnote_text <- c()
Hao Zhu17814c72018-01-10 11:32:14 -0500311 if (threeparttable) {
312 for (i in footnote_types) {
313 footnote_text <- c(footnote_text, latex_tfoot_maker_tpt_(
314 footnote_table$contents[[i]], footnote_table$titles[[i]],
315 footnote_as_chunk, ncol))
316 }
317 } else {
318 for (i in footnote_types) {
319 footnote_text <- c(footnote_text, latex_tfoot_maker_(
320 footnote_table$contents[[i]], footnote_table$titles[[i]],
321 footnote_as_chunk, ncol))
322 }
Hao Zhu19c4fa52018-01-09 12:01:14 -0500323 }
324 footnote_text <- paste0(footnote_text, collapse = "\n")
325 return(footnote_text)
326}
Hao Zhu9f917482018-01-08 18:09:33 -0500327
Hao Zhu17814c72018-01-10 11:32:14 -0500328latex_tfoot_maker_ <- function(ft_contents, ft_title, ft_chunk, ncol) {
Hao Zhu19c4fa52018-01-09 12:01:14 -0500329 footnote_text <- apply(ft_contents, 1, function(x) {
330 if (x[1] == "") {
331 x[2]
332 } else {
333 paste0('\\\\textsuperscript{', x[1], '} ', x[2])
334 }
335 })
336 if (ft_title != "") {
Hao Zhu25efb132018-05-20 22:00:56 -0400337 title_text <- ft_title
Hao Zhu19c4fa52018-01-09 12:01:14 -0500338 footnote_text <- c(title_text, footnote_text)
339 }
340 if (!ft_chunk) {
341 footnote_text <- paste0(
Cillian Berragande662b72020-03-05 16:17:28 +0000342 '\\\\multicolumn{', ncol, '}{l}{\\\\rule{0pt}{1em}', footnote_text, '}\\\\\\\\'
Hao Zhu19c4fa52018-01-09 12:01:14 -0500343 )
344 } else {
345 footnote_text <- paste0(
Cillian Berragande662b72020-03-05 16:17:28 +0000346 '\\\\multicolumn{', ncol, '}{l}{\\\\rule{0pt}{1em}',
Hao Zhu19c4fa52018-01-09 12:01:14 -0500347 paste0(footnote_text, collapse = " "),
348 '}\\\\\\\\'
349 )
350 }
351 return(footnote_text)
Hao Zhucdd7f922018-01-08 11:39:40 -0500352}
Hao Zhu17814c72018-01-10 11:32:14 -0500353
354latex_tfoot_maker_tpt_ <- function(ft_contents, ft_title, ft_chunk, ncol) {
355 footnote_text <- apply(ft_contents, 1, function(x) {
356 if (x[1] == "") {
357 paste0('\\\\item ', x[2])
358 } else {
359 paste0('\\\\item[', x[1], '] ', x[2])
360 }
361 })
362 if (ft_title != "") {
Hao Zhu25efb132018-05-20 22:00:56 -0400363 title_text <- paste0('\\\\item ', ft_title, ' ')
Hao Zhu17814c72018-01-10 11:32:14 -0500364 footnote_text <- c(title_text, footnote_text)
365 }
366 footnote_text <- paste0(footnote_text, collapse = "\n")
367 # if (!ft_chunk) {
368 # footnote_text <- paste0(footnote_text, collapse = "\n")
369 # } else {
370 # footnote_text <- paste0(footnote_text, collapse = " ")
371 # }
372 return(footnote_text)
373}