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. |
| 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 Zhu | b1e2e3d | 2018-01-09 13:31:42 -0500 | [diff] [blame^] | 29 | #' @examples dt <- mtcars[1:5, 1:5] |
Hao Zhu | 593f57e | 2018-01-09 13:30:01 -0500 | [diff] [blame] | 30 | #' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b")) |
| 31 | #' |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 32 | #' @export |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 33 | footnote <- function(kable_input, |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 34 | 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 Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 41 | escape = TRUE, |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 42 | general_title = "Note: ", |
| 43 | number_title = "", |
| 44 | alphabet_title = "", |
| 45 | symbol_title = "" |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 46 | ) { |
| 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 Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 52 | 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 Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 62 | 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 Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 71 | footnote_order <- footnote_order[footnote_order %in% notnull] |
| 72 | footnote_titles <- footnote_titles[footnote_order] |
| 73 | footnote_contents <- footnote_contents[footnote_order] |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 74 | footnote_contents <- lapply(footnote_contents, esca) |
| 75 | if (escape) { |
| 76 | if (kable_format == "html") { |
| 77 | footnote_contents <- lapply(footnote_contents, escape_html) |
| 78 | footnote_titles <- lapply(footnote_titles, escape_html) |
| 79 | } else { |
| 80 | footnote_contents <- lapply(footnote_contents, escape_latex) |
| 81 | footnote_titles <- lapply(footnote_titles, escape_latex) |
| 82 | } |
| 83 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 84 | footnote_table <- footnote_table_maker( |
| 85 | kable_format, footnote_titles, footnote_contents |
| 86 | ) |
| 87 | if (kable_format == "html") { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 88 | return(footnote_html(kable_input, footnote_table, footnote_as_chunk)) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 89 | } |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 90 | if (kable_format == "latex") { |
| 91 | return(footnote_latex(kable_input, footnote_table, footnote_as_chunk)) |
| 92 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | footnote_table_maker <- function(format, footnote_titles, footnote_contents) { |
| 96 | number_index <- read.csv(system.file("symbol_index.csv", |
| 97 | package = "kableExtra")) |
| 98 | if (format == "latex") { |
| 99 | symbol_index <- number_index$symbol.latex |
| 100 | } else { |
| 101 | symbol_index <- number_index$symbol.html |
| 102 | } |
| 103 | contents_length <- sapply(footnote_contents, length) |
| 104 | |
| 105 | if (!is.null(footnote_contents$general)) { |
| 106 | footnote_contents$general <- data.frame( |
| 107 | index = "", |
| 108 | footnote = paste(footnote_contents$general, collapse = " ") |
| 109 | ) |
| 110 | } |
| 111 | if (!is.null(footnote_contents$number)) { |
| 112 | footnote_contents$number <- data.frame( |
| 113 | index = as.character(1:length(footnote_contents$number)), |
| 114 | footnote = footnote_contents$number |
| 115 | ) |
| 116 | } |
| 117 | if (!is.null(footnote_contents$alphabet)) { |
| 118 | footnote_contents$alphabet <- data.frame( |
| 119 | index = letters[1:length(footnote_contents$alphabet)], |
| 120 | footnote = footnote_contents$alphabet |
| 121 | ) |
| 122 | } |
| 123 | if (!is.null(footnote_contents$symbol)) { |
| 124 | footnote_contents$symbol <- data.frame( |
| 125 | index = symbol_index[1:length(footnote_contents$symbol)], |
| 126 | footnote = footnote_contents$symbol |
| 127 | ) |
| 128 | } |
| 129 | |
| 130 | out <- list() |
| 131 | out$contents <- footnote_contents |
| 132 | out$titles <- footnote_titles |
| 133 | return(out) |
| 134 | } |
| 135 | |
| 136 | # HTML |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 137 | footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 138 | kable_attrs <- attributes(kable_input) |
| 139 | kable_xml <- read_kable_as_xml(kable_input) |
| 140 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 141 | new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 142 | xml_add_child(kable_xml, new_html_footnote) |
| 143 | |
| 144 | out <- as_kable_xml(kable_xml) |
| 145 | attributes(out) <- kable_attrs |
| 146 | return(out) |
| 147 | } |
| 148 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 149 | html_tfoot_maker <- function(footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 150 | footnote_types <- names(footnote_table$contents) |
| 151 | footnote_text <- c() |
| 152 | for (i in footnote_types) { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 153 | footnote_text <- c(footnote_text, html_tfoot_maker_( |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 154 | footnote_table$contents[[i]], footnote_table$titles[[i]], i, |
| 155 | footnote_as_chunk)) |
| 156 | } |
| 157 | footnote_text <- paste0( |
| 158 | "<tfoot>", paste0(footnote_text, collapse = ""), "</tfoot>" |
| 159 | ) |
| 160 | footnote_node <- read_html(footnote_text, options = c("RECOVER", "NOERROR")) |
| 161 | return(xml_child(xml_child(footnote_node, 1), 1)) |
| 162 | } |
| 163 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 164 | html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 165 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 166 | paste0('<sup>', x[1], '</sup> ', x[2]) |
| 167 | }) |
| 168 | if (ft_title != "") { |
| 169 | title_text <- paste0('<strong>', ft_title, '</strong>') |
| 170 | footnote_text <- c(title_text, footnote_text) |
| 171 | } |
| 172 | if (!ft_chunk) { |
| 173 | footnote_text <- paste0( |
| 174 | '<tr><td style="padding: 0; border: 0;" colspan="100%">', |
| 175 | footnote_text, '</td></tr>' |
| 176 | ) |
| 177 | } else { |
| 178 | footnote_text <- paste0( |
| 179 | '<tr><td style="padding: 0; border: 0;" colspan="100%">', |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 180 | paste0(footnote_text, collapse = " "), |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 181 | '</td></tr>' |
| 182 | ) |
| 183 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 184 | return(footnote_text) |
| 185 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 186 | |
| 187 | # LaTeX |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 188 | footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk) { |
| 189 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 190 | out <- enc2utf8(as.character(kable_input)) |
| 191 | if (table_info$tabular == "longtable") { |
| 192 | return() |
| 193 | } |
| 194 | footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk, |
| 195 | table_info$ncol) |
| 196 | out <- sub(table_info$end_tabular, |
| 197 | paste0(footnote_text, "\n\\\\end{", table_info$tabular, "}"), |
| 198 | out) |
| 199 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 200 | attr(out, "kable_meta") <- table_info |
| 201 | return(out) |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 202 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 203 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 204 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 205 | latex_tfoot_maker <- function(footnote_table, footnote_as_chunk, ncol) { |
| 206 | footnote_types <- names(footnote_table$contents) |
| 207 | footnote_text <- c() |
| 208 | for (i in footnote_types) { |
| 209 | footnote_text <- c(footnote_text, latex_tfoot_maker_( |
| 210 | footnote_table$contents[[i]], footnote_table$titles[[i]], i, |
| 211 | footnote_as_chunk, ncol)) |
| 212 | } |
| 213 | footnote_text <- paste0(footnote_text, collapse = "\n") |
| 214 | return(footnote_text) |
| 215 | } |
Hao Zhu | 9f91748 | 2018-01-08 18:09:33 -0500 | [diff] [blame] | 216 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 217 | latex_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk, ncol) { |
| 218 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 219 | if (x[1] == "") { |
| 220 | x[2] |
| 221 | } else { |
| 222 | paste0('\\\\textsuperscript{', x[1], '} ', x[2]) |
| 223 | } |
| 224 | }) |
| 225 | if (ft_title != "") { |
| 226 | title_text <- paste0('\\\\textbf{', ft_title, '} ') |
| 227 | footnote_text <- c(title_text, footnote_text) |
| 228 | } |
| 229 | if (!ft_chunk) { |
| 230 | footnote_text <- paste0( |
| 231 | '\\\\multicolumn{', ncol, '}{l}{', footnote_text, '}\\\\\\\\' |
| 232 | ) |
| 233 | } else { |
| 234 | footnote_text <- paste0( |
| 235 | '\\\\multicolumn{', ncol, '}{l}{', |
| 236 | paste0(footnote_text, collapse = " "), |
| 237 | '}\\\\\\\\' |
| 238 | ) |
| 239 | } |
| 240 | return(footnote_text) |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 241 | } |