blob: 8863327a7ad3a8ae7d990e0457d51e07e6fc9192 [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 Zhu8dd65a92018-01-05 20:40:27 -050029#' @export
Hao Zhucdd7f922018-01-08 11:39:40 -050030footnote <- function(kable_input,
Hao Zhu1ac13ad2018-01-08 16:12:24 -050031 general = NULL,
32 number = NULL,
33 alphabet = NULL,
34 symbol = NULL,
35 footnote_order = c("general", "number",
36 "alphabet", "symbol"),
37 footnote_as_chunk = FALSE,
Hao Zhue0782ab2018-01-09 13:24:13 -050038 escape = TRUE,
Hao Zhu1ac13ad2018-01-08 16:12:24 -050039 general_title = "Note: ",
40 number_title = "",
41 alphabet_title = "",
42 symbol_title = ""
Hao Zhu8dd65a92018-01-05 20:40:27 -050043) {
44 kable_format <- attr(kable_input, "format")
45 if (!kable_format %in% c("html", "latex")) {
46 message("Currently generic markdown table using pandoc is not supported.")
47 return(kable_input)
48 }
Hao Zhu8dd65a92018-01-05 20:40:27 -050049 if (length(alphabet) > 26) {
50 alphabet <- alphabet[1:26]
51 warning("Please don't use more than 26 footnotes in table_footnote ",
52 "alphabet. Use number instead.")
53 }
54 if (length(symbol) > 20) {
55 symbol <- symbol[1:20]
56 warning("Please don't use more than 20 footnotes in table_footnote ",
57 "symbol. Use number instead.")
58 }
Hao Zhue0782ab2018-01-09 13:24:13 -050059 footnote_titles <- list(
60 general = general_title, number = number_title,
61 alphabet = alphabet_title, symbol = symbol_title
62 )
63 footnote_contents <- list(
64 general = general, number = number, alphabet = alphabet, symbol = symbol
65 )
66 notnull <- names(footnote_contents)[!sapply(footnote_contents, is.null)]
67 if (length(notnull) == 0) {return(kable_input)}
Hao Zhu8dd65a92018-01-05 20:40:27 -050068 footnote_order <- footnote_order[footnote_order %in% notnull]
69 footnote_titles <- footnote_titles[footnote_order]
70 footnote_contents <- footnote_contents[footnote_order]
Hao Zhue0782ab2018-01-09 13:24:13 -050071 footnote_contents <- lapply(footnote_contents, esca)
72 if (escape) {
73 if (kable_format == "html") {
74 footnote_contents <- lapply(footnote_contents, escape_html)
75 footnote_titles <- lapply(footnote_titles, escape_html)
76 } else {
77 footnote_contents <- lapply(footnote_contents, escape_latex)
78 footnote_titles <- lapply(footnote_titles, escape_latex)
79 }
80 }
Hao Zhu8dd65a92018-01-05 20:40:27 -050081 footnote_table <- footnote_table_maker(
82 kable_format, footnote_titles, footnote_contents
83 )
84 if (kable_format == "html") {
Hao Zhucdd7f922018-01-08 11:39:40 -050085 return(footnote_html(kable_input, footnote_table, footnote_as_chunk))
Hao Zhu8dd65a92018-01-05 20:40:27 -050086 }
Hao Zhu19c4fa52018-01-09 12:01:14 -050087 if (kable_format == "latex") {
88 return(footnote_latex(kable_input, footnote_table, footnote_as_chunk))
89 }
Hao Zhu8dd65a92018-01-05 20:40:27 -050090}
91
92footnote_table_maker <- function(format, footnote_titles, footnote_contents) {
93 number_index <- read.csv(system.file("symbol_index.csv",
94 package = "kableExtra"))
95 if (format == "latex") {
96 symbol_index <- number_index$symbol.latex
97 } else {
98 symbol_index <- number_index$symbol.html
99 }
100 contents_length <- sapply(footnote_contents, length)
101
102 if (!is.null(footnote_contents$general)) {
103 footnote_contents$general <- data.frame(
104 index = "",
105 footnote = paste(footnote_contents$general, collapse = " ")
106 )
107 }
108 if (!is.null(footnote_contents$number)) {
109 footnote_contents$number <- data.frame(
110 index = as.character(1:length(footnote_contents$number)),
111 footnote = footnote_contents$number
112 )
113 }
114 if (!is.null(footnote_contents$alphabet)) {
115 footnote_contents$alphabet <- data.frame(
116 index = letters[1:length(footnote_contents$alphabet)],
117 footnote = footnote_contents$alphabet
118 )
119 }
120 if (!is.null(footnote_contents$symbol)) {
121 footnote_contents$symbol <- data.frame(
122 index = symbol_index[1:length(footnote_contents$symbol)],
123 footnote = footnote_contents$symbol
124 )
125 }
126
127 out <- list()
128 out$contents <- footnote_contents
129 out$titles <- footnote_titles
130 return(out)
131}
132
133# HTML
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500134footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500135 kable_attrs <- attributes(kable_input)
136 kable_xml <- read_kable_as_xml(kable_input)
137
Hao Zhucdd7f922018-01-08 11:39:40 -0500138 new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk)
Hao Zhu8dd65a92018-01-05 20:40:27 -0500139 xml_add_child(kable_xml, new_html_footnote)
140
141 out <- as_kable_xml(kable_xml)
142 attributes(out) <- kable_attrs
143 return(out)
144}
145
Hao Zhucdd7f922018-01-08 11:39:40 -0500146html_tfoot_maker <- function(footnote_table, footnote_as_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500147 footnote_types <- names(footnote_table$contents)
148 footnote_text <- c()
149 for (i in footnote_types) {
Hao Zhucdd7f922018-01-08 11:39:40 -0500150 footnote_text <- c(footnote_text, html_tfoot_maker_(
Hao Zhu8dd65a92018-01-05 20:40:27 -0500151 footnote_table$contents[[i]], footnote_table$titles[[i]], i,
152 footnote_as_chunk))
153 }
154 footnote_text <- paste0(
155 "<tfoot>", paste0(footnote_text, collapse = ""), "</tfoot>"
156 )
157 footnote_node <- read_html(footnote_text, options = c("RECOVER", "NOERROR"))
158 return(xml_child(xml_child(footnote_node, 1), 1))
159}
160
Hao Zhucdd7f922018-01-08 11:39:40 -0500161html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) {
Hao Zhu8dd65a92018-01-05 20:40:27 -0500162 footnote_text <- apply(ft_contents, 1, function(x) {
163 paste0('<sup>', x[1], '</sup> ', x[2])
164 })
165 if (ft_title != "") {
166 title_text <- paste0('<strong>', ft_title, '</strong>')
167 footnote_text <- c(title_text, footnote_text)
168 }
169 if (!ft_chunk) {
170 footnote_text <- paste0(
171 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
172 footnote_text, '</td></tr>'
173 )
174 } else {
175 footnote_text <- paste0(
176 '<tr><td style="padding: 0; border: 0;" colspan="100%">',
Hao Zhucdd7f922018-01-08 11:39:40 -0500177 paste0(footnote_text, collapse = " "),
Hao Zhu8dd65a92018-01-05 20:40:27 -0500178 '</td></tr>'
179 )
180 }
Hao Zhu8dd65a92018-01-05 20:40:27 -0500181 return(footnote_text)
182}
Hao Zhucdd7f922018-01-08 11:39:40 -0500183
184# LaTeX
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500185footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk) {
186 table_info <- magic_mirror(kable_input)
Hao Zhu19c4fa52018-01-09 12:01:14 -0500187 out <- enc2utf8(as.character(kable_input))
188 if (table_info$tabular == "longtable") {
189 return()
190 }
191 footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk,
192 table_info$ncol)
193 out <- sub(table_info$end_tabular,
194 paste0(footnote_text, "\n\\\\end{", table_info$tabular, "}"),
195 out)
196 out <- structure(out, format = "latex", class = "knitr_kable")
197 attr(out, "kable_meta") <- table_info
198 return(out)
Hao Zhu1ac13ad2018-01-08 16:12:24 -0500199
Hao Zhu19c4fa52018-01-09 12:01:14 -0500200}
Hao Zhucdd7f922018-01-08 11:39:40 -0500201
Hao Zhu19c4fa52018-01-09 12:01:14 -0500202latex_tfoot_maker <- function(footnote_table, footnote_as_chunk, ncol) {
203 footnote_types <- names(footnote_table$contents)
204 footnote_text <- c()
205 for (i in footnote_types) {
206 footnote_text <- c(footnote_text, latex_tfoot_maker_(
207 footnote_table$contents[[i]], footnote_table$titles[[i]], i,
208 footnote_as_chunk, ncol))
209 }
210 footnote_text <- paste0(footnote_text, collapse = "\n")
211 return(footnote_text)
212}
Hao Zhu9f917482018-01-08 18:09:33 -0500213
Hao Zhu19c4fa52018-01-09 12:01:14 -0500214latex_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk, ncol) {
215 footnote_text <- apply(ft_contents, 1, function(x) {
216 if (x[1] == "") {
217 x[2]
218 } else {
219 paste0('\\\\textsuperscript{', x[1], '} ', x[2])
220 }
221 })
222 if (ft_title != "") {
223 title_text <- paste0('\\\\textbf{', ft_title, '} ')
224 footnote_text <- c(title_text, footnote_text)
225 }
226 if (!ft_chunk) {
227 footnote_text <- paste0(
228 '\\\\multicolumn{', ncol, '}{l}{', footnote_text, '}\\\\\\\\'
229 )
230 } else {
231 footnote_text <- paste0(
232 '\\\\multicolumn{', ncol, '}{l}{',
233 paste0(footnote_text, collapse = " "),
234 '}\\\\\\\\'
235 )
236 }
237 return(footnote_text)
Hao Zhucdd7f922018-01-08 11:39:40 -0500238}