Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 1 | #' Add footnote (new) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 2 | #' |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 3 | #' @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 Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 23 | #' @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. |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 27 | #' @param general_title Section header for general footnotes. Default is |
| 28 | #' "Note: ". |
| 29 | #' @param number_title Section header for number footnotes. Default is "". |
| 30 | #' @param alphabet_title Section header for alphabet footnotes. Default is "". |
| 31 | #' @param symbol_title Section header for symbol footnotes. Default is "". |
| 32 | #' |
Hao Zhu | b1e2e3d | 2018-01-09 13:31:42 -0500 | [diff] [blame] | 33 | #' @examples dt <- mtcars[1:5, 1:5] |
Hao Zhu | 593f57e | 2018-01-09 13:30:01 -0500 | [diff] [blame] | 34 | #' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b")) |
| 35 | #' |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 36 | #' @export |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 37 | footnote <- function(kable_input, |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 38 | general = NULL, |
| 39 | number = NULL, |
| 40 | alphabet = NULL, |
| 41 | symbol = NULL, |
| 42 | footnote_order = c("general", "number", |
| 43 | "alphabet", "symbol"), |
| 44 | footnote_as_chunk = FALSE, |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 45 | escape = TRUE, |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 46 | threeparttable = FALSE, |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 47 | general_title = "Note: ", |
| 48 | number_title = "", |
| 49 | alphabet_title = "", |
| 50 | symbol_title = "" |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 51 | ) { |
| 52 | kable_format <- attr(kable_input, "format") |
| 53 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 54 | warning("Please specify format in kable. kableExtra can customize either ", |
| 55 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 56 | "for details.") |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 57 | return(kable_input) |
| 58 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 59 | if (length(alphabet) > 26) { |
| 60 | alphabet <- alphabet[1:26] |
| 61 | warning("Please don't use more than 26 footnotes in table_footnote ", |
| 62 | "alphabet. Use number instead.") |
| 63 | } |
| 64 | if (length(symbol) > 20) { |
| 65 | symbol <- symbol[1:20] |
| 66 | warning("Please don't use more than 20 footnotes in table_footnote ", |
| 67 | "symbol. Use number instead.") |
| 68 | } |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 69 | footnote_titles <- list( |
| 70 | general = general_title, number = number_title, |
| 71 | alphabet = alphabet_title, symbol = symbol_title |
| 72 | ) |
| 73 | footnote_contents <- list( |
| 74 | general = general, number = number, alphabet = alphabet, symbol = symbol |
| 75 | ) |
| 76 | notnull <- names(footnote_contents)[!sapply(footnote_contents, is.null)] |
| 77 | if (length(notnull) == 0) {return(kable_input)} |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 78 | footnote_order <- footnote_order[footnote_order %in% notnull] |
| 79 | footnote_titles <- footnote_titles[footnote_order] |
| 80 | footnote_contents <- footnote_contents[footnote_order] |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 81 | if (escape) { |
| 82 | if (kable_format == "html") { |
| 83 | footnote_contents <- lapply(footnote_contents, escape_html) |
| 84 | footnote_titles <- lapply(footnote_titles, escape_html) |
| 85 | } else { |
Hao Zhu | d463087 | 2018-03-26 11:26:36 -0400 | [diff] [blame] | 86 | footnote_contents <- lapply(footnote_contents, escape_latex2) |
| 87 | footnote_titles <- lapply(footnote_titles, escape_latex2) |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 88 | } |
| 89 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 90 | footnote_table <- footnote_table_maker( |
| 91 | kable_format, footnote_titles, footnote_contents |
| 92 | ) |
| 93 | if (kable_format == "html") { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 94 | return(footnote_html(kable_input, footnote_table, footnote_as_chunk)) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 95 | } |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 96 | if (kable_format == "latex") { |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 97 | return(footnote_latex(kable_input, footnote_table, footnote_as_chunk, |
| 98 | threeparttable)) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 99 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | footnote_table_maker <- function(format, footnote_titles, footnote_contents) { |
| 103 | number_index <- read.csv(system.file("symbol_index.csv", |
| 104 | package = "kableExtra")) |
| 105 | if (format == "latex") { |
| 106 | symbol_index <- number_index$symbol.latex |
| 107 | } else { |
| 108 | symbol_index <- number_index$symbol.html |
| 109 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 110 | |
| 111 | if (!is.null(footnote_contents$general)) { |
| 112 | footnote_contents$general <- data.frame( |
| 113 | index = "", |
Hao Zhu | bab692d | 2018-01-09 17:49:55 -0500 | [diff] [blame] | 114 | footnote = footnote_contents$general |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 115 | ) |
| 116 | } |
| 117 | if (!is.null(footnote_contents$number)) { |
| 118 | footnote_contents$number <- data.frame( |
| 119 | index = as.character(1:length(footnote_contents$number)), |
| 120 | footnote = footnote_contents$number |
| 121 | ) |
| 122 | } |
| 123 | if (!is.null(footnote_contents$alphabet)) { |
| 124 | footnote_contents$alphabet <- data.frame( |
| 125 | index = letters[1:length(footnote_contents$alphabet)], |
| 126 | footnote = footnote_contents$alphabet |
| 127 | ) |
| 128 | } |
| 129 | if (!is.null(footnote_contents$symbol)) { |
| 130 | footnote_contents$symbol <- data.frame( |
| 131 | index = symbol_index[1:length(footnote_contents$symbol)], |
| 132 | footnote = footnote_contents$symbol |
| 133 | ) |
| 134 | } |
| 135 | |
| 136 | out <- list() |
| 137 | out$contents <- footnote_contents |
| 138 | out$titles <- footnote_titles |
| 139 | return(out) |
| 140 | } |
| 141 | |
| 142 | # HTML |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 143 | footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 144 | kable_attrs <- attributes(kable_input) |
| 145 | kable_xml <- read_kable_as_xml(kable_input) |
| 146 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 147 | new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 148 | xml_add_child(kable_xml, new_html_footnote) |
| 149 | |
| 150 | out <- as_kable_xml(kable_xml) |
| 151 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 152 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 153 | return(out) |
| 154 | } |
| 155 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 156 | html_tfoot_maker <- function(footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 157 | footnote_types <- names(footnote_table$contents) |
| 158 | footnote_text <- c() |
| 159 | for (i in footnote_types) { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 160 | footnote_text <- c(footnote_text, html_tfoot_maker_( |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 161 | footnote_table$contents[[i]], footnote_table$titles[[i]], i, |
| 162 | footnote_as_chunk)) |
| 163 | } |
| 164 | footnote_text <- paste0( |
| 165 | "<tfoot>", paste0(footnote_text, collapse = ""), "</tfoot>" |
| 166 | ) |
| 167 | footnote_node <- read_html(footnote_text, options = c("RECOVER", "NOERROR")) |
| 168 | return(xml_child(xml_child(footnote_node, 1), 1)) |
| 169 | } |
| 170 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 171 | html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 172 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 173 | paste0('<sup>', x[1], '</sup> ', x[2]) |
| 174 | }) |
| 175 | if (ft_title != "") { |
| 176 | title_text <- paste0('<strong>', ft_title, '</strong>') |
| 177 | footnote_text <- c(title_text, footnote_text) |
| 178 | } |
| 179 | if (!ft_chunk) { |
| 180 | footnote_text <- paste0( |
| 181 | '<tr><td style="padding: 0; border: 0;" colspan="100%">', |
| 182 | footnote_text, '</td></tr>' |
| 183 | ) |
| 184 | } else { |
| 185 | footnote_text <- paste0( |
| 186 | '<tr><td style="padding: 0; border: 0;" colspan="100%">', |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 187 | paste0(footnote_text, collapse = " "), |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 188 | '</td></tr>' |
| 189 | ) |
| 190 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 191 | return(footnote_text) |
| 192 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 193 | |
| 194 | # LaTeX |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 195 | footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk, |
| 196 | threeparttable) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 197 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 198 | out <- enc2utf8(as.character(kable_input)) |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 199 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 200 | footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk, |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 201 | table_info$ncol, threeparttable) |
| 202 | if (threeparttable) { |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame^] | 203 | if (table_info$tabular %in% c("longtable", "longtabu") ) { |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 204 | out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"), |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 205 | paste0("\\\\begin{ThreePartTable}\n\\\\begin{TableNotes}", |
| 206 | ifelse(footnote_as_chunk, "[para]", ""), |
| 207 | "\n\\\\small\n", footnote_text, |
| 208 | "\n\\\\end{TableNotes}\n\\\\begin{", |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 209 | table_info$tabular, "}"), |
| 210 | out) |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame^] | 211 | out <- sub(paste0("\\\\end\\{",table_info$tabular, "\\}"), |
| 212 | paste0("\\\\end{", table_info$tabular, |
| 213 | "}\n\\\\end{ThreePartTable}"), |
| 214 | out) |
| 215 | if (table_info$booktabs) { |
| 216 | out <- sub("\\\\bottomrule", "\\\\bottomrule\n\\\\insertTableNotes", out) |
| 217 | } else { |
| 218 | out <- sub("\\\\hline\n\\\\end\\{longtable\\}", |
| 219 | "\\\\hline\n\\\\insertTableNotes\n\\\\end\\{longtable\\}", |
| 220 | out) |
| 221 | } |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 222 | } else { |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame^] | 223 | if (table_info$tabular == "tabu") { |
| 224 | stop("Please use `longtable = T` in your kable function. ", |
| 225 | "Full width threeparttable only works with longtable.") |
| 226 | } |
| 227 | out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"), |
| 228 | paste0("\\\\begin{threeparttable}\n\\\\begin{", |
| 229 | table_info$tabular, "}"), |
| 230 | out) |
| 231 | out <- sub(table_info$end_tabular, |
| 232 | paste0("\\\\end{", table_info$tabular, |
| 233 | "}\n\\\\begin{tablenotes}", |
| 234 | ifelse(footnote_as_chunk, "[para]", ""), |
| 235 | "\n\\\\small\n", footnote_text, |
| 236 | "\n\\\\end{tablenotes}\n\\\\end{threeparttable}"), |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 237 | out) |
| 238 | } |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 239 | } else { |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame^] | 240 | if (table_info$booktabs) { |
| 241 | out <- sub("\\\\bottomrule", |
| 242 | paste0("\\\\bottomrule\n", footnote_text), out) |
| 243 | } else { |
| 244 | out <- sub(table_info$end_tabular, |
| 245 | paste0(footnote_text, "\n\\\\end{", table_info$tabular, "}"), |
| 246 | out) |
| 247 | } |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 248 | } |
| 249 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 250 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 251 | attr(out, "kable_meta") <- table_info |
| 252 | return(out) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 253 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 254 | |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 255 | latex_tfoot_maker <- function(footnote_table, footnote_as_chunk, ncol, |
| 256 | threeparttable) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 257 | footnote_types <- names(footnote_table$contents) |
| 258 | footnote_text <- c() |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 259 | if (threeparttable) { |
| 260 | for (i in footnote_types) { |
| 261 | footnote_text <- c(footnote_text, latex_tfoot_maker_tpt_( |
| 262 | footnote_table$contents[[i]], footnote_table$titles[[i]], |
| 263 | footnote_as_chunk, ncol)) |
| 264 | } |
| 265 | } else { |
| 266 | for (i in footnote_types) { |
| 267 | footnote_text <- c(footnote_text, latex_tfoot_maker_( |
| 268 | footnote_table$contents[[i]], footnote_table$titles[[i]], |
| 269 | footnote_as_chunk, ncol)) |
| 270 | } |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 271 | } |
| 272 | footnote_text <- paste0(footnote_text, collapse = "\n") |
| 273 | return(footnote_text) |
| 274 | } |
Hao Zhu | 9f91748 | 2018-01-08 18:09:33 -0500 | [diff] [blame] | 275 | |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 276 | latex_tfoot_maker_ <- function(ft_contents, ft_title, ft_chunk, ncol) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 277 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 278 | if (x[1] == "") { |
| 279 | x[2] |
| 280 | } else { |
| 281 | paste0('\\\\textsuperscript{', x[1], '} ', x[2]) |
| 282 | } |
| 283 | }) |
| 284 | if (ft_title != "") { |
| 285 | title_text <- paste0('\\\\textbf{', ft_title, '} ') |
| 286 | footnote_text <- c(title_text, footnote_text) |
| 287 | } |
| 288 | if (!ft_chunk) { |
| 289 | footnote_text <- paste0( |
| 290 | '\\\\multicolumn{', ncol, '}{l}{', footnote_text, '}\\\\\\\\' |
| 291 | ) |
| 292 | } else { |
| 293 | footnote_text <- paste0( |
| 294 | '\\\\multicolumn{', ncol, '}{l}{', |
| 295 | paste0(footnote_text, collapse = " "), |
| 296 | '}\\\\\\\\' |
| 297 | ) |
| 298 | } |
| 299 | return(footnote_text) |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 300 | } |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 301 | |
| 302 | latex_tfoot_maker_tpt_ <- function(ft_contents, ft_title, ft_chunk, ncol) { |
| 303 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 304 | if (x[1] == "") { |
| 305 | paste0('\\\\item ', x[2]) |
| 306 | } else { |
| 307 | paste0('\\\\item[', x[1], '] ', x[2]) |
| 308 | } |
| 309 | }) |
| 310 | if (ft_title != "") { |
| 311 | title_text <- paste0('\\\\item \\\\textbf{', ft_title, '} ') |
| 312 | footnote_text <- c(title_text, footnote_text) |
| 313 | } |
| 314 | footnote_text <- paste0(footnote_text, collapse = "\n") |
| 315 | # if (!ft_chunk) { |
| 316 | # footnote_text <- paste0(footnote_text, collapse = "\n") |
| 317 | # } else { |
| 318 | # footnote_text <- paste0(footnote_text, collapse = " ") |
| 319 | # } |
| 320 | return(footnote_text) |
| 321 | } |