blob: 9bde4f0204f6d5c55819dc6308db97b4da1d7088 [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 Zhu94956582017-02-21 18:18:29 -05007#' @param bootstrap_options A character vector for bootstrap table options. For
8#' detailed information, please check the package vignette or visit the
Hao Zhu59f5fe02017-02-22 11:27:14 -05009#' w3schools' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page}
Hao Zhu26234122017-02-22 15:34:33 -050010#' . Possible options include "basic", "striped", "bordered", "hover",
Hao Zhu94956582017-02-21 18:18:29 -050011#' "condensed" and "responsive".
Hao Zhu59f5fe02017-02-22 11:27:14 -050012#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhu94956582017-02-21 18:18:29 -050013#' table should have 100\% width.
14#' @param float A character string determining whether and how the HTML table
Hao Zhu59f5fe02017-02-22 11:27:14 -050015#' should float on the page. Values could be "left", "center", "right"
Hao Zhu94956582017-02-21 18:18:29 -050016#' @param font_size A numeric input for table font size
Hao Zhue10cfd32017-02-21 16:41:14 -050017#'
18#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050019kable_styling <- function(kable_input,
20 bootstrap_options = "basic",
21 full_width = T,
22 float = c("center", "left", "right"),
23 font_size = NULL,
24 latex_hold_position = F,
25 latex_scale_down = F) {
26 kable_format <- attr(kable_input, "format")
27 if (!kable_format %in% c("html", "latex")) {
28 stop("Please specify output format in your kable function. Currently ",
29 "generic markdown table using pandoc is not supported.")
30 }
31 if (kable_format == "html") {
32 return(htmlTable_styling(kable_input,
33 bootstrap_options = bootstrap_options,
34 full_width = full_width,
35 float = float,
36 font_size = font_size))
37 }
38 if (kable_format == "latex") {
39 return(pdfTable_styling(full_width = full_width,
40 float = float,
41 font_size = font_size,
42 latex_hold_position = latex_hold_position,
43 latex_scale_down = latex_scale_down))
44 }
45}
46
47# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -050048htmlTable_styling <- function(kable_input,
49 bootstrap_options = "basic",
50 full_width = T,
51 float = c("center", "left", "right"),
52 font_size = NULL) {
53 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
54
55 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -050056 bootstrap_options <- match.arg(
57 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -050058 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -050059 several.ok = T
60 )
61
Hao Zhu26234122017-02-22 15:34:33 -050062 kable_xml_class <- NULL
63 if (xml_has_attr(kable_xml, "class")) {
64 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -050065 }
Hao Zhu26234122017-02-22 15:34:33 -050066 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
67 bootstrap_options <- "table"
68 } else {
69 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
70 bootstrap_options <- paste0("table-", bootstrap_options)
71 bootstrap_options <- c("table", bootstrap_options)
72 }
73 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
74 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -050075
Hao Zhu26234122017-02-22 15:34:33 -050076 # Modify style
77 kable_xml_style <- NULL
78 if (xml_has_attr(kable_xml, "style")) {
79 kable_xml_style <- xml_attr(kable_xml, "style")
80 }
Hao Zhue10cfd32017-02-21 16:41:14 -050081 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -050082 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -050083 paste0("font-size: ", font_size, "px;"))
Hao Zhue10cfd32017-02-21 16:41:14 -050084 }
85 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -050086 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -050087 }
Hao Zhu94956582017-02-21 18:18:29 -050088
89 float <- match.arg(float)
90 if (float == "center") {
Hao Zhu26234122017-02-22 15:34:33 -050091 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -050092 "margin-left:auto; margin-right:auto;")
Hao Zhu94956582017-02-21 18:18:29 -050093 }
94 if (float == "right") {
Hao Zhu26234122017-02-22 15:34:33 -050095 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -050096 "float: right;")
Hao Zhu94956582017-02-21 18:18:29 -050097 }
Hao Zhu26234122017-02-22 15:34:33 -050098 if (length(kable_xml_style) != 0) {
99 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500100 }
Hao Zhu26234122017-02-22 15:34:33 -0500101 return(structure(as.character(kable_xml), format = "html",
102 class = "knitr_kable"))
Hao Zhue10cfd32017-02-21 16:41:14 -0500103}
Hao Zhuc1f38412017-02-23 12:13:48 -0500104
105# LaTeX table style
106pdfTable_styling <- function(kable_input,
107 full_width = T,
108 float = c("center", "left", "right"),
109 font_size = NULL,
110 latex_hold_position = F,
111 latex_scale_down = F) {
112
113}