Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 1 | #' HTML table attributes |
| 2 | #' |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 3 | #' @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 Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 6 | #' |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 7 | #' @param bootstrap_options A character vector for bootstrap table options. For |
| 8 | #' detailed information, please check the package vignette or visit the |
Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame] | 9 | #' w3schools' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page} |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 10 | #' . Possible options include "basic", "striped", "bordered", "hover", |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 11 | #' "condensed" and "responsive". |
Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame] | 12 | #' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 13 | #' table should have 100\% width. |
| 14 | #' @param float A character string determining whether and how the HTML table |
Hao Zhu | 59f5fe0 | 2017-02-22 11:27:14 -0500 | [diff] [blame] | 15 | #' should float on the page. Values could be "left", "center", "right" |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 16 | #' @param font_size A numeric input for table font size |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 17 | #' |
| 18 | #' @export |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 19 | htmlTable_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 Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 27 | bootstrap_options <- match.arg( |
| 28 | bootstrap_options, |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 29 | c("basic", "striped", "bordered", "hover", "condensed", "responsive"), |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 30 | several.ok = T |
| 31 | ) |
| 32 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 33 | kable_xml_class <- NULL |
| 34 | if (xml_has_attr(kable_xml, "class")) { |
| 35 | kable_xml_class <- xml_attr(kable_xml, "class") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 36 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 37 | 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 Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 46 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 47 | # 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 Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 52 | if (!is.null(font_size)) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 53 | kable_xml_style <- c(kable_xml_style, |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 54 | paste0("font-size: ", font_size, "px;")) |
| 55 | } |
| 56 | if (!full_width) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 57 | kable_xml_style <- c(kable_xml_style, "width: auto !important;") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 58 | } |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 59 | |
| 60 | float <- match.arg(float) |
| 61 | if (float == "center") { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 62 | kable_xml_style <- c(kable_xml_style, |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 63 | "margin-left:auto; margin-right:auto;") |
| 64 | } |
| 65 | if (float == "right") { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 66 | kable_xml_style <- c(kable_xml_style, |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 67 | "float: right;") |
| 68 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 69 | if (length(kable_xml_style) != 0) { |
| 70 | xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 71 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 72 | return(structure(as.character(kable_xml), format = "html", |
| 73 | class = "knitr_kable")) |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 74 | } |