blob: 7e6f92a4b0279d12074eea9ff64c7138962d0db4 [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 Zhuf7994dd2017-02-27 16:58:42 -050015#' `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 Zhu59f5fe02017-02-22 11:27:14 -050023#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhuf7994dd2017-02-27 16:58:42 -050024#' 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 Zhu94956582017-02-21 18:18:29 -050033#' @param font_size A numeric input for table font size
Hao Zhue10cfd32017-02-21 16:41:14 -050034#'
Hao Zhu78e61222017-05-24 20:53:35 -040035#' @examples x_html <- knitr::kable(head(mtcars), "html")
36#' kable_styling(x_html, "striped", position = "left", font_size = 7)
37#'
38#' x_latex <- knitr::kable(head(mtcars), "latex")
39#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
40#'
Hao Zhue10cfd32017-02-21 16:41:14 -050041#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050042kable_styling <- function(kable_input,
43 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050044 latex_options = "basic",
45 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050046 position = "center",
Hao Zhuc05e1812017-02-25 01:45:35 -050047 font_size = NULL) {
Hao Zhu9b45a182017-02-27 18:17:46 -050048
49 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
50 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
51 }
52 if (length(latex_options) == 1 && latex_options == "basic") {
53 latex_options <- getOption("kable_styling_latex_options", "basic")
54 }
55 if (position == "center") {
56 position <- getOption("kable_styling_position", "center")
57 }
58 position <- match.arg(position,
59 c("center", "left", "right", "float_left", "float_right"))
60 if (is.null(font_size)) {
61 font_size <- getOption("kable_styling_font_size", NULL)
62 }
63
Hao Zhuc1f38412017-02-23 12:13:48 -050064 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050065
Hao Zhuc1f38412017-02-23 12:13:48 -050066 if (!kable_format %in% c("html", "latex")) {
guangchuang yu9c405392017-04-29 12:34:04 +080067 message("Currently generic markdown table using pandoc is not supported.")
68 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050069 }
70 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -050071 if (is.null(full_width)) {
72 full_width <- getOption("kable_styling_full_width", T)
73 }
Hao Zhuc1f38412017-02-23 12:13:48 -050074 return(htmlTable_styling(kable_input,
75 bootstrap_options = bootstrap_options,
76 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050077 position = position,
Hao Zhuc1f38412017-02-23 12:13:48 -050078 font_size = font_size))
79 }
80 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -050081 if (is.null(full_width)) {
82 full_width <- getOption("kable_styling_full_width", F)
83 }
Hao Zhuc05e1812017-02-25 01:45:35 -050084 return(pdfTable_styling(kable_input,
85 latex_options = latex_options,
86 full_width = full_width,
87 position = position,
88 font_size = font_size))
Hao Zhuc1f38412017-02-23 12:13:48 -050089 }
90}
91
92# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -050093htmlTable_styling <- function(kable_input,
94 bootstrap_options = "basic",
95 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -050096 position = c("center", "left", "right",
97 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -050098 font_size = NULL) {
Hao Zhu9b45a182017-02-27 18:17:46 -050099 table_info <- magic_mirror(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500100 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
101
102 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500103 bootstrap_options <- match.arg(
104 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500105 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500106 several.ok = T
107 )
108
Hao Zhu26234122017-02-22 15:34:33 -0500109 kable_xml_class <- NULL
110 if (xml_has_attr(kable_xml, "class")) {
111 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500112 }
Hao Zhu26234122017-02-22 15:34:33 -0500113 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
114 bootstrap_options <- "table"
115 } else {
116 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
117 bootstrap_options <- paste0("table-", bootstrap_options)
118 bootstrap_options <- c("table", bootstrap_options)
119 }
120 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
121 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500122
Hao Zhu26234122017-02-22 15:34:33 -0500123 # Modify style
124 kable_xml_style <- NULL
125 if (xml_has_attr(kable_xml, "style")) {
126 kable_xml_style <- xml_attr(kable_xml, "style")
127 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500128 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500129 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500130 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400131 kable_caption_node <- xml_tpart(kable_xml, "caption")
132 if (!is.null(kable_caption_node)) {
133 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
134 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500135 }
136 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500137 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500138 }
Hao Zhu94956582017-02-21 18:18:29 -0500139
Hao Zhuc05e1812017-02-25 01:45:35 -0500140 position <- match.arg(position)
141 position_style <- switch(
142 position,
143 center = "margin-left: auto; margin-right: auto;",
144 left = "text-align: right;",
145 right = "margin-right: 0; margin-left: auto",
146 float_left = "float: left; margin-right: 10px;",
147 float_right = "float: right; margin-left: 10px;"
148 )
149 kable_xml_style <- c(kable_xml_style, position_style)
150
Hao Zhu26234122017-02-22 15:34:33 -0500151 if (length(kable_xml_style) != 0) {
152 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500153 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500154
155 out <- structure(as.character(kable_xml), format = "html",
156 class = "knitr_kable")
157 attr(out, "original_kable_meta") <- table_info
158 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500159}
Hao Zhuc1f38412017-02-23 12:13:48 -0500160
161# LaTeX table style
162pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500163 latex_options = "basic",
164 full_width = F,
165 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500166 "float_left", "float_right"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500167 font_size = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500168
Hao Zhuc05e1812017-02-25 01:45:35 -0500169 latex_options <- match.arg(
170 latex_options,
171 c("basic", "striped", "hold_position", "scale_down"),
172 several.ok = T
173 )
174
Hao Zhua3fc0c42017-02-27 12:04:59 -0500175 out <- NULL
Hao Zhuc05e1812017-02-25 01:45:35 -0500176 out <- as.character(kable_input)
177 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500178
179 if ("striped" %in% latex_options) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500180 out <- styling_latex_striped(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500181 }
182
183 # hold_position is only meaningful in a table environment
184 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500185 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500186 }
187
Hao Zhua3fc0c42017-02-27 12:04:59 -0500188 if ("scale_down" %in% latex_options) {
189 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500190 }
191
192 if (full_width) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500193 out <- styling_latex_full_width(out, table_info)
194 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500195
Hao Zhua3fc0c42017-02-27 12:04:59 -0500196 if (!is.null(font_size)) {
197 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500198 }
199
200 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500201 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500202
203 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu9b45a182017-02-27 18:17:46 -0500204 attr(out, "original_kable_meta") <- table_info
Hao Zhuc05e1812017-02-25 01:45:35 -0500205 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500206}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500207
208styling_latex_striped <- function(x) {
209 usepackage_latex("xcolor", "table")
210 paste0(
211 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
212 "\\rowcolors{2}{gray!6}{white}\n", x, "\n\\rowcolors{2}{white}{white}")
213}
214
215styling_latex_hold_position <- function(x) {
216 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
217}
218
219styling_latex_scale_down <- function(x, table_info) {
220 # You cannot put longtable in a resizebox
221 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
222 if (table_info$tabular == "longtable") {
223 warning("Longtable cannot be resized.")
224 return(x)
225 }
226 x <- sub(table_info$begin_tabular,
Hao Zhucc21dc72017-05-20 01:15:25 -0400227 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500228 table_info$begin_tabular),
229 x)
230 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
231}
232
233styling_latex_full_width <- function(x, table_info) {
234 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
235 col_max_length <- apply(size_matrix, 1, max) + 4
236 col_ratio <- round(col_max_length / sum(col_max_length), 2)
237 col_align <- paste0("p{\\\\dimexpr", col_ratio,
Hao Zhu6a076462017-03-01 12:59:01 -0500238 "\\\\columnwidth-2\\\\tabcolsep}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500239 col_align <- paste0("{", paste(col_align, collapse = ""), "}")
240 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
241 table_info$begin_tabular, x)
242 sub(table_info$begin_tabular,
243 paste0(table_info$begin_tabular, col_align), x)
244}
245
246styling_latex_position <- function(x, table_info, position, latex_options) {
247 hold_position <- "hold_position" %in% latex_options
248 switch(
249 position,
250 center = styling_latex_position_center(x, table_info, hold_position),
251 left = styling_latex_position_left(x, table_info),
252 right = styling_latex_position_right(x, table_info, hold_position),
253 float_left = styling_latex_position_float(x, table_info, "l"),
254 float_right = styling_latex_position_float(x, table_info, "r")
255 )
256}
257
258styling_latex_position_center <- function(x, table_info, hold_position) {
259 if (!table_info$table_env & table_info$tabular == "tabular") {
Hao Zhuf7994dd2017-02-27 16:58:42 -0500260 return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}"))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500261 }
262 return(x)
263}
264
265styling_latex_position_left <- function(x, table_info) {
266 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
267 longtable_option <- "\\[l\\]"
268 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
269 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
270}
271
272styling_latex_position_right <- function(x, table_info, hold_position) {
273 warning("Position = right is only supported for longtable in LaTeX. ",
274 "Setting back to center...")
275 styling_latex_position_center(x, table_info, hold_position)
276}
277
278styling_latex_position_float <- function(x, table_info, option) {
279 if (table_info$tabular == "longtable") {
280 warning("wraptable is not supported for longtable.")
281 if (option == "l") return(styling_latex_position_left(x, table_info))
282 if (option == "r") return(styling_latex_position_right(x, table_info, F))
283 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500284 usepackage_latex("wrapfig")
285 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
286 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500287 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500288 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
289 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
290 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
291 x <- sub("\\\\begin\\{table\\}", option, x)
292 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500293 } else {
294 option <- sprintf("\\begin{wraptable}{%s}", option)
295 option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
296 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500297 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500298 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500299}
300
301styling_latex_font_size <- function(x, table_info, font_size) {
302 row_height <- font_size + 2
303 if (table_info$tabular == "tabular" & table_info$table_env) {
304 return(sub(table_info$begin_tabular,
305 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
306 "\\}\\\\selectfont\n", table_info$begin_tabular),
307 x))
308 }
309 # For longtable and tabular without table environment. Simple wrap around
310 # fontsize is good enough
311 return(paste0(
312 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
313 "\\endgroup"
314 ))
315}