Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 1 | #' HTML table attributes |
| 2 | #' |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 3 | #' @description This function provides a cleaner approach to modify the style |
| 4 | #' of HTML tables other than using the `table.attr` option in `knitr::kable()`. |
| 5 | #' Currenly, it assumes the HTML document has boot |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 6 | #' |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 7 | #' @param kable_input Output of `knitr::kable()` with `format` specified |
| 8 | #' @param bootstrap_options A character vector for bootstrap table options. |
| 9 | #' Please see package documentation site or visit the w3schools' |
| 10 | #' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page} |
| 11 | #' for more information. Possible options include `basic`, `striped`, |
| 12 | #' `bordered`, `hover`, `condensed` and `responsive`. |
| 13 | #' @param latex_options A character vector for LaTeX table options. Please see |
| 14 | #' package documentation site for more information. Possible options include |
| 15 | #' `basic`, `striped`, `hold_position`, `scale_down`. `striped` will add |
| 16 | #' alternative row colors to the table. It will imports `LaTeX` package `xcolor` |
| 17 | #' if enabled. `hold_position` will "hold" the floating table to the exact |
| 18 | #' position. It is useful when the `LaTeX` table is contained in a `table` |
| 19 | #' environment after you specified captions in `kable()`. It will force the |
| 20 | #' table to stay in the position where it was created in the document. |
| 21 | #' `scale_down` is useful for super wide table. It will automatically adjust |
| 22 | #' the table to page width. |
Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame] | 23 | #' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 24 | #' table should have 100\% width. Since HTML and pdf have different flavors on |
| 25 | #' the preferable format for `full_width`. If not specified, a HTML table will |
| 26 | #' have full width by default but this option will be set to `FALSE` for a |
| 27 | #' LaTeX table |
| 28 | #' @param position A character string determining how to position the table |
| 29 | #' on a page. Possible values include `left`, `center`, `right`, `float_left` |
| 30 | #' and `float_right`. Please see the package doc site for demonstrations. For |
| 31 | #' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be |
| 32 | #' imported. |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 33 | #' @param font_size A numeric input for table font size |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 34 | #' |
| 35 | #' @export |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 36 | kable_styling <- function(kable_input, |
| 37 | bootstrap_options = "basic", |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 38 | latex_options = "basic", |
| 39 | full_width = NULL, |
| 40 | position = c("center", "left", "right", |
| 41 | "float_left", "float_right"), |
| 42 | font_size = NULL) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 43 | kable_format <- attr(kable_input, "format") |
| 44 | if (!kable_format %in% c("html", "latex")) { |
| 45 | stop("Please specify output format in your kable function. Currently ", |
| 46 | "generic markdown table using pandoc is not supported.") |
| 47 | } |
| 48 | if (kable_format == "html") { |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 49 | if (is.null(full_width)) full_width <- T |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 50 | return(htmlTable_styling(kable_input, |
| 51 | bootstrap_options = bootstrap_options, |
| 52 | full_width = full_width, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 53 | position = position, |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 54 | font_size = font_size)) |
| 55 | } |
| 56 | if (kable_format == "latex") { |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 57 | if (is.null(full_width)) full_width <- F |
| 58 | return(pdfTable_styling(kable_input, |
| 59 | latex_options = latex_options, |
| 60 | full_width = full_width, |
| 61 | position = position, |
| 62 | font_size = font_size)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | # htmlTable Styling ------------ |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 67 | htmlTable_styling <- function(kable_input, |
| 68 | bootstrap_options = "basic", |
| 69 | full_width = T, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 70 | position = c("center", "left", "right", |
| 71 | "float_left", "float_right"), |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 72 | font_size = NULL) { |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 73 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 74 | kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT")) |
| 75 | |
| 76 | # Modify class |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 77 | bootstrap_options <- match.arg( |
| 78 | bootstrap_options, |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 79 | c("basic", "striped", "bordered", "hover", "condensed", "responsive"), |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 80 | several.ok = T |
| 81 | ) |
| 82 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 83 | kable_xml_class <- NULL |
| 84 | if (xml_has_attr(kable_xml, "class")) { |
| 85 | kable_xml_class <- xml_attr(kable_xml, "class") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 86 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 87 | if (length(bootstrap_options) == 1 && bootstrap_options == "basic") { |
| 88 | bootstrap_options <- "table" |
| 89 | } else { |
| 90 | bootstrap_options <- bootstrap_options[bootstrap_options != "basic"] |
| 91 | bootstrap_options <- paste0("table-", bootstrap_options) |
| 92 | bootstrap_options <- c("table", bootstrap_options) |
| 93 | } |
| 94 | xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options), |
| 95 | collapse = " ") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 96 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 97 | # Modify style |
| 98 | kable_xml_style <- NULL |
| 99 | if (xml_has_attr(kable_xml, "style")) { |
| 100 | kable_xml_style <- xml_attr(kable_xml, "style") |
| 101 | } |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 102 | if (!is.null(font_size)) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 103 | kable_xml_style <- c(kable_xml_style, |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 104 | paste0("font-size: ", font_size, "px;")) |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 105 | } |
| 106 | if (!full_width) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 107 | kable_xml_style <- c(kable_xml_style, "width: auto !important;") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 108 | } |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 109 | |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 110 | position <- match.arg(position) |
| 111 | position_style <- switch( |
| 112 | position, |
| 113 | center = "margin-left: auto; margin-right: auto;", |
| 114 | left = "text-align: right;", |
| 115 | right = "margin-right: 0; margin-left: auto", |
| 116 | float_left = "float: left; margin-right: 10px;", |
| 117 | float_right = "float: right; margin-left: 10px;" |
| 118 | ) |
| 119 | kable_xml_style <- c(kable_xml_style, position_style) |
| 120 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 121 | if (length(kable_xml_style) != 0) { |
| 122 | xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 123 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 124 | return(structure(as.character(kable_xml), format = "html", |
| 125 | class = "knitr_kable")) |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 126 | } |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 127 | |
| 128 | # LaTeX table style |
| 129 | pdfTable_styling <- function(kable_input, |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 130 | latex_options = "basic", |
| 131 | full_width = F, |
| 132 | position = c("center", "left", "right", |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 133 | "float_left", "float_right"), |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 134 | font_size = NULL) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 135 | |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 136 | latex_options <- match.arg( |
| 137 | latex_options, |
| 138 | c("basic", "striped", "hold_position", "scale_down"), |
| 139 | several.ok = T |
| 140 | ) |
| 141 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 142 | out <- NULL |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 143 | out <- as.character(kable_input) |
| 144 | table_info <- magic_mirror(kable_input) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 145 | table_info$valign2 <- sub("\\[", "\\\\[", table_info$valign) |
| 146 | table_info$valign2 <- sub("\\]", "\\\\]", table_info$valign2) |
| 147 | table_info$valign3 <- sub("\\[", "", table_info$valign) |
| 148 | table_info$valign3 <- sub("\\]", "", table_info$valign3) |
| 149 | table_info$begin_tabular <- paste0("\\\\begin\\{", table_info$tabular, "\\}", |
| 150 | table_info$valign2) |
| 151 | table_info$end_tabular <- paste0("\\\\end\\{", table_info$tabular, "\\}") |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 152 | |
| 153 | |
| 154 | if ("striped" %in% latex_options) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 155 | out <- styling_latex_striped(out) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | # hold_position is only meaningful in a table environment |
| 159 | if ("hold_position" %in% latex_options & table_info$table_env) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 160 | out <- styling_latex_hold_position(out) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 163 | if ("scale_down" %in% latex_options) { |
| 164 | out <- styling_latex_scale_down(out, table_info) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (full_width) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 168 | out <- styling_latex_full_width(out, table_info) |
| 169 | } |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 170 | |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 171 | if (!is.null(font_size)) { |
| 172 | out <- styling_latex_font_size(out, table_info, font_size) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | position <- match.arg(position) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 176 | out <- styling_latex_position(out, table_info, position, latex_options) |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 177 | |
| 178 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 179 | return(out) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 180 | } |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 181 | |
| 182 | styling_latex_striped <- function(x) { |
| 183 | usepackage_latex("xcolor", "table") |
| 184 | paste0( |
| 185 | # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document |
| 186 | "\\rowcolors{2}{gray!6}{white}\n", x, "\n\\rowcolors{2}{white}{white}") |
| 187 | } |
| 188 | |
| 189 | styling_latex_hold_position <- function(x) { |
| 190 | sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x) |
| 191 | } |
| 192 | |
| 193 | styling_latex_scale_down <- function(x, table_info) { |
| 194 | # You cannot put longtable in a resizebox |
| 195 | # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised |
| 196 | if (table_info$tabular == "longtable") { |
| 197 | warning("Longtable cannot be resized.") |
| 198 | return(x) |
| 199 | } |
| 200 | x <- sub(table_info$begin_tabular, |
| 201 | paste0("\\\\resizebox\\{\\\\textwidth\\}\\{\\!\\}\\{", |
| 202 | table_info$begin_tabular), |
| 203 | x) |
| 204 | sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x) |
| 205 | } |
| 206 | |
| 207 | styling_latex_full_width <- function(x, table_info) { |
| 208 | size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar) |
| 209 | col_max_length <- apply(size_matrix, 1, max) + 4 |
| 210 | col_ratio <- round(col_max_length / sum(col_max_length), 2) |
| 211 | col_align <- paste0("p{\\\\dimexpr", col_ratio, |
| 212 | "\\\\linewidth-2\\\\tabcolsep}") |
| 213 | col_align <- paste0("{", paste(col_align, collapse = ""), "}") |
| 214 | x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"), |
| 215 | table_info$begin_tabular, x) |
| 216 | sub(table_info$begin_tabular, |
| 217 | paste0(table_info$begin_tabular, col_align), x) |
| 218 | } |
| 219 | |
| 220 | styling_latex_position <- function(x, table_info, position, latex_options) { |
| 221 | hold_position <- "hold_position" %in% latex_options |
| 222 | switch( |
| 223 | position, |
| 224 | center = styling_latex_position_center(x, table_info, hold_position), |
| 225 | left = styling_latex_position_left(x, table_info), |
| 226 | right = styling_latex_position_right(x, table_info, hold_position), |
| 227 | float_left = styling_latex_position_float(x, table_info, "l"), |
| 228 | float_right = styling_latex_position_float(x, table_info, "r") |
| 229 | ) |
| 230 | } |
| 231 | |
| 232 | styling_latex_position_center <- function(x, table_info, hold_position) { |
| 233 | if (!table_info$table_env & table_info$tabular == "tabular") { |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 234 | return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}")) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 235 | } |
| 236 | return(x) |
| 237 | } |
| 238 | |
| 239 | styling_latex_position_left <- function(x, table_info) { |
| 240 | if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x)) |
| 241 | longtable_option <- "\\[l\\]" |
| 242 | sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2), |
| 243 | paste0("\\\\begin\\{longtable\\}", longtable_option), x) |
| 244 | } |
| 245 | |
| 246 | styling_latex_position_right <- function(x, table_info, hold_position) { |
| 247 | warning("Position = right is only supported for longtable in LaTeX. ", |
| 248 | "Setting back to center...") |
| 249 | styling_latex_position_center(x, table_info, hold_position) |
| 250 | } |
| 251 | |
| 252 | styling_latex_position_float <- function(x, table_info, option) { |
| 253 | if (table_info$tabular == "longtable") { |
| 254 | warning("wraptable is not supported for longtable.") |
| 255 | if (option == "l") return(styling_latex_position_left(x, table_info)) |
| 256 | if (option == "r") return(styling_latex_position_right(x, table_info, F)) |
| 257 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 258 | usepackage_latex("wrapfig") |
| 259 | size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar) |
| 260 | col_max_length <- apply(size_matrix, 1, max) + 4 |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 261 | if (table_info$table_env) { |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 262 | option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option) |
| 263 | option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}") |
| 264 | x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x) |
| 265 | x <- sub("\\\\begin\\{table\\}", option, x) |
| 266 | x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x) |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 267 | } else { |
| 268 | option <- sprintf("\\begin{wraptable}{%s}", option) |
| 269 | option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}") |
| 270 | x <- paste0(option, x, "\\end{wraptable}") |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 271 | } |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 272 | return(x) |
Hao Zhu | a3fc0c4 | 2017-02-27 12:04:59 -0500 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | styling_latex_font_size <- function(x, table_info, font_size) { |
| 276 | row_height <- font_size + 2 |
| 277 | if (table_info$tabular == "tabular" & table_info$table_env) { |
| 278 | return(sub(table_info$begin_tabular, |
| 279 | paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height, |
| 280 | "\\}\\\\selectfont\n", table_info$begin_tabular), |
| 281 | x)) |
| 282 | } |
| 283 | # For longtable and tabular without table environment. Simple wrap around |
| 284 | # fontsize is good enough |
| 285 | return(paste0( |
| 286 | "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x, |
| 287 | "\\endgroup" |
| 288 | )) |
| 289 | } |