blob: b7e58c20fd64c5da73b7f907b5e532a346e09340 [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 }
102 contents_length <- sapply(footnote_contents, length)
103
104 if (!is.null(footnote_contents$general)) {
105 footnote_contents$general <- data.frame(
106 index = "",
107 footnote = paste(footnote_contents$general, collapse = " ")
108 )
109 }
110 if (!is.null(footnote_contents$number)) {
111 footnote_contents$number <- data.frame(
112 index = as.character(1:length(footnote_contents$number)),
113 footnote = footnote_contents$number
114 )
115 }
116 if (!is.null(footnote_contents$alphabet)) {
117 footnote_contents$alphabet <- data.frame(
118 index = letters[1:length(footnote_contents$alphabet)],
119 footnote = footnote_contents$alphabet
120 )
121 }
122 if (!is.null(footnote_contents$symbol)) {
123 footnote_contents$symbol <- data.frame(
124 index = symbol_index[1:length(footnote_contents$symbol)],
125 footnote = footnote_contents$symbol
126 )
127 }
128
129 out <- list()
130 out$contents <- footnote_contents
131 out$titles <- footnote_titles
132 return(out)
133}
134
135# HTML
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500136footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500137 kable_attrs <- attributes(kable_input)
138 kable_xml <- read_kable_as_xml(kable_input)
139
Hao Zhucdd7f922018-01-08 11:39:40 -0500140 new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk)
Hao Zhu8dd65a92018-01-05 20:40:27 -0500141 xml_add_child(kable_xml, new_html_footnote)
142
143 out <- as_kable_xml(kable_xml)
144 attributes(out) <- kable_attrs
145 return(out)
146}
147
Hao Zhucdd7f922018-01-08 11:39:40 -0500148html_tfoot_maker <- function(footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500149 footnote_types <- names(footnote_table$contents)
150 footnote_text <- c()
151 for (i in footnote_types) {
Hao Zhucdd7f922018-01-08 11:39:40 -0500152 footnote_text <- c(footnote_text, html_tfoot_maker_(
Hao Zhu8dd65a92018-01-05 20:40:27 -0500153 footnote_table$contents[[i]], footnote_table$titles[[i]], i,
154 footnote_as_chunk))
155 }
156 footnote_text <- paste0(
157 "<tfoot>", paste0(footnote_text, collapse = ""), "</tfoot>"
158 )
159 footnote_node <- read_html(footnote_text, options = c("RECOVER", "NOERROR"))
160 return(xml_child(xml_child(footnote_node, 1), 1))
161}
162
Hao Zhucdd7f922018-01-08 11:39:40 -0500163html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500164 footnote_text <- apply(ft_contents, 1, function(x) {
165 paste0('<sup>', x[1], '</sup> ', x[2])
166 })
167 if (ft_title != "") {
168 title_text <- paste0('<strong>', ft_title, '</strong>')
169 footnote_text <- c(title_text, footnote_text)
170 }
171 if (!ft_chunk) {
172 footnote_text <- paste0(
173 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
174 footnote_text, '</td></tr>'
175 )
176 } else {
177 footnote_text <- paste0(
178 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
Hao Zhucdd7f922018-01-08 11:39:40 -0500179 paste0(footnote_text, collapse = " "),
Hao Zhu8dd65a92018-01-05 20:40:27 -0500180 '</td></tr>'
181 )
182 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500183 return(footnote_text)
184}
Hao Zhucdd7f922018-01-08 11:39:40 -0500185
186# LaTeX
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500187footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk) {
188 table_info <- magic_mirror(kable_input)
Hao Zhu19c4fa52018-01-09 12:01:14 -0500189 out <- enc2utf8(as.character(kable_input))
Hao Zhu19c4fa52018-01-09 12:01:14 -0500190 footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk,
191 table_info$ncol)
192 out <- sub(table_info$end_tabular,
193 paste0(footnote_text, "\n\\\\end{", table_info$tabular, "}"),
194 out)
195 out <- structure(out, format = "latex", class = "knitr_kable")
196 attr(out, "kable_meta") <- table_info
197 return(out)
Hao Zhu19c4fa52018-01-09 12:01:14 -0500198}
Hao Zhucdd7f922018-01-08 11:39:40 -0500199
Hao Zhu19c4fa52018-01-09 12:01:14 -0500200latex_tfoot_maker <- function(footnote_table, footnote_as_chunk, ncol) {
201 footnote_types <- names(footnote_table$contents)
202 footnote_text <- c()
203 for (i in footnote_types) {
204 footnote_text <- c(footnote_text, latex_tfoot_maker_(
205 footnote_table$contents[[i]], footnote_table$titles[[i]], i,
206 footnote_as_chunk, ncol))
207 }
208 footnote_text <- paste0(footnote_text, collapse = "\n")
209 return(footnote_text)
210}
Hao Zhu9f917482018-01-08 18:09:33 -0500211
Hao Zhu19c4fa52018-01-09 12:01:14 -0500212latex_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk, ncol) {
213 footnote_text <- apply(ft_contents, 1, function(x) {
214 if (x[1] == "") {
215 x[2]
216 } else {
217 paste0('\\\\textsuperscript{', x[1], '} ', x[2])
218 }
219 })
220 if (ft_title != "") {
221 title_text <- paste0('\\\\textbf{', ft_title, '} ')
222 footnote_text <- c(title_text, footnote_text)
223 }
224 if (!ft_chunk) {
225 footnote_text <- paste0(
226 '\\\\multicolumn{', ncol, '}{l}{', footnote_text, '}\\\\\\\\'
227 )
228 } else {
229 footnote_text <- paste0(
230 '\\\\multicolumn{', ncol, '}{l}{',
231 paste0(footnote_text, collapse = " "),
232 '}\\\\\\\\'
233 )
234 }
235 return(footnote_text)
Hao Zhucdd7f922018-01-08 11:39:40 -0500236}