blob: 98e6039aaa2a7ffea4fc1b7918f9e9ab2a88a234 [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 Zhua31e97f2017-06-08 14:55:41 -040036#' @param ... extra options for HTML or LaTeX
Hao Zhue10cfd32017-02-21 16:41:14 -050037#'
Hao Zhu78e61222017-05-24 20:53:35 -040038#' @examples x_html <- knitr::kable(head(mtcars), "html")
39#' kable_styling(x_html, "striped", position = "left", font_size = 7)
40#'
41#' x_latex <- knitr::kable(head(mtcars), "latex")
42#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
43#'
Hao Zhue10cfd32017-02-21 16:41:14 -050044#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050045kable_styling <- function(kable_input,
46 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050047 latex_options = "basic",
48 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050049 position = "center",
Hao Zhua31e97f2017-06-08 14:55:41 -040050 font_size = NULL,
51 ...) {
Hao Zhu9b45a182017-02-27 18:17:46 -050052
53 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
54 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
55 }
56 if (length(latex_options) == 1 && latex_options == "basic") {
57 latex_options <- getOption("kable_styling_latex_options", "basic")
58 }
59 if (position == "center") {
60 position <- getOption("kable_styling_position", "center")
61 }
62 position <- match.arg(position,
63 c("center", "left", "right", "float_left", "float_right"))
64 if (is.null(font_size)) {
65 font_size <- getOption("kable_styling_font_size", NULL)
66 }
67
Hao Zhuc1f38412017-02-23 12:13:48 -050068 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050069
Hao Zhuc1f38412017-02-23 12:13:48 -050070 if (!kable_format %in% c("html", "latex")) {
guangchuang yu9c405392017-04-29 12:34:04 +080071 message("Currently generic markdown table using pandoc is not supported.")
72 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050073 }
74 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -050075 if (is.null(full_width)) {
76 full_width <- getOption("kable_styling_full_width", T)
77 }
Hao Zhuc1f38412017-02-23 12:13:48 -050078 return(htmlTable_styling(kable_input,
79 bootstrap_options = bootstrap_options,
80 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050081 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -040082 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -050083 }
84 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -050085 if (is.null(full_width)) {
86 full_width <- getOption("kable_styling_full_width", F)
87 }
Hao Zhuc05e1812017-02-25 01:45:35 -050088 return(pdfTable_styling(kable_input,
89 latex_options = latex_options,
90 full_width = full_width,
91 position = position,
Hao Zhua31e97f2017-06-08 14:55:41 -040092 font_size = font_size, ...))
Hao Zhuc1f38412017-02-23 12:13:48 -050093 }
94}
95
96# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -050097htmlTable_styling <- function(kable_input,
98 bootstrap_options = "basic",
99 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -0500100 position = c("center", "left", "right",
101 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -0500102 font_size = NULL) {
Hao Zhu9b45a182017-02-27 18:17:46 -0500103 table_info <- magic_mirror(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500104 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
105
106 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500107 bootstrap_options <- match.arg(
108 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500109 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500110 several.ok = T
111 )
112
Hao Zhu26234122017-02-22 15:34:33 -0500113 kable_xml_class <- NULL
114 if (xml_has_attr(kable_xml, "class")) {
115 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500116 }
Hao Zhu26234122017-02-22 15:34:33 -0500117 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
118 bootstrap_options <- "table"
119 } else {
120 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
121 bootstrap_options <- paste0("table-", bootstrap_options)
122 bootstrap_options <- c("table", bootstrap_options)
123 }
124 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
125 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500126
Hao Zhu26234122017-02-22 15:34:33 -0500127 # Modify style
128 kable_xml_style <- NULL
129 if (xml_has_attr(kable_xml, "style")) {
130 kable_xml_style <- xml_attr(kable_xml, "style")
131 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500132 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500133 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500134 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400135 kable_caption_node <- xml_tpart(kable_xml, "caption")
136 if (!is.null(kable_caption_node)) {
137 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
138 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500139 }
140 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500141 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500142 }
Hao Zhu94956582017-02-21 18:18:29 -0500143
Hao Zhuc05e1812017-02-25 01:45:35 -0500144 position <- match.arg(position)
145 position_style <- switch(
146 position,
147 center = "margin-left: auto; margin-right: auto;",
148 left = "text-align: right;",
149 right = "margin-right: 0; margin-left: auto",
150 float_left = "float: left; margin-right: 10px;",
151 float_right = "float: right; margin-left: 10px;"
152 )
153 kable_xml_style <- c(kable_xml_style, position_style)
154
Hao Zhu26234122017-02-22 15:34:33 -0500155 if (length(kable_xml_style) != 0) {
156 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500157 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500158
159 out <- structure(as.character(kable_xml), format = "html",
160 class = "knitr_kable")
161 attr(out, "original_kable_meta") <- table_info
162 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500163}
Hao Zhuc1f38412017-02-23 12:13:48 -0500164
165# LaTeX table style
166pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500167 latex_options = "basic",
168 full_width = F,
169 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500170 "float_left", "float_right"),
Hao Zhua31e97f2017-06-08 14:55:41 -0400171 font_size = NULL,
Hao Zhu838f6ed2017-06-08 17:22:45 -0400172 repeat_header_text = "\\textit{(continued)}") {
Hao Zhuc1f38412017-02-23 12:13:48 -0500173
Hao Zhuc05e1812017-02-25 01:45:35 -0500174 latex_options <- match.arg(
175 latex_options,
Hao Zhu971d89f2017-06-07 19:22:47 -0400176 c("basic", "striped", "hold_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500177 several.ok = T
178 )
179
Hao Zhua3fc0c42017-02-27 12:04:59 -0500180 out <- NULL
Hao Zhuc05e1812017-02-25 01:45:35 -0500181 out <- as.character(kable_input)
182 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500183
184 if ("striped" %in% latex_options) {
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400185 out <- styling_latex_striped(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500186 }
187
188 # hold_position is only meaningful in a table environment
189 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500190 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500191 }
192
Hao Zhua3fc0c42017-02-27 12:04:59 -0500193 if ("scale_down" %in% latex_options) {
194 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500195 }
196
Hao Zhu971d89f2017-06-07 19:22:47 -0400197 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
Hao Zhua31e97f2017-06-08 14:55:41 -0400198 out <- styling_latex_repeat_header(out, table_info, repeat_header_text)
Hao Zhu971d89f2017-06-07 19:22:47 -0400199 }
200
Hao Zhuc05e1812017-02-25 01:45:35 -0500201 if (full_width) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500202 out <- styling_latex_full_width(out, table_info)
203 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500204
Hao Zhua3fc0c42017-02-27 12:04:59 -0500205 if (!is.null(font_size)) {
206 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500207 }
208
209 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500210 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500211
212 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu9b45a182017-02-27 18:17:46 -0500213 attr(out, "original_kable_meta") <- table_info
Hao Zhuc05e1812017-02-25 01:45:35 -0500214 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500215}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500216
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400217styling_latex_striped <- function(x, table_info) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500218 usepackage_latex("xcolor", "table")
Hao Zhu7d4a3e32017-06-08 21:29:13 -0400219 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
220 if (table_info$tabular == "longtable" & !is.na(table_info$caption)) {
221 row_color <- "\\rowcolors{2}{white}{gray!6}\n"
222 } else {
223 row_color <- "\\rowcolors{2}{gray!6}{white}\n"
224 }
225 return(paste0(row_color, x, "\n\\rowcolors{2}{white}{white}"))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500226}
227
228styling_latex_hold_position <- function(x) {
229 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
230}
231
232styling_latex_scale_down <- function(x, table_info) {
233 # You cannot put longtable in a resizebox
234 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
235 if (table_info$tabular == "longtable") {
236 warning("Longtable cannot be resized.")
237 return(x)
238 }
239 x <- sub(table_info$begin_tabular,
Hao Zhucc21dc72017-05-20 01:15:25 -0400240 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500241 table_info$begin_tabular),
242 x)
243 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
244}
245
Hao Zhua31e97f2017-06-08 14:55:41 -0400246styling_latex_repeat_header <- function(x, table_info, repeat_header_text) {
247 x <- read_lines(x)
248 if (table_info$booktabs) {
249 header_rows_start <- which(x == "\\toprule")[1]
250 header_rows_end <- which(x == "\\midrule")[1]
251 } else {
252 header_rows_start <- which(x == "\\hline")[1]
253 header_rows_end <- which(x == "\\hline")[2]
254 }
Hao Zhufdec1842017-06-08 17:06:04 -0400255
256 if (is.na(table_info$caption)) {
257 continue_line <- paste0(
258 "\\multicolumn{", table_info$ncol, "}{@{}l}{", repeat_header_text,
259 "}\\\\"
260 )
261 } else {
262 continue_line <- paste0(
263 "\\caption{", table_info$caption, " ", repeat_header_text,
264 "}\\\\"
265 )
266 }
267
Hao Zhua31e97f2017-06-08 14:55:41 -0400268 x <- c(
269 x[1:header_rows_end],
270 "\\endfirsthead",
271 continue_line,
272 x[header_rows_start:header_rows_end],
273 "\\endhead",
274 x[(header_rows_end + 1):length(x)]
275 )
276 x <- paste0(x, collapse = "\n")
277 return(x)
Hao Zhu971d89f2017-06-07 19:22:47 -0400278}
279
Hao Zhua3fc0c42017-02-27 12:04:59 -0500280styling_latex_full_width <- function(x, table_info) {
281 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
282 col_max_length <- apply(size_matrix, 1, max) + 4
283 col_ratio <- round(col_max_length / sum(col_max_length), 2)
284 col_align <- paste0("p{\\\\dimexpr", col_ratio,
Hao Zhu6a076462017-03-01 12:59:01 -0500285 "\\\\columnwidth-2\\\\tabcolsep}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500286 col_align <- paste0("{", paste(col_align, collapse = ""), "}")
287 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
288 table_info$begin_tabular, x)
289 sub(table_info$begin_tabular,
290 paste0(table_info$begin_tabular, col_align), x)
291}
292
293styling_latex_position <- function(x, table_info, position, latex_options) {
294 hold_position <- "hold_position" %in% latex_options
295 switch(
296 position,
297 center = styling_latex_position_center(x, table_info, hold_position),
298 left = styling_latex_position_left(x, table_info),
299 right = styling_latex_position_right(x, table_info, hold_position),
300 float_left = styling_latex_position_float(x, table_info, "l"),
301 float_right = styling_latex_position_float(x, table_info, "r")
302 )
303}
304
305styling_latex_position_center <- function(x, table_info, hold_position) {
306 if (!table_info$table_env & table_info$tabular == "tabular") {
Hao Zhuf7994dd2017-02-27 16:58:42 -0500307 return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}"))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500308 }
309 return(x)
310}
311
312styling_latex_position_left <- function(x, table_info) {
313 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
314 longtable_option <- "\\[l\\]"
315 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
316 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
317}
318
319styling_latex_position_right <- function(x, table_info, hold_position) {
320 warning("Position = right is only supported for longtable in LaTeX. ",
321 "Setting back to center...")
322 styling_latex_position_center(x, table_info, hold_position)
323}
324
325styling_latex_position_float <- function(x, table_info, option) {
326 if (table_info$tabular == "longtable") {
327 warning("wraptable is not supported for longtable.")
328 if (option == "l") return(styling_latex_position_left(x, table_info))
329 if (option == "r") return(styling_latex_position_right(x, table_info, F))
330 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500331 usepackage_latex("wrapfig")
332 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
333 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500334 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500335 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
336 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
337 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
338 x <- sub("\\\\begin\\{table\\}", option, x)
339 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500340 } else {
341 option <- sprintf("\\begin{wraptable}{%s}", option)
342 option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
343 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500344 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500345 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500346}
347
348styling_latex_font_size <- function(x, table_info, font_size) {
349 row_height <- font_size + 2
350 if (table_info$tabular == "tabular" & table_info$table_env) {
351 return(sub(table_info$begin_tabular,
352 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
353 "\\}\\\\selectfont\n", table_info$begin_tabular),
354 x))
355 }
356 # For longtable and tabular without table environment. Simple wrap around
357 # fontsize is good enough
358 return(paste0(
359 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
360 "\\endgroup"
361 ))
362}