blob: 74f11e1d115202bc701b148c562f5b7fe78995cc [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 Zhue10cfd32017-02-21 16:41:14 -050036#'
Hao Zhu78e61222017-05-24 20:53:35 -040037#' @examples x_html <- knitr::kable(head(mtcars), "html")
38#' kable_styling(x_html, "striped", position = "left", font_size = 7)
39#'
40#' x_latex <- knitr::kable(head(mtcars), "latex")
41#' kable_styling(x_latex, latex_options = "striped", position = "float_left")
42#'
Hao Zhue10cfd32017-02-21 16:41:14 -050043#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050044kable_styling <- function(kable_input,
45 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050046 latex_options = "basic",
47 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050048 position = "center",
Hao Zhuc05e1812017-02-25 01:45:35 -050049 font_size = NULL) {
Hao Zhu9b45a182017-02-27 18:17:46 -050050
51 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
52 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
53 }
54 if (length(latex_options) == 1 && latex_options == "basic") {
55 latex_options <- getOption("kable_styling_latex_options", "basic")
56 }
57 if (position == "center") {
58 position <- getOption("kable_styling_position", "center")
59 }
60 position <- match.arg(position,
61 c("center", "left", "right", "float_left", "float_right"))
62 if (is.null(font_size)) {
63 font_size <- getOption("kable_styling_font_size", NULL)
64 }
65
Hao Zhuc1f38412017-02-23 12:13:48 -050066 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050067
Hao Zhuc1f38412017-02-23 12:13:48 -050068 if (!kable_format %in% c("html", "latex")) {
guangchuang yu9c405392017-04-29 12:34:04 +080069 message("Currently generic markdown table using pandoc is not supported.")
70 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050071 }
72 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -050073 if (is.null(full_width)) {
74 full_width <- getOption("kable_styling_full_width", T)
75 }
Hao Zhuc1f38412017-02-23 12:13:48 -050076 return(htmlTable_styling(kable_input,
77 bootstrap_options = bootstrap_options,
78 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050079 position = position,
Hao Zhuc1f38412017-02-23 12:13:48 -050080 font_size = font_size))
81 }
82 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -050083 if (is.null(full_width)) {
84 full_width <- getOption("kable_styling_full_width", F)
85 }
Hao Zhuc05e1812017-02-25 01:45:35 -050086 return(pdfTable_styling(kable_input,
87 latex_options = latex_options,
88 full_width = full_width,
89 position = position,
90 font_size = font_size))
Hao Zhuc1f38412017-02-23 12:13:48 -050091 }
92}
93
94# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -050095htmlTable_styling <- function(kable_input,
96 bootstrap_options = "basic",
97 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -050098 position = c("center", "left", "right",
99 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -0500100 font_size = NULL) {
Hao Zhu9b45a182017-02-27 18:17:46 -0500101 table_info <- magic_mirror(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -0500102 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
103
104 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -0500105 bootstrap_options <- match.arg(
106 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -0500107 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500108 several.ok = T
109 )
110
Hao Zhu26234122017-02-22 15:34:33 -0500111 kable_xml_class <- NULL
112 if (xml_has_attr(kable_xml, "class")) {
113 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500114 }
Hao Zhu26234122017-02-22 15:34:33 -0500115 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
116 bootstrap_options <- "table"
117 } else {
118 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
119 bootstrap_options <- paste0("table-", bootstrap_options)
120 bootstrap_options <- c("table", bootstrap_options)
121 }
122 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
123 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500124
Hao Zhu26234122017-02-22 15:34:33 -0500125 # Modify style
126 kable_xml_style <- NULL
127 if (xml_has_attr(kable_xml, "style")) {
128 kable_xml_style <- xml_attr(kable_xml, "style")
129 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500130 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500131 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500132 paste0("font-size: ", font_size, "px;"))
Hao Zhufc14c9b2017-05-22 14:03:22 -0400133 kable_caption_node <- xml_tpart(kable_xml, "caption")
134 if (!is.null(kable_caption_node)) {
135 xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
136 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500137 }
138 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500139 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500140 }
Hao Zhu94956582017-02-21 18:18:29 -0500141
Hao Zhuc05e1812017-02-25 01:45:35 -0500142 position <- match.arg(position)
143 position_style <- switch(
144 position,
145 center = "margin-left: auto; margin-right: auto;",
146 left = "text-align: right;",
147 right = "margin-right: 0; margin-left: auto",
148 float_left = "float: left; margin-right: 10px;",
149 float_right = "float: right; margin-left: 10px;"
150 )
151 kable_xml_style <- c(kable_xml_style, position_style)
152
Hao Zhu26234122017-02-22 15:34:33 -0500153 if (length(kable_xml_style) != 0) {
154 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500155 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500156
157 out <- structure(as.character(kable_xml), format = "html",
158 class = "knitr_kable")
159 attr(out, "original_kable_meta") <- table_info
160 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500161}
Hao Zhuc1f38412017-02-23 12:13:48 -0500162
163# LaTeX table style
164pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500165 latex_options = "basic",
166 full_width = F,
167 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500168 "float_left", "float_right"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500169 font_size = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500170
Hao Zhuc05e1812017-02-25 01:45:35 -0500171 latex_options <- match.arg(
172 latex_options,
Hao Zhu971d89f2017-06-07 19:22:47 -0400173 c("basic", "striped", "hold_position", "scale_down", "repeat_header"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500174 several.ok = T
175 )
176
Hao Zhua3fc0c42017-02-27 12:04:59 -0500177 out <- NULL
Hao Zhuc05e1812017-02-25 01:45:35 -0500178 out <- as.character(kable_input)
179 table_info <- magic_mirror(kable_input)
Hao Zhuc05e1812017-02-25 01:45:35 -0500180
181 if ("striped" %in% latex_options) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500182 out <- styling_latex_striped(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500183 }
184
185 # hold_position is only meaningful in a table environment
186 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500187 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500188 }
189
Hao Zhua3fc0c42017-02-27 12:04:59 -0500190 if ("scale_down" %in% latex_options) {
191 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500192 }
193
Hao Zhu971d89f2017-06-07 19:22:47 -0400194 if ("repeat_header" %in% latex_options & table_info$tabular == "longtable") {
195 out <- styling_latex_repeat_header(out, table_info)
196 }
197
Hao Zhuc05e1812017-02-25 01:45:35 -0500198 if (full_width) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500199 out <- styling_latex_full_width(out, table_info)
200 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500201
Hao Zhua3fc0c42017-02-27 12:04:59 -0500202 if (!is.null(font_size)) {
203 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500204 }
205
206 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500207 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500208
209 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu9b45a182017-02-27 18:17:46 -0500210 attr(out, "original_kable_meta") <- table_info
Hao Zhuc05e1812017-02-25 01:45:35 -0500211 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500212}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500213
214styling_latex_striped <- function(x) {
215 usepackage_latex("xcolor", "table")
216 paste0(
217 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
218 "\\rowcolors{2}{gray!6}{white}\n", x, "\n\\rowcolors{2}{white}{white}")
219}
220
221styling_latex_hold_position <- function(x) {
222 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
223}
224
225styling_latex_scale_down <- function(x, table_info) {
226 # You cannot put longtable in a resizebox
227 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
228 if (table_info$tabular == "longtable") {
229 warning("Longtable cannot be resized.")
230 return(x)
231 }
232 x <- sub(table_info$begin_tabular,
Hao Zhucc21dc72017-05-20 01:15:25 -0400233 paste0("\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500234 table_info$begin_tabular),
235 x)
236 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
237}
238
Hao Zhu971d89f2017-06-07 19:22:47 -0400239styling_latex_repeat_header <- function(x, table_info) {
240 row_one <- table_info$contents[2]
241 new_row_one <- paste0("\\\\endhead\n", row_one)
242 return(sub(row_one, new_row_one, x))
243}
244
Hao Zhua3fc0c42017-02-27 12:04:59 -0500245styling_latex_full_width <- function(x, table_info) {
246 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
247 col_max_length <- apply(size_matrix, 1, max) + 4
248 col_ratio <- round(col_max_length / sum(col_max_length), 2)
249 col_align <- paste0("p{\\\\dimexpr", col_ratio,
Hao Zhu6a076462017-03-01 12:59:01 -0500250 "\\\\columnwidth-2\\\\tabcolsep}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500251 col_align <- paste0("{", paste(col_align, collapse = ""), "}")
252 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
253 table_info$begin_tabular, x)
254 sub(table_info$begin_tabular,
255 paste0(table_info$begin_tabular, col_align), x)
256}
257
258styling_latex_position <- function(x, table_info, position, latex_options) {
259 hold_position <- "hold_position" %in% latex_options
260 switch(
261 position,
262 center = styling_latex_position_center(x, table_info, hold_position),
263 left = styling_latex_position_left(x, table_info),
264 right = styling_latex_position_right(x, table_info, hold_position),
265 float_left = styling_latex_position_float(x, table_info, "l"),
266 float_right = styling_latex_position_float(x, table_info, "r")
267 )
268}
269
270styling_latex_position_center <- function(x, table_info, hold_position) {
271 if (!table_info$table_env & table_info$tabular == "tabular") {
Hao Zhuf7994dd2017-02-27 16:58:42 -0500272 return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}"))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500273 }
274 return(x)
275}
276
277styling_latex_position_left <- function(x, table_info) {
278 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
279 longtable_option <- "\\[l\\]"
280 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
281 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
282}
283
284styling_latex_position_right <- function(x, table_info, hold_position) {
285 warning("Position = right is only supported for longtable in LaTeX. ",
286 "Setting back to center...")
287 styling_latex_position_center(x, table_info, hold_position)
288}
289
290styling_latex_position_float <- function(x, table_info, option) {
291 if (table_info$tabular == "longtable") {
292 warning("wraptable is not supported for longtable.")
293 if (option == "l") return(styling_latex_position_left(x, table_info))
294 if (option == "r") return(styling_latex_position_right(x, table_info, F))
295 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500296 usepackage_latex("wrapfig")
297 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
298 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500299 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500300 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
301 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
302 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
303 x <- sub("\\\\begin\\{table\\}", option, x)
304 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500305 } else {
306 option <- sprintf("\\begin{wraptable}{%s}", option)
307 option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
308 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500309 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500310 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500311}
312
313styling_latex_font_size <- function(x, table_info, font_size) {
314 row_height <- font_size + 2
315 if (table_info$tabular == "tabular" & table_info$table_env) {
316 return(sub(table_info$begin_tabular,
317 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
318 "\\}\\\\selectfont\n", table_info$begin_tabular),
319 x))
320 }
321 # For longtable and tabular without table environment. Simple wrap around
322 # fontsize is good enough
323 return(paste0(
324 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
325 "\\endgroup"
326 ))
327}