blob: 98da798a39c9761d5244dc8de1c6caef7581a672 [file] [log] [blame]
Hao Zhue10cfd32017-02-21 16:41:14 -05001#' HTML table attributes
2#'
3#' @description This function provides a set of shortcuts to common HTML table
4#' formats
5#'
Hao Zhu94956582017-02-21 18:18:29 -05006#' @param bootstrap_options A character vector for bootstrap table options. For
7#' detailed information, please check the package vignette or visit the
Hao Zhu59f5fe02017-02-22 11:27:14 -05008#' w3schools' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page}
Hao Zhu94956582017-02-21 18:18:29 -05009#' . Possible options include "bs-table", "striped", "bordered", "hover",
10#' "condensed" and "responsive".
Hao Zhu59f5fe02017-02-22 11:27:14 -050011#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhu94956582017-02-21 18:18:29 -050012#' table should have 100\% width.
13#' @param float A character string determining whether and how the HTML table
Hao Zhu59f5fe02017-02-22 11:27:14 -050014#' should float on the page. Values could be "left", "center", "right"
Hao Zhu94956582017-02-21 18:18:29 -050015#' @param font_size A numeric input for table font size
Hao Zhue10cfd32017-02-21 16:41:14 -050016#'
17#' @export
18htmlTable_styling <- function(bootstrap_options = "bs-table",
Hao Zhu94956582017-02-21 18:18:29 -050019 full_width = T,
Hao Zhu59f5fe02017-02-22 11:27:14 -050020 float = c("center", "left", "right"),
Hao Zhu94956582017-02-21 18:18:29 -050021 font_size = NULL) {
Hao Zhue10cfd32017-02-21 16:41:14 -050022 bootstrap_options <- match.arg(
23 bootstrap_options,
24 c("bs-table", "striped", "bordered", "hover", "condensed", "responsive"),
25 several.ok = T
26 )
27
28 table_attr_class <- character()
29 if (length(bootstrap_options) == 1 && bootstrap_options == "bs-table") {
30 table_attr_class <- "class='table'"
31 } else {
32 bootstrap_options <- bootstrap_options[bootstrap_options != "bs-table"]
33 bootstrap_options <- paste0("table-", bootstrap_options)
34 table_attr_class <- paste0("class='table ",
35 paste0(bootstrap_options, collapse = " "), "'")
36 }
37
38 table_attr_style <- c()
39 if (!is.null(font_size)) {
40 table_attr_style <- c(table_attr_style,
41 paste0("font-size: ", font_size, "px;"))
42 }
43 if (!full_width) {
44 table_attr_style <- c(table_attr_style, "width: auto !important;")
45 }
Hao Zhu94956582017-02-21 18:18:29 -050046
47 float <- match.arg(float)
48 if (float == "center") {
49 table_attr_style <- c(table_attr_style,
50 "margin-left:auto; margin-right:auto;")
51 }
52 if (float == "right") {
53 table_attr_style <- c(table_attr_style,
54 "float: right;")
55 }
Hao Zhue10cfd32017-02-21 16:41:14 -050056 if (length(table_attr_style) != 0) {
57 table_attr_style <- paste0("style = '",
58 paste0(table_attr_style, collapse = " "), "'")
59 }
60 return(paste(table_attr_class, table_attr_style))
61}