blob: f68b60c739967e9a45e7c8ca9de05beb4a832816 [file] [log] [blame]
Hao Zhue10cfd32017-02-21 16:41:14 -05001#' HTML table attributes
2#'
Hao Zhu26234122017-02-22 15:34:33 -05003#' @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 Zhue10cfd32017-02-21 16:41:14 -05006#'
Hao Zhuf7994dd2017-02-27 16:58:42 -05007#' @param kable_input Output of `knitr::kable()` with `format` specified
8#' @param bootstrap_options A character vector for bootstrap table options.
Hao Zhu6a076462017-03-01 12:59:01 -05009#' Please see package vignette or visit the w3schools'
Hao Zhuf7994dd2017-02-27 16:58:42 -050010#' \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
Hao Zhu6a076462017-03-01 12:59:01 -050014#' package vignette for more information. Possible options include
Hao Zhu971d89f2017-06-07 19:22:47 -040015#' `basic`, `striped`, `hold_position`, `scale_down` & `repeat_header`.
16#' `striped` will add alternative row colors to the table. It will imports
17#' `LaTeX` package `xcolor` if enabled. `hold_position` will "hold" the floating
18#' table to the exact position. It is useful when the `LaTeX` table is contained
19#' in a `table` environment after you specified captions in `kable()`. It will
20#' force the table to stay in the position where it was created in the document.
Hao Zhuf7994dd2017-02-27 16:58:42 -050021#' `scale_down` is useful for super wide table. It will automatically adjust
Hao Zhu971d89f2017-06-07 19:22:47 -040022#' the table to page width. `repeat_header` in only meaningful in a longtable
23#' environment. It will let the header row repeat on every page in that long
24#' table.
Hao Zhu59f5fe02017-02-22 11:27:14 -050025#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhuf7994dd2017-02-27 16:58:42 -050026#' table should have 100\% width. Since HTML and pdf have different flavors on
27#' the preferable format for `full_width`. If not specified, a HTML table will
28#' have full width by default but this option will be set to `FALSE` for a
29#' LaTeX table
30#' @param position A character string determining how to position the table
31#' on a page. Possible values include `left`, `center`, `right`, `float_left`
32#' and `float_right`. Please see the package doc site for demonstrations. For
33#' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be
34#' imported.
Hao Zhu94956582017-02-21 18:18:29 -050035#' @param font_size A numeric input for table font size
Hao Zhuca211db2017-07-25 00:06:43 -040036#' @param ... extra options for HTML or LaTeX. For LaTeX, extra options includes
37#' `repeat_header_method` and `repeat_header_text`. `repeat_header_method` can
38#' either be `append`(default) or `replace` while `repeat_header_text` is just a
39#' text string you want to append on or replace the caption.
Hao Zhue10cfd32017-02-21 16:41:14 -050040#'
Hao Zhu78e61222017-05-24 20:53:35 -040041#' @examples x_html <- knitr::kable(head(mtcars), "html")
42#' kable_styling(x_html, "striped", position = "left", font_size = 7)
43#'
44#' x_latex <- knitr::kable(head(mtcars), "latex")
45#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
46#'
Hao Zhue10cfd32017-02-21 16:41:14 -050047#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050048kable_styling <- function(kable_input,
49 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050050 latex_options = "basic",
51 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050052 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040053 font_size = NULL,
54 ...) {
Hao Zhu9b45a182017-02-27 18:17:46 -050055
56 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
57 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
58 }
59 if (length(latex_options) == 1 && latex_options == "basic") {
60 latex_options <- getOption("kable_styling_latex_options", "basic")
61 }
62 if (position == "center") {
63 position <- getOption("kable_styling_position", "center")
64 }
65 position <- match.arg(position,
66 c("center", "left", "right", "float_left", "float_right"))
67 if (is.null(font_size)) {
68 font_size <- getOption("kable_styling_font_size", NULL)
69 }
70
Hao Zhuc1f38412017-02-23 12:13:48 -050071 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050072
Hao Zhuc1f38412017-02-23 12:13:48 -050073 if (!kable_format %in% c("html", "latex")) {
guangchuang yu9c405392017-04-29 12:34:04 +080074 message("Currently generic markdown table using pandoc is not supported.")
75 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050076 }
77 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -050078 if (is.null(full_width)) {
79 full_width <- getOption("kable_styling_full_width", T)
80 }
Hao Zhuc1f38412017-02-23 12:13:48 -050081 return(htmlTable_styling(kable_input,
82 bootstrap_options = bootstrap_options,
83 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050084 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -040085 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -050086 }
87 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -050088 if (is.null(full_width)) {
89 full_width <- getOption("kable_styling_full_width", F)
90 }
Hao Zhuc05e1812017-02-25 01:45:35 -050091 return(pdfTable_styling(kable_input,
92 latex_options = latex_options,
93 full_width = full_width,
94 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -040095 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -050096 }
97}
98
99# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -0500100htmlTable_styling <- function(kable_input,
101 bootstrap_options = "basic",
102 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500103 position = c("center", "left", "right",
104 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -0500105 font_size = NULL) {
Hao Zhu909ea382017-06-12 15:43:47 -0400106 kable_attrs <- attributes(kable_input)
Hao Zhu9bab1532017-07-24 15:08:41 -0400107 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500108
109 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500110 bootstrap_options <- match.arg(
111 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500112 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500113 several.ok = T
114 )
115
Hao Zhu26234122017-02-22 15:34:33 -0500116 kable_xml_class <- NULL
117 if (xml_has_attr(kable_xml, "class")) {
118 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500119 }
Hao Zhu26234122017-02-22 15:34:33 -0500120 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
121 bootstrap_options <- "table"
122 } else {
123 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
124 bootstrap_options <- paste0("table-", bootstrap_options)
125 bootstrap_options <- c("table", bootstrap_options)
126 }
127 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
128 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500129
Hao Zhu26234122017-02-22 15:34:33 -0500130 # Modify style
131 kable_xml_style <- NULL
132 if (xml_has_attr(kable_xml, "style")) {
133 kable_xml_style <- xml_attr(kable_xml, "style")
134 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500135 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500136 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500137 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400138 kable_caption_node <- xml_tpart(kable_xml, "caption")
139 if (!is.null(kable_caption_node)) {
140 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
141 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500142 }
143 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500144 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500145 }
Hao Zhu94956582017-02-21 18:18:29 -0500146
Hao Zhuc05e1812017-02-25 01:45:35 -0500147 position <- match.arg(position)
148 position_style <- switch(
149 position,
150 center = "margin-left: auto; margin-right: auto;",
151 left = "text-align: right;",
152 right = "margin-right: 0; margin-left: auto",
153 float_left = "float: left; margin-right: 10px;",
154 float_right = "float: right; margin-left: 10px;"
155 )
156 kable_xml_style <- c(kable_xml_style, position_style)
157
Hao Zhu26234122017-02-22 15:34:33 -0500158 if (length(kable_xml_style) != 0) {
159 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500160 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500161
Hao Zhuf2dfd142017-07-24 14:43:28 -0400162 out <- as_kable_xml(kable_xml)
Hao Zhu909ea382017-06-12 15:43:47 -0400163 attributes(out) <- kable_attrs
Hao Zhu9b45a182017-02-27 18:17:46 -0500164 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500165}
Hao Zhuc1f38412017-02-23 12:13:48 -0500166
167# LaTeX table style
168pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500169 latex_options = "basic",
170 full_width = F,
171 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500172 "float_left", "float_right"),
Hao Zhua31e97f2017-06-08 14:55:41 -0400173 font_size = NULL,
Hao Zhuca211db2017-07-25 00:06:43 -0400174 repeat_header_text = "\\textit{(continued)}",
175 repeat_header_method = c("append", "replace")) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500176
Hao Zhuc05e1812017-02-25 01:45:35 -0500177 latex_options <- match.arg(
178 latex_options,
Hao Zhu971d89f2017-06-07 19:22:47 -0400179 c("basic", "striped", "hold_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500180 several.ok = T
181 )
182
Hao Zhuca211db2017-07-25 00:06:43 -0400183 repeat_header_method <- match.arg(repeat_header_method)
184
Hao Zhua3fc0c42017-02-27 12:04:59 -0500185 out <- NULL
Hao Zhuc05e1812017-02-25 01:45:35 -0500186 out <- as.character(kable_input)
187 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500188
189 if ("striped" %in% latex_options) {
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400190 out <- styling_latex_striped(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500191 }
192
193 # hold_position is only meaningful in a table environment
194 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500195 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500196 }
197
Hao Zhua3fc0c42017-02-27 12:04:59 -0500198 if ("scale_down" %in% latex_options) {
199 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500200 }
201
Hao Zhu971d89f2017-06-07 19:22:47 -0400202 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhuca211db2017-07-25 00:06:43 -0400203 out <- styling_latex_repeat_header(out, table_info, repeat_header_text,
204 repeat_header_method)
Hao Zhu971d89f2017-06-07 19:22:47 -0400205 }
206
Hao Zhuc05e1812017-02-25 01:45:35 -0500207 if (full_width) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500208 out <- styling_latex_full_width(out, table_info)
209 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500210
Hao Zhua3fc0c42017-02-27 12:04:59 -0500211 if (!is.null(font_size)) {
212 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500213 }
214
215 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500216 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500217
218 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400219 attr(out, "kable_meta") <- table_info
Hao Zhuc05e1812017-02-25 01:45:35 -0500220 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500221}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500222
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400223styling_latex_striped <- function(x, table_info) {
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400224 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
225 if (table_info$tabular == "longtable" & !is.na(table_info$caption)) {
226 row_color <- "\\rowcolors{2}{white}{gray!6}\n"
227 } else {
228 row_color <- "\\rowcolors{2}{gray!6}{white}\n"
229 }
230 return(paste0(row_color, x, "\n\\rowcolors{2}{white}{white}"))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500231}
232
233styling_latex_hold_position <- function(x) {
234 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
235}
236
237styling_latex_scale_down <- function(x, table_info) {
238 # You cannot put longtable in a resizebox
239 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
240 if (table_info$tabular == "longtable") {
241 warning("Longtable cannot be resized.")
242 return(x)
243 }
244 x <- sub(table_info$begin_tabular,
Hao Zhucc21dc72017-05-20 01:15:25 -0400245 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500246 table_info$begin_tabular),
247 x)
248 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
249}
250
Hao Zhuca211db2017-07-25 00:06:43 -0400251styling_latex_repeat_header <- function(x, table_info, repeat_header_text,
252 repeat_header_method) {
Hao Zhua31e97f2017-06-08 14:55:41 -0400253 x <- read_lines(x)
254 if (table_info$booktabs) {
255 header_rows_start <- which(x == "\\toprule")[1]
256 header_rows_end <- which(x == "\\midrule")[1]
257 } else {
258 header_rows_start <- which(x == "\\hline")[1]
259 header_rows_end <- which(x == "\\hline")[2]
260 }
Hao Zhufdec1842017-06-08 17:06:04 -0400261
262 if (is.na(table_info$caption)) {
263 continue_line <- paste0(
264 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
265 "}\\\\"
266 )
267 } else {
Hao Zhuca211db2017-07-25 00:06:43 -0400268 if (repeat_header_method == "append") {
Hao Zhufd2ef9b2017-07-25 13:14:18 -0400269 caption_without_lab <- sub("\\\\label\\{[^\\}]*\\}", "", table_info$caption)
Hao Zhu6f95ea42017-07-25 13:12:12 -0400270 repeat_header_text <- paste(caption_without_lab, repeat_header_text)
Hao Zhuca211db2017-07-25 00:06:43 -0400271 }
272 continue_line <- paste0("\\caption{", repeat_header_text, "}\\\\")
Hao Zhufdec1842017-06-08 17:06:04 -0400273 }
274
Hao Zhua31e97f2017-06-08 14:55:41 -0400275 x <- c(
276 x[1:header_rows_end],
277 "\\endfirsthead",
278 continue_line,
279 x[header_rows_start:header_rows_end],
280 "\\endhead",
281 x[(header_rows_end + 1):length(x)]
282 )
283 x <- paste0(x, collapse = "\n")
284 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400285}
286
Hao Zhua3fc0c42017-02-27 12:04:59 -0500287styling_latex_full_width <- function(x, table_info) {
288 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
289 col_max_length <- apply(size_matrix, 1, max) + 4
290 col_ratio <- round(col_max_length / sum(col_max_length), 2)
291 col_align <- paste0("p{\\\\dimexpr", col_ratio,
Hao Zhu6a076462017-03-01 12:59:01 -0500292 "\\\\columnwidth-2\\\\tabcolsep}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500293 col_align <- paste0("{", paste(col_align, collapse = ""), "}")
294 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
295 table_info$begin_tabular, x)
296 sub(table_info$begin_tabular,
297 paste0(table_info$begin_tabular, col_align), x)
298}
299
300styling_latex_position <- function(x, table_info, position, latex_options) {
301 hold_position <- "hold_position" %in% latex_options
302 switch(
303 position,
304 center = styling_latex_position_center(x, table_info, hold_position),
305 left = styling_latex_position_left(x, table_info),
306 right = styling_latex_position_right(x, table_info, hold_position),
307 float_left = styling_latex_position_float(x, table_info, "l"),
308 float_right = styling_latex_position_float(x, table_info, "r")
309 )
310}
311
312styling_latex_position_center <- function(x, table_info, hold_position) {
313 if (!table_info$table_env & table_info$tabular == "tabular") {
Hao Zhuf7994dd2017-02-27 16:58:42 -0500314 return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}"))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500315 }
316 return(x)
317}
318
319styling_latex_position_left <- function(x, table_info) {
320 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
321 longtable_option <- "\\[l\\]"
322 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
323 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
324}
325
326styling_latex_position_right <- function(x, table_info, hold_position) {
327 warning("Position = right is only supported for longtable in LaTeX. ",
328 "Setting back to center...")
329 styling_latex_position_center(x, table_info, hold_position)
330}
331
332styling_latex_position_float <- function(x, table_info, option) {
333 if (table_info$tabular == "longtable") {
334 warning("wraptable is not supported for longtable.")
335 if (option == "l") return(styling_latex_position_left(x, table_info))
336 if (option == "r") return(styling_latex_position_right(x, table_info, F))
337 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500338 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
339 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500340 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500341 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
342 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
343 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
344 x <- sub("\\\\begin\\{table\\}", option, x)
345 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500346 } else {
347 option <- sprintf("\\begin{wraptable}{%s}", option)
348 option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
349 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500350 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500351 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500352}
353
354styling_latex_font_size <- function(x, table_info, font_size) {
355 row_height <- font_size + 2
356 if (table_info$tabular == "tabular" & table_info$table_env) {
357 return(sub(table_info$begin_tabular,
358 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
359 "\\}\\\\selectfont\n", table_info$begin_tabular),
360 x))
361 }
362 # For longtable and tabular without table environment. Simple wrap around
363 # fontsize is good enough
364 return(paste0(
365 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
366 "\\endgroup"
367 ))
368}
Hao Zhuf2dfd142017-07-24 14:43:28 -0400369
370