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 | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame^] | 19 | kable_styling <- function(kable_input, |
| 20 | bootstrap_options = "basic", |
| 21 | full_width = T, |
| 22 | float = c("center", "left", "right"), |
| 23 | font_size = NULL, |
| 24 | latex_hold_position = F, |
| 25 | latex_scale_down = F) { |
| 26 | kable_format <- attr(kable_input, "format") |
| 27 | if (!kable_format %in% c("html", "latex")) { |
| 28 | stop("Please specify output format in your kable function. Currently ", |
| 29 | "generic markdown table using pandoc is not supported.") |
| 30 | } |
| 31 | if (kable_format == "html") { |
| 32 | return(htmlTable_styling(kable_input, |
| 33 | bootstrap_options = bootstrap_options, |
| 34 | full_width = full_width, |
| 35 | float = float, |
| 36 | font_size = font_size)) |
| 37 | } |
| 38 | if (kable_format == "latex") { |
| 39 | return(pdfTable_styling(full_width = full_width, |
| 40 | float = float, |
| 41 | font_size = font_size, |
| 42 | latex_hold_position = latex_hold_position, |
| 43 | latex_scale_down = latex_scale_down)) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | # htmlTable Styling ------------ |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 48 | htmlTable_styling <- function(kable_input, |
| 49 | bootstrap_options = "basic", |
| 50 | full_width = T, |
| 51 | float = c("center", "left", "right"), |
| 52 | font_size = NULL) { |
| 53 | kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT")) |
| 54 | |
| 55 | # Modify class |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 56 | bootstrap_options <- match.arg( |
| 57 | bootstrap_options, |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 58 | c("basic", "striped", "bordered", "hover", "condensed", "responsive"), |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 59 | several.ok = T |
| 60 | ) |
| 61 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 62 | kable_xml_class <- NULL |
| 63 | if (xml_has_attr(kable_xml, "class")) { |
| 64 | kable_xml_class <- xml_attr(kable_xml, "class") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 65 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 66 | if (length(bootstrap_options) == 1 && bootstrap_options == "basic") { |
| 67 | bootstrap_options <- "table" |
| 68 | } else { |
| 69 | bootstrap_options <- bootstrap_options[bootstrap_options != "basic"] |
| 70 | bootstrap_options <- paste0("table-", bootstrap_options) |
| 71 | bootstrap_options <- c("table", bootstrap_options) |
| 72 | } |
| 73 | xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options), |
| 74 | collapse = " ") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 75 | |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 76 | # Modify style |
| 77 | kable_xml_style <- NULL |
| 78 | if (xml_has_attr(kable_xml, "style")) { |
| 79 | kable_xml_style <- xml_attr(kable_xml, "style") |
| 80 | } |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 81 | if (!is.null(font_size)) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 82 | kable_xml_style <- c(kable_xml_style, |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame^] | 83 | paste0("font-size: ", font_size, "px;")) |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 84 | } |
| 85 | if (!full_width) { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 86 | kable_xml_style <- c(kable_xml_style, "width: auto !important;") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 87 | } |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 88 | |
| 89 | float <- match.arg(float) |
| 90 | if (float == "center") { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 91 | kable_xml_style <- c(kable_xml_style, |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame^] | 92 | "margin-left:auto; margin-right:auto;") |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 93 | } |
| 94 | if (float == "right") { |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 95 | kable_xml_style <- c(kable_xml_style, |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame^] | 96 | "float: right;") |
Hao Zhu | 9495658 | 2017-02-21 18:18:29 -0500 | [diff] [blame] | 97 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 98 | if (length(kable_xml_style) != 0) { |
| 99 | xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ") |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 100 | } |
Hao Zhu | 2623412 | 2017-02-22 15:34:33 -0500 | [diff] [blame] | 101 | return(structure(as.character(kable_xml), format = "html", |
| 102 | class = "knitr_kable")) |
Hao Zhu | e10cfd3 | 2017-02-21 16:41:14 -0500 | [diff] [blame] | 103 | } |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame^] | 104 | |
| 105 | # LaTeX table style |
| 106 | pdfTable_styling <- function(kable_input, |
| 107 | full_width = T, |
| 108 | float = c("center", "left", "right"), |
| 109 | font_size = NULL, |
| 110 | latex_hold_position = F, |
| 111 | latex_scale_down = F) { |
| 112 | |
| 113 | } |