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. |
Augusto Magalhães | f17fa46 | 2019-03-05 12:55:21 +0900 | [diff] [blame] | 27 | #' @param fixed_small_size T/F When you want to keep the footnote small after |
| 28 | #' specifying large font size with the kable_styling() (e.g. ideal font for headers |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 29 | #' and table content with small font in footnotes). |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 30 | #' @param general_title Section header for general footnotes. Default is |
| 31 | #' "Note: ". |
| 32 | #' @param number_title Section header for number footnotes. Default is "". |
| 33 | #' @param alphabet_title Section header for alphabet footnotes. Default is "". |
| 34 | #' @param symbol_title Section header for symbol footnotes. Default is "". |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 35 | #' @param title_format Choose from "italic"(default), "bold" and "underline". |
| 36 | #' Multiple options are possible. |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 37 | #' @param symbol_manual User can manually supply a vector of either html or |
| 38 | #' latex symbols. For example, `symbol_manual = c('*', '\\\\dag', '\\\\ddag')`.` |
Hao Zhu | 465fc65 | 2018-05-20 20:02:36 -0400 | [diff] [blame] | 39 | #' |
Hao Zhu | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame^] | 40 | #' @examples |
| 41 | #' \dontrun{ |
| 42 | #' dt <- mtcars[1:5, 1:5] |
Hao Zhu | 593f57e | 2018-01-09 13:30:01 -0500 | [diff] [blame] | 43 | #' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b")) |
Hao Zhu | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame^] | 44 | #' } |
Hao Zhu | 593f57e | 2018-01-09 13:30:01 -0500 | [diff] [blame] | 45 | #' |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 46 | #' @export |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 47 | footnote <- function(kable_input, |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 48 | general = NULL, |
| 49 | number = NULL, |
| 50 | alphabet = NULL, |
| 51 | symbol = NULL, |
| 52 | footnote_order = c("general", "number", |
| 53 | "alphabet", "symbol"), |
| 54 | footnote_as_chunk = FALSE, |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 55 | escape = TRUE, |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 56 | threeparttable = FALSE, |
Augusto Magalhães | f17fa46 | 2019-03-05 12:55:21 +0900 | [diff] [blame] | 57 | fixed_small_size = FALSE, |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 58 | general_title = "Note: ", |
| 59 | number_title = "", |
| 60 | alphabet_title = "", |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 61 | symbol_title = "", |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 62 | title_format = "italic", |
| 63 | symbol_manual = NULL |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 64 | ) { |
| 65 | kable_format <- attr(kable_input, "format") |
| 66 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 67 | warning("Please specify format in kable. kableExtra can customize either ", |
| 68 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 69 | "for details.") |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 70 | return(kable_input) |
| 71 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 72 | if (length(alphabet) > 26) { |
| 73 | alphabet <- alphabet[1:26] |
| 74 | warning("Please don't use more than 26 footnotes in table_footnote ", |
| 75 | "alphabet. Use number instead.") |
| 76 | } |
| 77 | if (length(symbol) > 20) { |
| 78 | symbol <- symbol[1:20] |
| 79 | warning("Please don't use more than 20 footnotes in table_footnote ", |
| 80 | "symbol. Use number instead.") |
| 81 | } |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 82 | footnote_titles <- list( |
| 83 | general = general_title, number = number_title, |
| 84 | alphabet = alphabet_title, symbol = symbol_title |
| 85 | ) |
| 86 | footnote_contents <- list( |
| 87 | general = general, number = number, alphabet = alphabet, symbol = symbol |
| 88 | ) |
| 89 | notnull <- names(footnote_contents)[!sapply(footnote_contents, is.null)] |
| 90 | if (length(notnull) == 0) {return(kable_input)} |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 91 | footnote_order <- footnote_order[footnote_order %in% notnull] |
| 92 | footnote_titles <- footnote_titles[footnote_order] |
| 93 | footnote_contents <- footnote_contents[footnote_order] |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 94 | if (escape) { |
| 95 | if (kable_format == "html") { |
| 96 | footnote_contents <- lapply(footnote_contents, escape_html) |
| 97 | footnote_titles <- lapply(footnote_titles, escape_html) |
| 98 | } else { |
Hao Zhu | d463087 | 2018-03-26 11:26:36 -0400 | [diff] [blame] | 99 | footnote_contents <- lapply(footnote_contents, escape_latex2) |
Hao Zhu | 1aff734 | 2018-04-02 18:33:15 -0400 | [diff] [blame] | 100 | footnote_contents <- lapply(footnote_contents, linebreak) |
Hao Zhu | d463087 | 2018-03-26 11:26:36 -0400 | [diff] [blame] | 101 | footnote_titles <- lapply(footnote_titles, escape_latex2) |
Hao Zhu | 1aff734 | 2018-04-02 18:33:15 -0400 | [diff] [blame] | 102 | footnote_titles <- lapply(footnote_titles, linebreak) |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 103 | } |
| 104 | } |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 105 | title_format <- match.arg(title_format, c("italic", "bold", "underline"), |
| 106 | several.ok = TRUE) |
| 107 | footnote_titles <- lapply(footnote_titles, footnote_title_format, |
| 108 | kable_format, title_format) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 109 | footnote_table <- footnote_table_maker( |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 110 | kable_format, footnote_titles, footnote_contents, symbol_manual |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 111 | ) |
| 112 | if (kable_format == "html") { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 113 | return(footnote_html(kable_input, footnote_table, footnote_as_chunk)) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 114 | } |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 115 | if (kable_format == "latex") { |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 116 | return(footnote_latex(kable_input, footnote_table, footnote_as_chunk, |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 117 | threeparttable, fixed_small_size)) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 118 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 121 | footnote_title_format <- function(x, format, title_format) { |
| 122 | if (x == "") return(x) |
| 123 | if (format == "html") { |
| 124 | title_style <- "" |
| 125 | if ("italic" %in% title_format) { |
| 126 | title_style <- paste0(title_style, "font-style: italic;") |
| 127 | } |
| 128 | if ("bold" %in% title_format) { |
| 129 | title_style <- paste0(title_style, "font-weight: bold;") |
| 130 | } |
| 131 | if ("underline" %in% title_format) { |
| 132 | title_style <- paste0(title_style, "text-decoration: underline;") |
| 133 | } |
| 134 | return(paste0( |
| 135 | '<span style="', title_style, '">', x, '</span>' |
| 136 | )) |
| 137 | } else { |
| 138 | if ("italic" %in% title_format) { |
| 139 | x <- paste0("\\\\textit\\{", x, "\\}") |
| 140 | } |
| 141 | if ("bold" %in% title_format) { |
| 142 | x <- paste0("\\\\textbf\\{", x, "\\}") |
| 143 | } |
| 144 | if ("underline" %in% title_format) { |
| 145 | x <- paste0("\\\\underline\\{", x, "\\}") |
| 146 | } |
| 147 | return(x) |
| 148 | } |
| 149 | } |
| 150 | |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 151 | footnote_table_maker <- function(format, footnote_titles, footnote_contents, |
| 152 | symbol_manual) { |
| 153 | if (is.null(symbol_manual)) { |
| 154 | number_index <- read.csv(system.file("symbol_index.csv", |
| 155 | package = "kableExtra")) |
| 156 | if (format == "latex") { |
| 157 | symbol_index <- number_index$symbol.latex |
| 158 | } else { |
| 159 | symbol_index <- number_index$symbol.html |
| 160 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 161 | } else { |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 162 | symbol_index <- symbol_manual |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 163 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 164 | |
Hao Zhu | 149faed | 2018-10-23 16:36:22 -0400 | [diff] [blame] | 165 | |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 166 | if (!is.null(footnote_contents$general)) { |
| 167 | footnote_contents$general <- data.frame( |
| 168 | index = "", |
Hao Zhu | bab692d | 2018-01-09 17:49:55 -0500 | [diff] [blame] | 169 | footnote = footnote_contents$general |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 170 | ) |
| 171 | } |
| 172 | if (!is.null(footnote_contents$number)) { |
| 173 | footnote_contents$number <- data.frame( |
| 174 | index = as.character(1:length(footnote_contents$number)), |
| 175 | footnote = footnote_contents$number |
| 176 | ) |
| 177 | } |
| 178 | if (!is.null(footnote_contents$alphabet)) { |
| 179 | footnote_contents$alphabet <- data.frame( |
| 180 | index = letters[1:length(footnote_contents$alphabet)], |
| 181 | footnote = footnote_contents$alphabet |
| 182 | ) |
| 183 | } |
| 184 | if (!is.null(footnote_contents$symbol)) { |
| 185 | footnote_contents$symbol <- data.frame( |
| 186 | index = symbol_index[1:length(footnote_contents$symbol)], |
| 187 | footnote = footnote_contents$symbol |
| 188 | ) |
| 189 | } |
| 190 | |
| 191 | out <- list() |
| 192 | out$contents <- footnote_contents |
| 193 | out$titles <- footnote_titles |
| 194 | return(out) |
| 195 | } |
| 196 | |
| 197 | # HTML |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 198 | footnote_html <- function(kable_input, footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 199 | kable_attrs <- attributes(kable_input) |
| 200 | kable_xml <- read_kable_as_xml(kable_input) |
| 201 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 202 | new_html_footnote <- html_tfoot_maker(footnote_table, footnote_as_chunk) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 203 | xml_add_child(kable_xml, new_html_footnote) |
| 204 | |
| 205 | out <- as_kable_xml(kable_xml) |
| 206 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 207 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 208 | return(out) |
| 209 | } |
| 210 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 211 | html_tfoot_maker <- function(footnote_table, footnote_as_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 212 | footnote_types <- names(footnote_table$contents) |
| 213 | footnote_text <- c() |
| 214 | for (i in footnote_types) { |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 215 | footnote_text <- c(footnote_text, html_tfoot_maker_( |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 216 | footnote_table$contents[[i]], footnote_table$titles[[i]], i, |
| 217 | footnote_as_chunk)) |
| 218 | } |
| 219 | footnote_text <- paste0( |
| 220 | "<tfoot>", paste0(footnote_text, collapse = ""), "</tfoot>" |
| 221 | ) |
| 222 | footnote_node <- read_html(footnote_text, options = c("RECOVER", "NOERROR")) |
| 223 | return(xml_child(xml_child(footnote_node, 1), 1)) |
| 224 | } |
| 225 | |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 226 | html_tfoot_maker_ <- function(ft_contents, ft_title, ft_type, ft_chunk) { |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 227 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 228 | paste0('<sup>', x[1], '</sup> ', x[2]) |
| 229 | }) |
| 230 | if (ft_title != "") { |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 231 | title_text <- ft_title |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 232 | footnote_text <- c(title_text, footnote_text) |
| 233 | } |
| 234 | if (!ft_chunk) { |
| 235 | footnote_text <- paste0( |
| 236 | '<tr><td style="padding: 0; border: 0;" colspan="100%">', |
| 237 | footnote_text, '</td></tr>' |
| 238 | ) |
| 239 | } else { |
| 240 | footnote_text <- paste0( |
| 241 | '<tr><td style="padding: 0; border: 0;" colspan="100%">', |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 242 | paste0(footnote_text, collapse = " "), |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 243 | '</td></tr>' |
| 244 | ) |
| 245 | } |
Hao Zhu | 8dd65a9 | 2018-01-05 20:40:27 -0500 | [diff] [blame] | 246 | return(footnote_text) |
| 247 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 248 | |
| 249 | # LaTeX |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 250 | footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk, |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 251 | threeparttable, fixed_small_size) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 252 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 253 | out <- solve_enc(kable_input) |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 254 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 255 | footnote_text <- latex_tfoot_maker(footnote_table, footnote_as_chunk, |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 256 | table_info$ncol, threeparttable) |
| 257 | if (threeparttable) { |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 258 | if (table_info$tabular %in% c("longtable", "longtabu") ) { |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 259 | out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"), |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 260 | paste0("\\\\begin{ThreePartTable}\n\\\\begin{TableNotes}", |
| 261 | ifelse(footnote_as_chunk, "[para]", ""), |
Augusto Magalhães | f17fa46 | 2019-03-05 12:55:21 +0900 | [diff] [blame] | 262 | ifelse(fixed_small_size,"\n\\\\small\n","\n"), footnote_text, |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 263 | "\n\\\\end{TableNotes}\n\\\\begin{", |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 264 | table_info$tabular, "}"), |
| 265 | out) |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 266 | out <- sub(paste0("\\\\end\\{",table_info$tabular, "\\}"), |
| 267 | paste0("\\\\end{", table_info$tabular, |
| 268 | "}\n\\\\end{ThreePartTable}"), |
| 269 | out) |
| 270 | if (table_info$booktabs) { |
| 271 | out <- sub("\\\\bottomrule", "\\\\bottomrule\n\\\\insertTableNotes", out) |
| 272 | } else { |
| 273 | out <- sub("\\\\hline\n\\\\end\\{longtable\\}", |
| 274 | "\\\\hline\n\\\\insertTableNotes\n\\\\end\\{longtable\\}", |
| 275 | out) |
| 276 | } |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 277 | } else { |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 278 | if (table_info$tabular == "tabu") { |
| 279 | stop("Please use `longtable = T` in your kable function. ", |
| 280 | "Full width threeparttable only works with longtable.") |
| 281 | } |
| 282 | out <- sub(paste0("\\\\begin\\{", table_info$tabular, "\\}"), |
| 283 | paste0("\\\\begin{threeparttable}\n\\\\begin{", |
| 284 | table_info$tabular, "}"), |
| 285 | out) |
| 286 | out <- sub(table_info$end_tabular, |
| 287 | paste0("\\\\end{", table_info$tabular, |
| 288 | "}\n\\\\begin{tablenotes}", |
| 289 | ifelse(footnote_as_chunk, "[para]", ""), |
Augusto Magalhães | f17fa46 | 2019-03-05 12:55:21 +0900 | [diff] [blame] | 290 | ifelse(fixed_small_size,"\n\\\\small\n","\n"), footnote_text, |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 291 | "\n\\\\end{tablenotes}\n\\\\end{threeparttable}"), |
Hao Zhu | 27c7c85 | 2018-03-26 16:18:23 -0400 | [diff] [blame] | 292 | out) |
| 293 | } |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 294 | } else { |
Hao Zhu | 23bde3a | 2018-03-28 16:00:55 -0400 | [diff] [blame] | 295 | if (table_info$booktabs) { |
| 296 | out <- sub("\\\\bottomrule", |
| 297 | paste0("\\\\bottomrule\n", footnote_text), out) |
| 298 | } else { |
| 299 | out <- sub(table_info$end_tabular, |
| 300 | paste0(footnote_text, "\n\\\\end{", table_info$tabular, "}"), |
| 301 | out) |
| 302 | } |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 305 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 306 | attr(out, "kable_meta") <- table_info |
| 307 | return(out) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 308 | } |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 309 | |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 310 | latex_tfoot_maker <- function(footnote_table, footnote_as_chunk, ncol, |
| 311 | threeparttable) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 312 | footnote_types <- names(footnote_table$contents) |
| 313 | footnote_text <- c() |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 314 | if (threeparttable) { |
| 315 | for (i in footnote_types) { |
| 316 | footnote_text <- c(footnote_text, latex_tfoot_maker_tpt_( |
| 317 | footnote_table$contents[[i]], footnote_table$titles[[i]], |
| 318 | footnote_as_chunk, ncol)) |
| 319 | } |
| 320 | } else { |
| 321 | for (i in footnote_types) { |
| 322 | footnote_text <- c(footnote_text, latex_tfoot_maker_( |
| 323 | footnote_table$contents[[i]], footnote_table$titles[[i]], |
| 324 | footnote_as_chunk, ncol)) |
| 325 | } |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 326 | } |
| 327 | footnote_text <- paste0(footnote_text, collapse = "\n") |
| 328 | return(footnote_text) |
| 329 | } |
Hao Zhu | 9f91748 | 2018-01-08 18:09:33 -0500 | [diff] [blame] | 330 | |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 331 | latex_tfoot_maker_ <- function(ft_contents, ft_title, ft_chunk, ncol) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 332 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 333 | if (x[1] == "") { |
| 334 | x[2] |
| 335 | } else { |
| 336 | paste0('\\\\textsuperscript{', x[1], '} ', x[2]) |
| 337 | } |
| 338 | }) |
| 339 | if (ft_title != "") { |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 340 | title_text <- ft_title |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 341 | footnote_text <- c(title_text, footnote_text) |
| 342 | } |
| 343 | if (!ft_chunk) { |
| 344 | footnote_text <- paste0( |
Cillian Berragan | de662b7 | 2020-03-05 16:17:28 +0000 | [diff] [blame] | 345 | '\\\\multicolumn{', ncol, '}{l}{\\\\rule{0pt}{1em}', footnote_text, '}\\\\\\\\' |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 346 | ) |
| 347 | } else { |
| 348 | footnote_text <- paste0( |
Cillian Berragan | de662b7 | 2020-03-05 16:17:28 +0000 | [diff] [blame] | 349 | '\\\\multicolumn{', ncol, '}{l}{\\\\rule{0pt}{1em}', |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 350 | paste0(footnote_text, collapse = " "), |
| 351 | '}\\\\\\\\' |
| 352 | ) |
| 353 | } |
| 354 | return(footnote_text) |
Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 355 | } |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 356 | |
| 357 | latex_tfoot_maker_tpt_ <- function(ft_contents, ft_title, ft_chunk, ncol) { |
| 358 | footnote_text <- apply(ft_contents, 1, function(x) { |
| 359 | if (x[1] == "") { |
| 360 | paste0('\\\\item ', x[2]) |
| 361 | } else { |
| 362 | paste0('\\\\item[', x[1], '] ', x[2]) |
| 363 | } |
| 364 | }) |
| 365 | if (ft_title != "") { |
Hao Zhu | 25efb13 | 2018-05-20 22:00:56 -0400 | [diff] [blame] | 366 | title_text <- paste0('\\\\item ', ft_title, ' ') |
Hao Zhu | 17814c7 | 2018-01-10 11:32:14 -0500 | [diff] [blame] | 367 | footnote_text <- c(title_text, footnote_text) |
| 368 | } |
| 369 | footnote_text <- paste0(footnote_text, collapse = "\n") |
| 370 | # if (!ft_chunk) { |
| 371 | # footnote_text <- paste0(footnote_text, collapse = "\n") |
| 372 | # } else { |
| 373 | # footnote_text <- paste0(footnote_text, collapse = " ") |
| 374 | # } |
| 375 | return(footnote_text) |
| 376 | } |