blob: 9afd9cac62592046885d834ee2928c3c2fb597cf [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
8#' w3schools' [Bootstrap Page](https://www.w3schools.com/bootstrap/bootstrap_tables.asp)
9#' . Possible options include "bs-table", "striped", "bordered", "hover",
10#' "condensed" and "responsive".
11#' @param full_width A `TRUE` of `FALSE` variable controlling whether the HTML
12#' table should have 100\% width.
13#' @param float A character string determining whether and how the HTML table
14#' should float on the page.
15#' @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,
20 float = c("left", "center", "right"),
21 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}