blob: 41e69256b35a423bdb5fd1da1fb58fb524e3cd51 [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
29#' and table content with small font in footnotes).
30#' keeping the footnote small.
Hao Zhue0782ab2018-01-09 13:24:13 -050031#' @param general_title Section header for general footnotes. Default is
32#' "Note: ".
33#' @param number_title Section header for number footnotes. Default is "".
34#' @param alphabet_title Section header for alphabet footnotes. Default is "".
35#' @param symbol_title Section header for symbol footnotes. Default is "".
Hao Zhu25efb132018-05-20 22:00:56 -040036#' @param title_format Choose from "italic"(default), "bold" and "underline".
37#' Multiple options are possible.
Hao Zhu149faed2018-10-23 16:36:22 -040038#' @param symbol_manual User can manually supply a vector of either html or
39#' latex symbols. For example, `symbol_manual = c('*', '\\\\dag', '\\\\ddag')`.`
Hao Zhu465fc652018-05-20 20:02:36 -040040#'
Hao Zhub1e2e3d2018-01-09 13:31:42 -050041#' @examples dt <- mtcars[1:5, 1:5]
Hao Zhu593f57e2018-01-09 13:30:01 -050042#' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b"))
43#'
Hao Zhu8dd65a92018-01-05 20:40:27 -050044#' @export
Hao Zhucdd7f922018-01-08 11:39:40 -050045footnote <- function(kable_input,
Hao Zhu1ac13ad2018-01-08 16:12:24 -050046 general = NULL,
47 number = NULL,
48 alphabet = NULL,
49 symbol = NULL,
50 footnote_order = c("general", "number",
51 "alphabet", "symbol"),
52 footnote_as_chunk = FALSE,
Hao Zhue0782ab2018-01-09 13:24:13 -050053 escape = TRUE,
Hao Zhu17814c72018-01-10 11:32:14 -050054 threeparttable = FALSE,
Augusto Magalhãesf17fa462019-03-05 12:55:21 +090055 fixed_small_size = FALSE,
Hao Zhu1ac13ad2018-01-08 16:12:24 -050056 general_title = "Note: ",
57 number_title = "",
58 alphabet_title = "",
Hao Zhu25efb132018-05-20 22:00:56 -040059 symbol_title = "",
Hao Zhu149faed2018-10-23 16:36:22 -040060 title_format = "italic",
61 symbol_manual = NULL
Hao Zhu8dd65a92018-01-05 20:40:27 -050062) {
63 kable_format <- attr(kable_input, "format")
64 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050065 warning("Please specify format in kable. kableExtra can customize either ",
66 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
67 "for details.")
Hao Zhu8dd65a92018-01-05 20:40:27 -050068 return(kable_input)
69 }
Hao Zhu8dd65a92018-01-05 20:40:27 -050070 if (length(alphabet) > 26) {
71 alphabet <- alphabet[1:26]
72 warning("Please don't use more than 26 footnotes in table_footnote ",
73 "alphabet. Use number instead.")
74 }
75 if (length(symbol) > 20) {
76 symbol <- symbol[1:20]
77 warning("Please don't use more than 20 footnotes in table_footnote ",
78 "symbol. Use number instead.")
79 }
Hao Zhue0782ab2018-01-09 13:24:13 -050080 footnote_titles <- list(
81 general = general_title, number = number_title,
82 alphabet = alphabet_title, symbol = symbol_title
83 )
84 footnote_contents <- list(
85 general = general, number = number, alphabet = alphabet, symbol = symbol
86 )
87 notnull <- names(footnote_contents)[!sapply(footnote_contents, is.null)]
88 if (length(notnull) == 0) {return(kable_input)}
Hao Zhu8dd65a92018-01-05 20:40:27 -050089 footnote_order <- footnote_order[footnote_order %in% notnull]
90 footnote_titles <- footnote_titles[footnote_order]
91 footnote_contents <- footnote_contents[footnote_order]
Hao Zhue0782ab2018-01-09 13:24:13 -050092 if (escape) {
93 if (kable_format == "html") {
94 footnote_contents <- lapply(footnote_contents, escape_html)
95 footnote_titles <- lapply(footnote_titles, escape_html)
96 } else {
Hao Zhud4630872018-03-26 11:26:36 -040097 footnote_contents <- lapply(footnote_contents, escape_latex2)
Hao Zhu1aff7342018-04-02 18:33:15 -040098 footnote_contents <- lapply(footnote_contents, linebreak)
Hao Zhud4630872018-03-26 11:26:36 -040099 footnote_titles <- lapply(footnote_titles, escape_latex2)
Hao Zhu1aff7342018-04-02 18:33:15 -0400100 footnote_titles <- lapply(footnote_titles, linebreak)
Hao Zhue0782ab2018-01-09 13:24:13 -0500101 }
102 }
Hao Zhu25efb132018-05-20 22:00:56 -0400103 title_format <- match.arg(title_format, c("italic", "bold", "underline"),
104 several.ok = TRUE)
105 footnote_titles <- lapply(footnote_titles, footnote_title_format,
106 kable_format, title_format)
Hao Zhu8dd65a92018-01-05 20:40:27 -0500107 footnote_table <- footnote_table_maker(
Hao Zhu149faed2018-10-23 16:36:22 -0400108 kable_format, footnote_titles, footnote_contents, symbol_manual
Hao Zhu8dd65a92018-01-05 20:40:27 -0500109 )
110 if (kable_format == "html") {
Hao Zhucdd7f922018-01-08 11:39:40 -0500111 return(footnote_html(kable_input, footnote_table, footnote_as_chunk))
Hao Zhu8dd65a92018-01-05 20:40:27 -0500112 }
Hao Zhu19c4fa52018-01-09 12:01:14 -0500113 if (kable_format == "latex") {
Hao Zhu17814c72018-01-10 11:32:14 -0500114 return(footnote_latex(kable_input, footnote_table, footnote_as_chunk,
115 threeparttable))
Hao Zhu19c4fa52018-01-09 12:01:14 -0500116 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500117}
118
Hao Zhu25efb132018-05-20 22:00:56 -0400119footnote_title_format <- function(x, format, title_format) {
120 if (x == "") return(x)
121 if (format == "html") {
122 title_style <- ""
123 if ("italic" %in% title_format) {
124 title_style <- paste0(title_style, "font-style: italic;")
125 }
126 if ("bold" %in% title_format) {
127 title_style <- paste0(title_style, "font-weight: bold;")
128 }
129 if ("underline" %in% title_format) {
130 title_style <- paste0(title_style, "text-decoration: underline;")
131 }
132 return(paste0(
133 '<span style="', title_style, '">', x, '</span>'
134 ))
135 } else {
136 if ("italic" %in% title_format) {
137 x <- paste0("\\\\textit\\{", x, "\\}")
138 }
139 if ("bold" %in% title_format) {
140 x <- paste0("\\\\textbf\\{", x, "\\}")
141 }
142 if ("underline" %in% title_format) {
143 x <- paste0("\\\\underline\\{", x, "\\}")
144 }
145 return(x)
146 }
147}
148
Hao Zhu149faed2018-10-23 16:36:22 -0400149footnote_table_maker <- function(format, footnote_titles, footnote_contents,
150 symbol_manual) {
151 if (is.null(symbol_manual)) {
152 number_index <- read.csv(system.file("symbol_index.csv",
153 package = "kableExtra"))
154 if (format == "latex") {
155 symbol_index <- number_index$symbol.latex
156 } else {
157 symbol_index <- number_index$symbol.html
158 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500159 } else {
Hao Zhu149faed2018-10-23 16:36:22 -0400160 symbol_index <- symbol_manual
Hao Zhu8dd65a92018-01-05 20:40:27 -0500161 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500162
Hao Zhu149faed2018-10-23 16:36:22 -0400163
Hao Zhu8dd65a92018-01-05 20:40:27 -0500164 if (!is.null(footnote_contents$general)) {
165 footnote_contents$general <- data.frame(
166 index = "",
Hao Zhubab692d2018-01-09 17:49:55 -0500167 footnote = footnote_contents$general
Hao Zhu8dd65a92018-01-05 20:40:27 -0500168 )
169 }
170 if (!is.null(footnote_contents$number)) {
171 footnote_contents$number <- data.frame(
172 index = as.character(1:length(footnote_contents$number)),
173 footnote = footnote_contents$number
174 )
175 }
176 if (!is.null(footnote_contents$alphabet)) {
177 footnote_contents$alphabet <- data.frame(
178 index = letters[1:length(footnote_contents$alphabet)],
179 footnote = footnote_contents$alphabet
180 )
181 }
182 if (!is.null(footnote_contents$symbol)) {
183 footnote_contents$symbol <- data.frame(
184 index = symbol_index[1:length(footnote_contents$symbol)],
185 footnote = footnote_contents$symbol
186 )
187 }
188
189 out <- list()
190 out$contents <- footnote_contents
191 out$titles <- footnote_titles
192 return(out)
193}
194
195# HTML
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500196footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500197 kable_attrs <- attributes(kable_input)
198 kable_xml <- read_kable_as_xml(kable_input)
199
Hao Zhucdd7f922018-01-08 11:39:40 -0500200 new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk)
Hao Zhu8dd65a92018-01-05 20:40:27 -0500201 xml_add_child(kable_xml, new_html_footnote)
202
203 out <- as_kable_xml(kable_xml)
204 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500205 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu8dd65a92018-01-05 20:40:27 -0500206 return(out)
207}
208
Hao Zhucdd7f922018-01-08 11:39:40 -0500209html_tfoot_maker <- function(footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500210 footnote_types <- names(footnote_table$contents)
211 footnote_text <- c()
212 for (i in footnote_types) {
Hao Zhucdd7f922018-01-08 11:39:40 -0500213 footnote_text <- c(footnote_text, html_tfoot_maker_(
Hao Zhu8dd65a92018-01-05 20:40:27 -0500214 footnote_table$contents[[i]], footnote_table$titles[[i]], i,
215 footnote_as_chunk))
216 }
217 footnote_text <- paste0(
218 "<tfoot>", paste0(footnote_text, collapse = ""), "</tfoot>"
219 )
220 footnote_node <- read_html(footnote_text, options = c("RECOVER", "NOERROR"))
221 return(xml_child(xml_child(footnote_node, 1), 1))
222}
223
Hao Zhucdd7f922018-01-08 11:39:40 -0500224html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500225 footnote_text <- apply(ft_contents, 1, function(x) {
226 paste0('<sup>', x[1], '</sup> ', x[2])
227 })
228 if (ft_title != "") {
Hao Zhu25efb132018-05-20 22:00:56 -0400229 title_text <- ft_title
Hao Zhu8dd65a92018-01-05 20:40:27 -0500230 footnote_text <- c(title_text, footnote_text)
231 }
232 if (!ft_chunk) {
233 footnote_text <- paste0(
234 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
235 footnote_text, '</td></tr>'
236 )
237 } else {
238 footnote_text <- paste0(
239 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
Hao Zhucdd7f922018-01-08 11:39:40 -0500240 paste0(footnote_text, collapse = " "),
Hao Zhu8dd65a92018-01-05 20:40:27 -0500241 '</td></tr>'
242 )
243 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500244 return(footnote_text)
245}
Hao Zhucdd7f922018-01-08 11:39:40 -0500246
247# LaTeX
Hao Zhu17814c72018-01-10 11:32:14 -0500248footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk,
249 threeparttable) {
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500250 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -0400251 out <- solve_enc(kable_input)
Hao Zhu17814c72018-01-10 11:32:14 -0500252
Hao Zhu19c4fa52018-01-09 12:01:14 -0500253 footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk,
Hao Zhu17814c72018-01-10 11:32:14 -0500254 table_info$ncol, threeparttable)
255 if (threeparttable) {
Hao Zhu23bde3a2018-03-28 16:00:55 -0400256 if (table_info$tabular %in% c("longtable", "longtabu") ) {
Hao Zhu17814c72018-01-10 11:32:14 -0500257 out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"),
Hao Zhu27c7c852018-03-26 16:18:23 -0400258 paste0("\\\\begin{ThreePartTable}\n\\\\begin{TableNotes}",
259 ifelse(footnote_as_chunk, "[para]", ""),
Augusto Magalhãesf17fa462019-03-05 12:55:21 +0900260 ifelse(fixed_small_size,"\n\\\\small\n","\n"), footnote_text,
Hao Zhu27c7c852018-03-26 16:18:23 -0400261 "\n\\\\end{TableNotes}\n\\\\begin{",
Hao Zhu17814c72018-01-10 11:32:14 -0500262 table_info$tabular, "}"),
263 out)
Hao Zhu23bde3a2018-03-28 16:00:55 -0400264 out <- sub(paste0("\\\\end\\{",table_info$tabular, "\\}"),
265 paste0("\\\\end{", table_info$tabular,
266 "}\n\\\\end{ThreePartTable}"),
267 out)
268 if (table_info$booktabs) {
269 out <- sub("\\\\bottomrule", "\\\\bottomrule\n\\\\insertTableNotes", out)
270 } else {
271 out <- sub("\\\\hline\n\\\\end\\{longtable\\}",
272 "\\\\hline\n\\\\insertTableNotes\n\\\\end\\{longtable\\}",
273 out)
274 }
Hao Zhu27c7c852018-03-26 16:18:23 -0400275 } else {
Hao Zhu23bde3a2018-03-28 16:00:55 -0400276 if (table_info$tabular == "tabu") {
277 stop("Please use `longtable = T` in your kable function. ",
278 "Full width threeparttable only works with longtable.")
279 }
280 out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"),
281 paste0("\\\\begin{threeparttable}\n\\\\begin{",
282 table_info$tabular, "}"),
283 out)
284 out <- sub(table_info$end_tabular,
285 paste0("\\\\end{", table_info$tabular,
286 "}\n\\\\begin{tablenotes}",
287 ifelse(footnote_as_chunk, "[para]", ""),
Augusto Magalhãesf17fa462019-03-05 12:55:21 +0900288 ifelse(fixed_small_size,"\n\\\\small\n","\n"), footnote_text,
Hao Zhu23bde3a2018-03-28 16:00:55 -0400289 "\n\\\\end{tablenotes}\n\\\\end{threeparttable}"),
Hao Zhu27c7c852018-03-26 16:18:23 -0400290 out)
291 }
Hao Zhu17814c72018-01-10 11:32:14 -0500292 } else {
Hao Zhu23bde3a2018-03-28 16:00:55 -0400293 if (table_info$booktabs) {
294 out <- sub("\\\\bottomrule",
295 paste0("\\\\bottomrule\n", footnote_text), out)
296 } else {
297 out <- sub(table_info$end_tabular,
298 paste0(footnote_text, "\n\\\\end{", table_info$tabular, "}"),
299 out)
300 }
Hao Zhu17814c72018-01-10 11:32:14 -0500301 }
302
Hao Zhu19c4fa52018-01-09 12:01:14 -0500303 out <- structure(out, format = "latex", class = "knitr_kable")
304 attr(out, "kable_meta") <- table_info
305 return(out)
Hao Zhu19c4fa52018-01-09 12:01:14 -0500306}
Hao Zhucdd7f922018-01-08 11:39:40 -0500307
Hao Zhu17814c72018-01-10 11:32:14 -0500308latex_tfoot_maker <- function(footnote_table, footnote_as_chunk, ncol,
309 threeparttable) {
Hao Zhu19c4fa52018-01-09 12:01:14 -0500310 footnote_types <- names(footnote_table$contents)
311 footnote_text <- c()
Hao Zhu17814c72018-01-10 11:32:14 -0500312 if (threeparttable) {
313 for (i in footnote_types) {
314 footnote_text <- c(footnote_text, latex_tfoot_maker_tpt_(
315 footnote_table$contents[[i]], footnote_table$titles[[i]],
316 footnote_as_chunk, ncol))
317 }
318 } else {
319 for (i in footnote_types) {
320 footnote_text <- c(footnote_text, latex_tfoot_maker_(
321 footnote_table$contents[[i]], footnote_table$titles[[i]],
322 footnote_as_chunk, ncol))
323 }
Hao Zhu19c4fa52018-01-09 12:01:14 -0500324 }
325 footnote_text <- paste0(footnote_text, collapse = "\n")
326 return(footnote_text)
327}
Hao Zhu9f917482018-01-08 18:09:33 -0500328
Hao Zhu17814c72018-01-10 11:32:14 -0500329latex_tfoot_maker_ <- function(ft_contents, ft_title, ft_chunk, ncol) {
Hao Zhu19c4fa52018-01-09 12:01:14 -0500330 footnote_text <- apply(ft_contents, 1, function(x) {
331 if (x[1] == "") {
332 x[2]
333 } else {
334 paste0('\\\\textsuperscript{', x[1], '} ', x[2])
335 }
336 })
337 if (ft_title != "") {
Hao Zhu25efb132018-05-20 22:00:56 -0400338 title_text <- ft_title
Hao Zhu19c4fa52018-01-09 12:01:14 -0500339 footnote_text <- c(title_text, footnote_text)
340 }
341 if (!ft_chunk) {
342 footnote_text <- paste0(
343 '\\\\multicolumn{', ncol, '}{l}{', footnote_text, '}\\\\\\\\'
344 )
345 } else {
346 footnote_text <- paste0(
347 '\\\\multicolumn{', ncol, '}{l}{',
348 paste0(footnote_text, collapse = " "),
349 '}\\\\\\\\'
350 )
351 }
352 return(footnote_text)
Hao Zhucdd7f922018-01-08 11:39:40 -0500353}
Hao Zhu17814c72018-01-10 11:32:14 -0500354
355latex_tfoot_maker_tpt_ <- function(ft_contents, ft_title, ft_chunk, ncol) {
356 footnote_text <- apply(ft_contents, 1, function(x) {
357 if (x[1] == "") {
358 paste0('\\\\item ', x[2])
359 } else {
360 paste0('\\\\item[', x[1], '] ', x[2])
361 }
362 })
363 if (ft_title != "") {
Hao Zhu25efb132018-05-20 22:00:56 -0400364 title_text <- paste0('\\\\item ', ft_title, ' ')
Hao Zhu17814c72018-01-10 11:32:14 -0500365 footnote_text <- c(title_text, footnote_text)
366 }
367 footnote_text <- paste0(footnote_text, collapse = "\n")
368 # if (!ft_chunk) {
369 # footnote_text <- paste0(footnote_text, collapse = "\n")
370 # } else {
371 # footnote_text <- paste0(footnote_text, collapse = " ")
372 # }
373 return(footnote_text)
374}