blob: dcd0c96285add6b3fdc06fc5ab3e0ca23efed5b6 [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 Zhu26234122017-02-22 15:34:33 -050019htmlTable_styling <- function(kable_input,
20 bootstrap_options = "basic",
21 full_width = T,
22 float = c("center", "left", "right"),
23 font_size = NULL) {
24 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
25
26 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -050027 bootstrap_options <- match.arg(
28 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -050029 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -050030 several.ok = T
31 )
32
Hao Zhu26234122017-02-22 15:34:33 -050033 kable_xml_class <- NULL
34 if (xml_has_attr(kable_xml, "class")) {
35 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -050036 }
Hao Zhu26234122017-02-22 15:34:33 -050037 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
38 bootstrap_options <- "table"
39 } else {
40 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
41 bootstrap_options <- paste0("table-", bootstrap_options)
42 bootstrap_options <- c("table", bootstrap_options)
43 }
44 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
45 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -050046
Hao Zhu26234122017-02-22 15:34:33 -050047 # Modify style
48 kable_xml_style <- NULL
49 if (xml_has_attr(kable_xml, "style")) {
50 kable_xml_style <- xml_attr(kable_xml, "style")
51 }
Hao Zhue10cfd32017-02-21 16:41:14 -050052 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -050053 kable_xml_style <- c(kable_xml_style,
Hao Zhue10cfd32017-02-21 16:41:14 -050054 paste0("font-size: ", font_size, "px;"))
55 }
56 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -050057 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -050058 }
Hao Zhu94956582017-02-21 18:18:29 -050059
60 float <- match.arg(float)
61 if (float == "center") {
Hao Zhu26234122017-02-22 15:34:33 -050062 kable_xml_style <- c(kable_xml_style,
Hao Zhu94956582017-02-21 18:18:29 -050063 "margin-left:auto; margin-right:auto;")
64 }
65 if (float == "right") {
Hao Zhu26234122017-02-22 15:34:33 -050066 kable_xml_style <- c(kable_xml_style,
Hao Zhu94956582017-02-21 18:18:29 -050067 "float: right;")
68 }
Hao Zhu26234122017-02-22 15:34:33 -050069 if (length(kable_xml_style) != 0) {
70 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -050071 }
Hao Zhu26234122017-02-22 15:34:33 -050072 return(structure(as.character(kable_xml), format = "html",
73 class = "knitr_kable"))
Hao Zhue10cfd32017-02-21 16:41:14 -050074}