Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 1 | #' HTML table attributes |
| 2 | #' |
| 3 | #' @description This function provides a set of shortcuts to common HTML table |
| 4 | #' formats |
| 5 | #' |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 6 | #' @param bootstrap_options A character vector for bootstrap table options. For |
| 7 | #' detailed information, please check the package vignette or visit the |
Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame^] | 8 | #' w3schools' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page} |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 9 | #' . Possible options include "bs-table", "striped", "bordered", "hover", |
| 10 | #' "condensed" and "responsive". |
Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame^] | 11 | #' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 12 | #' table should have 100\% width. |
| 13 | #' @param float A character string determining whether and how the HTML table |
Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame^] | 14 | #' should float on the page. Values could be "left", "center", "right" |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 15 | #' @param font_size A numeric input for table font size |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 16 | #' |
| 17 | #' @export |
| 18 | htmlTable_styling <- function(bootstrap_options = "bs-table", |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 19 | full_width = T, |
Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame^] | 20 | float = c("center", "left", "right"), |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 21 | font_size = NULL) { |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 22 | 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 Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 46 | |
| 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 Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 56 | 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 | } |