blob: ad6189ae8ba47abad184480d7aeabb86427d6599 [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#'
35#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050036kable_styling <- function(kable_input,
37 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050038 latex_options = "basic",
39 full_width = NULL,
Hao Zhu9b45a182017-02-27 18:17:46 -050040 position = "center",
Hao Zhuc05e1812017-02-25 01:45:35 -050041 font_size = NULL) {
Hao Zhu9b45a182017-02-27 18:17:46 -050042
43 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
44 bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
45 }
46 if (length(latex_options) == 1 && latex_options == "basic") {
47 latex_options <- getOption("kable_styling_latex_options", "basic")
48 }
49 if (position == "center") {
50 position <- getOption("kable_styling_position", "center")
51 }
52 position <- match.arg(position,
53 c("center", "left", "right", "float_left", "float_right"))
54 if (is.null(font_size)) {
55 font_size <- getOption("kable_styling_font_size", NULL)
56 }
57
Hao Zhuc1f38412017-02-23 12:13:48 -050058 kable_format <- attr(kable_input, "format")
Hao Zhu9b45a182017-02-27 18:17:46 -050059
Hao Zhuc1f38412017-02-23 12:13:48 -050060 if (!kable_format %in% c("html", "latex")) {
61 stop("Please specify output format in your kable function. Currently ",
62 "generic markdown table using pandoc is not supported.")
63 }
64 if (kable_format == "html") {
Hao Zhu9b45a182017-02-27 18:17:46 -050065 if (is.null(full_width)) {
66 full_width <- getOption("kable_styling_full_width", T)
67 }
Hao Zhuc1f38412017-02-23 12:13:48 -050068 return(htmlTable_styling(kable_input,
69 bootstrap_options = bootstrap_options,
70 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050071 position = position,
Hao Zhuc1f38412017-02-23 12:13:48 -050072 font_size = font_size))
73 }
74 if (kable_format == "latex") {
Hao Zhu9b45a182017-02-27 18:17:46 -050075 if (is.null(full_width)) {
76 full_width <- getOption("kable_styling_full_width", F)
77 }
Hao Zhuc05e1812017-02-25 01:45:35 -050078 return(pdfTable_styling(kable_input,
79 latex_options = latex_options,
80 full_width = full_width,
81 position = position,
82 font_size = font_size))
Hao Zhuc1f38412017-02-23 12:13:48 -050083 }
84}
85
86# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -050087htmlTable_styling <- function(kable_input,
88 bootstrap_options = "basic",
89 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -050090 position = c("center", "left", "right",
91 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -050092 font_size = NULL) {
Hao Zhu9b45a182017-02-27 18:17:46 -050093 table_info <- magic_mirror(kable_input)
Hao Zhu26234122017-02-22 15:34:33 -050094 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
95
96 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -050097 bootstrap_options <- match.arg(
98 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -050099 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -0500100 several.ok = T
101 )
102
Hao Zhu26234122017-02-22 15:34:33 -0500103 kable_xml_class <- NULL
104 if (xml_has_attr(kable_xml, "class")) {
105 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -0500106 }
Hao Zhu26234122017-02-22 15:34:33 -0500107 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
108 bootstrap_options <- "table"
109 } else {
110 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
111 bootstrap_options <- paste0("table-", bootstrap_options)
112 bootstrap_options <- c("table", bootstrap_options)
113 }
114 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
115 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500116
Hao Zhu26234122017-02-22 15:34:33 -0500117 # Modify style
118 kable_xml_style <- NULL
119 if (xml_has_attr(kable_xml, "style")) {
120 kable_xml_style <- xml_attr(kable_xml, "style")
121 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500122 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500123 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500124 paste0("font-size: ", font_size, "px;"))
Hao Zhue10cfd32017-02-21 16:41:14 -0500125 }
126 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500127 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500128 }
Hao Zhu94956582017-02-21 18:18:29 -0500129
Hao Zhuc05e1812017-02-25 01:45:35 -0500130 position <- match.arg(position)
131 position_style <- switch(
132 position,
133 center = "margin-left: auto; margin-right: auto;",
134 left = "text-align: right;",
135 right = "margin-right: 0; margin-left: auto",
136 float_left = "float: left; margin-right: 10px;",
137 float_right = "float: right; margin-left: 10px;"
138 )
139 kable_xml_style <- c(kable_xml_style, position_style)
140
Hao Zhu26234122017-02-22 15:34:33 -0500141 if (length(kable_xml_style) != 0) {
142 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500143 }
Hao Zhu9b45a182017-02-27 18:17:46 -0500144
145 out <- structure(as.character(kable_xml), format = "html",
146 class = "knitr_kable")
147 attr(out, "original_kable_meta") <- table_info
148 return(out)
Hao Zhue10cfd32017-02-21 16:41:14 -0500149}
Hao Zhuc1f38412017-02-23 12:13:48 -0500150
151# LaTeX table style
152pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500153 latex_options = "basic",
154 full_width = F,
155 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500156 "float_left", "float_right"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500157 font_size = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500158
Hao Zhuc05e1812017-02-25 01:45:35 -0500159 latex_options <- match.arg(
160 latex_options,
161 c("basic", "striped", "hold_position", "scale_down"),
162 several.ok = T
163 )
164
Hao Zhua3fc0c42017-02-27 12:04:59 -0500165 out <- NULL
Hao Zhuc05e1812017-02-25 01:45:35 -0500166 out <- as.character(kable_input)
167 table_info <- magic_mirror(kable_input)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500168 table_info$valign2 <- sub("\\[", "\\\\[", table_info$valign)
169 table_info$valign2 <- sub("\\]", "\\\\]", table_info$valign2)
170 table_info$valign3 <- sub("\\[", "", table_info$valign)
171 table_info$valign3 <- sub("\\]", "", table_info$valign3)
172 table_info$begin_tabular <- paste0("\\\\begin\\{", table_info$tabular, "\\}",
173 table_info$valign2)
174 table_info$end_tabular <- paste0("\\\\end\\{", table_info$tabular, "\\}")
Hao Zhuc05e1812017-02-25 01:45:35 -0500175
176
177 if ("striped" %in% latex_options) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500178 out <- styling_latex_striped(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500179 }
180
181 # hold_position is only meaningful in a table environment
182 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500183 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500184 }
185
Hao Zhua3fc0c42017-02-27 12:04:59 -0500186 if ("scale_down" %in% latex_options) {
187 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500188 }
189
190 if (full_width) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500191 out <- styling_latex_full_width(out, table_info)
192 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500193
Hao Zhua3fc0c42017-02-27 12:04:59 -0500194 if (!is.null(font_size)) {
195 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500196 }
197
198 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500199 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500200
201 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu9b45a182017-02-27 18:17:46 -0500202 attr(out, "original_kable_meta") <- table_info
Hao Zhuc05e1812017-02-25 01:45:35 -0500203 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500204}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500205
206styling_latex_striped <- function(x) {
207 usepackage_latex("xcolor", "table")
208 paste0(
209 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
210 "\\rowcolors{2}{gray!6}{white}\n", x, "\n\\rowcolors{2}{white}{white}")
211}
212
213styling_latex_hold_position <- function(x) {
214 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
215}
216
217styling_latex_scale_down <- function(x, table_info) {
218 # You cannot put longtable in a resizebox
219 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
220 if (table_info$tabular == "longtable") {
221 warning("Longtable cannot be resized.")
222 return(x)
223 }
224 x <- sub(table_info$begin_tabular,
225 paste0("\\\\resizebox\\{\\\\textwidth\\}\\{\\!\\}\\{",
226 table_info$begin_tabular),
227 x)
228 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
229}
230
231styling_latex_full_width <- function(x, table_info) {
232 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
233 col_max_length <- apply(size_matrix, 1, max) + 4
234 col_ratio <- round(col_max_length / sum(col_max_length), 2)
235 col_align <- paste0("p{\\\\dimexpr", col_ratio,
Hao Zhu6a076462017-03-01 12:59:01 -0500236 "\\\\columnwidth-2\\\\tabcolsep}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500237 col_align <- paste0("{", paste(col_align, collapse = ""), "}")
238 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
239 table_info$begin_tabular, x)
240 sub(table_info$begin_tabular,
241 paste0(table_info$begin_tabular, col_align), x)
242}
243
244styling_latex_position <- function(x, table_info, position, latex_options) {
245 hold_position <- "hold_position" %in% latex_options
246 switch(
247 position,
248 center = styling_latex_position_center(x, table_info, hold_position),
249 left = styling_latex_position_left(x, table_info),
250 right = styling_latex_position_right(x, table_info, hold_position),
251 float_left = styling_latex_position_float(x, table_info, "l"),
252 float_right = styling_latex_position_float(x, table_info, "r")
253 )
254}
255
256styling_latex_position_center <- function(x, table_info, hold_position) {
257 if (!table_info$table_env & table_info$tabular == "tabular") {
Hao Zhuf7994dd2017-02-27 16:58:42 -0500258 return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}"))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500259 }
260 return(x)
261}
262
263styling_latex_position_left <- function(x, table_info) {
264 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
265 longtable_option <- "\\[l\\]"
266 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
267 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
268}
269
270styling_latex_position_right <- function(x, table_info, hold_position) {
271 warning("Position = right is only supported for longtable in LaTeX. ",
272 "Setting back to center...")
273 styling_latex_position_center(x, table_info, hold_position)
274}
275
276styling_latex_position_float <- function(x, table_info, option) {
277 if (table_info$tabular == "longtable") {
278 warning("wraptable is not supported for longtable.")
279 if (option == "l") return(styling_latex_position_left(x, table_info))
280 if (option == "r") return(styling_latex_position_right(x, table_info, F))
281 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500282 usepackage_latex("wrapfig")
283 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
284 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500285 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500286 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
287 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
288 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
289 x <- sub("\\\\begin\\{table\\}", option, x)
290 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500291 } else {
292 option <- sprintf("\\begin{wraptable}{%s}", option)
293 option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
294 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500295 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500296 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500297}
298
299styling_latex_font_size <- function(x, table_info, font_size) {
300 row_height <- font_size + 2
301 if (table_info$tabular == "tabular" & table_info$table_env) {
302 return(sub(table_info$begin_tabular,
303 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
304 "\\}\\\\selectfont\n", table_info$begin_tabular),
305 x))
306 }
307 # For longtable and tabular without table environment. Simple wrap around
308 # fontsize is good enough
309 return(paste0(
310 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
311 "\\endgroup"
312 ))
313}