blob: 296c9773400625bc63a452c6a976ab0e44b61e9a [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.
23#' @param general_title Section header for general footnotes. Default is
24#' "Note: ".
25#' @param number_title Section header for number footnotes. Default is "".
26#' @param alphabet_title Section header for alphabet footnotes. Default is "".
27#' @param symbol_title Section header for symbol footnotes. Default is "".
28#'
Hao Zhub1e2e3d2018-01-09 13:31:42 -050029#' @examples dt <- mtcars[1:5, 1:5]
Hao Zhu593f57e2018-01-09 13:30:01 -050030#' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b"))
31#'
Hao Zhu8dd65a92018-01-05 20:40:27 -050032#' @export
Hao Zhucdd7f922018-01-08 11:39:40 -050033footnote <- function(kable_input,
Hao Zhu1ac13ad2018-01-08 16:12:24 -050034 general = NULL,
35 number = NULL,
36 alphabet = NULL,
37 symbol = NULL,
38 footnote_order = c("general", "number",
39 "alphabet", "symbol"),
40 footnote_as_chunk = FALSE,
Hao Zhue0782ab2018-01-09 13:24:13 -050041 escape = TRUE,
Hao Zhu1ac13ad2018-01-08 16:12:24 -050042 general_title = "Note: ",
43 number_title = "",
44 alphabet_title = "",
45 symbol_title = ""
Hao Zhu8dd65a92018-01-05 20:40:27 -050046) {
47 kable_format <- attr(kable_input, "format")
48 if (!kable_format %in% c("html", "latex")) {
49 message("Currently generic markdown table using pandoc is not supported.")
50 return(kable_input)
51 }
Hao Zhu8dd65a92018-01-05 20:40:27 -050052 if (length(alphabet) > 26) {
53 alphabet <- alphabet[1:26]
54 warning("Please don't use more than 26 footnotes in table_footnote ",
55 "alphabet. Use number instead.")
56 }
57 if (length(symbol) > 20) {
58 symbol <- symbol[1:20]
59 warning("Please don't use more than 20 footnotes in table_footnote ",
60 "symbol. Use number instead.")
61 }
Hao Zhue0782ab2018-01-09 13:24:13 -050062 footnote_titles <- list(
63 general = general_title, number = number_title,
64 alphabet = alphabet_title, symbol = symbol_title
65 )
66 footnote_contents <- list(
67 general = general, number = number, alphabet = alphabet, symbol = symbol
68 )
69 notnull <- names(footnote_contents)[!sapply(footnote_contents, is.null)]
70 if (length(notnull) == 0) {return(kable_input)}
Hao Zhu8dd65a92018-01-05 20:40:27 -050071 footnote_order <- footnote_order[footnote_order %in% notnull]
72 footnote_titles <- footnote_titles[footnote_order]
73 footnote_contents <- footnote_contents[footnote_order]
Hao Zhue0782ab2018-01-09 13:24:13 -050074 if (escape) {
75 if (kable_format == "html") {
76 footnote_contents <- lapply(footnote_contents, escape_html)
77 footnote_titles <- lapply(footnote_titles, escape_html)
78 } else {
79 footnote_contents <- lapply(footnote_contents, escape_latex)
80 footnote_titles <- lapply(footnote_titles, escape_latex)
81 }
82 }
Hao Zhu8dd65a92018-01-05 20:40:27 -050083 footnote_table <- footnote_table_maker(
84 kable_format, footnote_titles, footnote_contents
85 )
86 if (kable_format == "html") {
Hao Zhucdd7f922018-01-08 11:39:40 -050087 return(footnote_html(kable_input, footnote_table, footnote_as_chunk))
Hao Zhu8dd65a92018-01-05 20:40:27 -050088 }
Hao Zhu19c4fa52018-01-09 12:01:14 -050089 if (kable_format == "latex") {
90 return(footnote_latex(kable_input, footnote_table, footnote_as_chunk))
91 }
Hao Zhu8dd65a92018-01-05 20:40:27 -050092}
93
94footnote_table_maker <- function(format, footnote_titles, footnote_contents) {
95 number_index <- read.csv(system.file("symbol_index.csv",
96 package = "kableExtra"))
97 if (format == "latex") {
98 symbol_index <- number_index$symbol.latex
99 } else {
100 symbol_index <- number_index$symbol.html
101 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500102
103 if (!is.null(footnote_contents$general)) {
104 footnote_contents$general <- data.frame(
105 index = "",
Hao Zhubab692d2018-01-09 17:49:55 -0500106 footnote = footnote_contents$general
Hao Zhu8dd65a92018-01-05 20:40:27 -0500107 )
108 }
109 if (!is.null(footnote_contents$number)) {
110 footnote_contents$number <- data.frame(
111 index = as.character(1:length(footnote_contents$number)),
112 footnote = footnote_contents$number
113 )
114 }
115 if (!is.null(footnote_contents$alphabet)) {
116 footnote_contents$alphabet <- data.frame(
117 index = letters[1:length(footnote_contents$alphabet)],
118 footnote = footnote_contents$alphabet
119 )
120 }
121 if (!is.null(footnote_contents$symbol)) {
122 footnote_contents$symbol <- data.frame(
123 index = symbol_index[1:length(footnote_contents$symbol)],
124 footnote = footnote_contents$symbol
125 )
126 }
127
128 out <- list()
129 out$contents <- footnote_contents
130 out$titles <- footnote_titles
131 return(out)
132}
133
134# HTML
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500135footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500136 kable_attrs <- attributes(kable_input)
137 kable_xml <- read_kable_as_xml(kable_input)
138
Hao Zhucdd7f922018-01-08 11:39:40 -0500139 new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk)
Hao Zhu8dd65a92018-01-05 20:40:27 -0500140 xml_add_child(kable_xml, new_html_footnote)
141
142 out <- as_kable_xml(kable_xml)
143 attributes(out) <- kable_attrs
144 return(out)
145}
146
Hao Zhucdd7f922018-01-08 11:39:40 -0500147html_tfoot_maker <- function(footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500148 footnote_types <- names(footnote_table$contents)
149 footnote_text <- c()
150 for (i in footnote_types) {
Hao Zhucdd7f922018-01-08 11:39:40 -0500151 footnote_text <- c(footnote_text, html_tfoot_maker_(
Hao Zhu8dd65a92018-01-05 20:40:27 -0500152 footnote_table$contents[[i]], footnote_table$titles[[i]], i,
153 footnote_as_chunk))
154 }
155 footnote_text <- paste0(
156 "<tfoot>", paste0(footnote_text, collapse = ""), "</tfoot>"
157 )
158 footnote_node <- read_html(footnote_text, options = c("RECOVER", "NOERROR"))
159 return(xml_child(xml_child(footnote_node, 1), 1))
160}
161
Hao Zhucdd7f922018-01-08 11:39:40 -0500162html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500163 footnote_text <- apply(ft_contents, 1, function(x) {
164 paste0('<sup>', x[1], '</sup> ', x[2])
165 })
166 if (ft_title != "") {
167 title_text <- paste0('<strong>', ft_title, '</strong>')
168 footnote_text <- c(title_text, footnote_text)
169 }
170 if (!ft_chunk) {
171 footnote_text <- paste0(
172 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
173 footnote_text, '</td></tr>'
174 )
175 } else {
176 footnote_text <- paste0(
177 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
Hao Zhucdd7f922018-01-08 11:39:40 -0500178 paste0(footnote_text, collapse = " "),
Hao Zhu8dd65a92018-01-05 20:40:27 -0500179 '</td></tr>'
180 )
181 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500182 return(footnote_text)
183}
Hao Zhucdd7f922018-01-08 11:39:40 -0500184
185# LaTeX
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500186footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk) {
187 table_info <- magic_mirror(kable_input)
Hao Zhu19c4fa52018-01-09 12:01:14 -0500188 out <- enc2utf8(as.character(kable_input))
Hao Zhu19c4fa52018-01-09 12:01:14 -0500189 footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk,
190 table_info$ncol)
191 out <- sub(table_info$end_tabular,
192 paste0(footnote_text, "\n\\\\end{", table_info$tabular, "}"),
193 out)
194 out <- structure(out, format = "latex", class = "knitr_kable")
195 attr(out, "kable_meta") <- table_info
196 return(out)
Hao Zhu19c4fa52018-01-09 12:01:14 -0500197}
Hao Zhucdd7f922018-01-08 11:39:40 -0500198
Hao Zhu19c4fa52018-01-09 12:01:14 -0500199latex_tfoot_maker <- function(footnote_table, footnote_as_chunk, ncol) {
200 footnote_types <- names(footnote_table$contents)
201 footnote_text <- c()
202 for (i in footnote_types) {
203 footnote_text <- c(footnote_text, latex_tfoot_maker_(
204 footnote_table$contents[[i]], footnote_table$titles[[i]], i,
205 footnote_as_chunk, ncol))
206 }
207 footnote_text <- paste0(footnote_text, collapse = "\n")
208 return(footnote_text)
209}
Hao Zhu9f917482018-01-08 18:09:33 -0500210
Hao Zhu19c4fa52018-01-09 12:01:14 -0500211latex_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk, ncol) {
212 footnote_text <- apply(ft_contents, 1, function(x) {
213 if (x[1] == "") {
214 x[2]
215 } else {
216 paste0('\\\\textsuperscript{', x[1], '} ', x[2])
217 }
218 })
219 if (ft_title != "") {
220 title_text <- paste0('\\\\textbf{', ft_title, '} ')
221 footnote_text <- c(title_text, footnote_text)
222 }
223 if (!ft_chunk) {
224 footnote_text <- paste0(
225 '\\\\multicolumn{', ncol, '}{l}{', footnote_text, '}\\\\\\\\'
226 )
227 } else {
228 footnote_text <- paste0(
229 '\\\\multicolumn{', ncol, '}{l}{',
230 paste0(footnote_text, collapse = " "),
231 '}\\\\\\\\'
232 )
233 }
234 return(footnote_text)
Hao Zhucdd7f922018-01-08 11:39:40 -0500235}