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 | 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 Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 83 | footnote_table <- footnote_table_maker( |
| 84 | kable_format, footnote_titles, footnote_contents |
| 85 | ) |
| 86 | if (kable_format == "html") { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 87 | return(footnote_html(kable_input, footnote_table, footnote_as_chunk)) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 88 | } |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 89 | if (kable_format == "latex") { |
| 90 | return(footnote_latex(kable_input, footnote_table, footnote_as_chunk)) |
| 91 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | footnote_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 Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 102 | |
| 103 | if (!is.null(footnote_contents$general)) { |
| 104 | footnote_contents$general <- data.frame( |
| 105 | index = "", |
Hao Zhu | bab692d | 2018-01-09 17:49:55 -0500 | [diff] [blame^] | 106 | footnote = footnote_contents$general |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 107 | ) |
| 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 Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 135 | footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 136 | kable_attrs <- attributes(kable_input) |
| 137 | kable_xml <- read_kable_as_xml(kable_input) |
| 138 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 139 | new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 140 | 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 Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 147 | html_tfoot_maker <- function(footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 148 | footnote_types <- names(footnote_table$contents) |
| 149 | footnote_text <- c() |
| 150 | for (i in footnote_types) { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 151 | footnote_text <- c(footnote_text, html_tfoot_maker_( |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 152 | 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 Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 162 | html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 163 | 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 Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 178 | paste0(footnote_text, collapse = " "), |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 179 | '</td></tr>' |
| 180 | ) |
| 181 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 182 | return(footnote_text) |
| 183 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 184 | |
| 185 | # LaTeX |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 186 | footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk) { |
| 187 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 188 | out <- enc2utf8(as.character(kable_input)) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 189 | 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 Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 197 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 198 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 199 | latex_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 Zhu | 9f91748 | 2018-01-08 18:09:33 -0500 | [diff] [blame] | 210 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 211 | latex_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 Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 235 | } |