blob: 2d4d1482ac8c99e4346303baca555a67acd9c759 [file] [log] [blame]
Hao Zhue10cfd32017-02-21 16:41:14 -05001#' HTML table attributes
2#'
Hao Zhu26234122017-02-22 15:34:33 -05003#' @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 Zhue10cfd32017-02-21 16:41:14 -05006#'
Hao Zhu94956582017-02-21 18:18:29 -05007#' @param bootstrap_options A character vector for bootstrap table options. For
8#' detailed information, please check the package vignette or visit the
Hao Zhu59f5fe02017-02-22 11:27:14 -05009#' w3schools' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page}
Hao Zhu26234122017-02-22 15:34:33 -050010#' . Possible options include "basic", "striped", "bordered", "hover",
Hao Zhu94956582017-02-21 18:18:29 -050011#' "condensed" and "responsive".
Hao Zhu59f5fe02017-02-22 11:27:14 -050012#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhu94956582017-02-21 18:18:29 -050013#' table should have 100\% width.
Hao Zhuc05e1812017-02-25 01:45:35 -050014#' @param position A character string determining whether and how the HTML table
Hao Zhu59f5fe02017-02-22 11:27:14 -050015#' should float on the page. Values could be "left", "center", "right"
Hao Zhu94956582017-02-21 18:18:29 -050016#' @param font_size A numeric input for table font size
Hao Zhue10cfd32017-02-21 16:41:14 -050017#'
18#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050019kable_styling <- function(kable_input,
20 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050021 latex_options = "basic",
22 full_width = NULL,
23 position = c("center", "left", "right",
24 "float_left", "float_right"),
25 font_size = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -050026 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") {
Hao Zhuc05e1812017-02-25 01:45:35 -050032 if (is.null(full_width)) full_width <- T
Hao Zhuc1f38412017-02-23 12:13:48 -050033 return(htmlTable_styling(kable_input,
34 bootstrap_options = bootstrap_options,
35 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050036 position = position,
Hao Zhuc1f38412017-02-23 12:13:48 -050037 font_size = font_size))
38 }
39 if (kable_format == "latex") {
Hao Zhuc05e1812017-02-25 01:45:35 -050040 if (is.null(full_width)) full_width <- F
41 return(pdfTable_styling(kable_input,
42 latex_options = latex_options,
43 full_width = full_width,
44 position = position,
45 font_size = font_size))
Hao Zhuc1f38412017-02-23 12:13:48 -050046 }
47}
48
49# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -050050htmlTable_styling <- function(kable_input,
51 bootstrap_options = "basic",
52 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -050053 position = c("center", "left", "right",
54 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -050055 font_size = NULL) {
Hao Zhuc05e1812017-02-25 01:45:35 -050056
Hao Zhu26234122017-02-22 15:34:33 -050057 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
58
59 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -050060 bootstrap_options <- match.arg(
61 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -050062 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -050063 several.ok = T
64 )
65
Hao Zhu26234122017-02-22 15:34:33 -050066 kable_xml_class <- NULL
67 if (xml_has_attr(kable_xml, "class")) {
68 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -050069 }
Hao Zhu26234122017-02-22 15:34:33 -050070 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
71 bootstrap_options <- "table"
72 } else {
73 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
74 bootstrap_options <- paste0("table-", bootstrap_options)
75 bootstrap_options <- c("table", bootstrap_options)
76 }
77 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
78 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -050079
Hao Zhu26234122017-02-22 15:34:33 -050080 # Modify style
81 kable_xml_style <- NULL
82 if (xml_has_attr(kable_xml, "style")) {
83 kable_xml_style <- xml_attr(kable_xml, "style")
84 }
Hao Zhue10cfd32017-02-21 16:41:14 -050085 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -050086 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -050087 paste0("font-size: ", font_size, "px;"))
Hao Zhue10cfd32017-02-21 16:41:14 -050088 }
89 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -050090 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -050091 }
Hao Zhu94956582017-02-21 18:18:29 -050092
Hao Zhuc05e1812017-02-25 01:45:35 -050093 position <- match.arg(position)
94 position_style <- switch(
95 position,
96 center = "margin-left: auto; margin-right: auto;",
97 left = "text-align: right;",
98 right = "margin-right: 0; margin-left: auto",
99 float_left = "float: left; margin-right: 10px;",
100 float_right = "float: right; margin-left: 10px;"
101 )
102 kable_xml_style <- c(kable_xml_style, position_style)
103
Hao Zhu26234122017-02-22 15:34:33 -0500104 if (length(kable_xml_style) != 0) {
105 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500106 }
Hao Zhu26234122017-02-22 15:34:33 -0500107 return(structure(as.character(kable_xml), format = "html",
108 class = "knitr_kable"))
Hao Zhue10cfd32017-02-21 16:41:14 -0500109}
Hao Zhuc1f38412017-02-23 12:13:48 -0500110
111# LaTeX table style
112pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500113 latex_options = "basic",
114 full_width = F,
115 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500116 "float_left", "float_right"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500117 font_size = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500118
Hao Zhuc05e1812017-02-25 01:45:35 -0500119 latex_options <- match.arg(
120 latex_options,
121 c("basic", "striped", "hold_position", "scale_down"),
122 several.ok = T
123 )
124
Hao Zhua3fc0c42017-02-27 12:04:59 -0500125 out <- NULL
Hao Zhuc05e1812017-02-25 01:45:35 -0500126 out <- as.character(kable_input)
127 table_info <- magic_mirror(kable_input)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500128 table_info$valign2 <- sub("\\[", "\\\\[", table_info$valign)
129 table_info$valign2 <- sub("\\]", "\\\\]", table_info$valign2)
130 table_info$valign3 <- sub("\\[", "", table_info$valign)
131 table_info$valign3 <- sub("\\]", "", table_info$valign3)
132 table_info$begin_tabular <- paste0("\\\\begin\\{", table_info$tabular, "\\}",
133 table_info$valign2)
134 table_info$end_tabular <- paste0("\\\\end\\{", table_info$tabular, "\\}")
Hao Zhuc05e1812017-02-25 01:45:35 -0500135
136
137 if ("striped" %in% latex_options) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500138 out <- styling_latex_striped(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500139 }
140
141 # hold_position is only meaningful in a table environment
142 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500143 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500144 }
145
Hao Zhua3fc0c42017-02-27 12:04:59 -0500146 if ("scale_down" %in% latex_options) {
147 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500148 }
149
150 if (full_width) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500151 out <- styling_latex_full_width(out, table_info)
152 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500153
Hao Zhua3fc0c42017-02-27 12:04:59 -0500154 if (!is.null(font_size)) {
155 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500156 }
157
158 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500159 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500160
161 out <- structure(out, format = "latex", class = "knitr_kable")
162 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500163}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500164
165styling_latex_striped <- function(x) {
166 usepackage_latex("xcolor", "table")
167 paste0(
168 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
169 "\\rowcolors{2}{gray!6}{white}\n", x, "\n\\rowcolors{2}{white}{white}")
170}
171
172styling_latex_hold_position <- function(x) {
173 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
174}
175
176styling_latex_scale_down <- function(x, table_info) {
177 # You cannot put longtable in a resizebox
178 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
179 if (table_info$tabular == "longtable") {
180 warning("Longtable cannot be resized.")
181 return(x)
182 }
183 x <- sub(table_info$begin_tabular,
184 paste0("\\\\resizebox\\{\\\\textwidth\\}\\{\\!\\}\\{",
185 table_info$begin_tabular),
186 x)
187 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
188}
189
190styling_latex_full_width <- function(x, table_info) {
191 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
192 col_max_length <- apply(size_matrix, 1, max) + 4
193 col_ratio <- round(col_max_length / sum(col_max_length), 2)
194 col_align <- paste0("p{\\\\dimexpr", col_ratio,
195 "\\\\linewidth-2\\\\tabcolsep}")
196 col_align <- paste0("{", paste(col_align, collapse = ""), "}")
197 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
198 table_info$begin_tabular, x)
199 sub(table_info$begin_tabular,
200 paste0(table_info$begin_tabular, col_align), x)
201}
202
203styling_latex_position <- function(x, table_info, position, latex_options) {
204 hold_position <- "hold_position" %in% latex_options
205 switch(
206 position,
207 center = styling_latex_position_center(x, table_info, hold_position),
208 left = styling_latex_position_left(x, table_info),
209 right = styling_latex_position_right(x, table_info, hold_position),
210 float_left = styling_latex_position_float(x, table_info, "l"),
211 float_right = styling_latex_position_float(x, table_info, "r")
212 )
213}
214
215styling_latex_position_center <- function(x, table_info, hold_position) {
216 if (!table_info$table_env & table_info$tabular == "tabular") {
217 table_env_setup <- "\\begin{table}"
218 if (hold_position) {
219 table_env_setup <- paste0(table_env_setup, "[!h]")
220 }
221 return(paste0(table_env_setup, "\n\\centering", x, "\n\\end{table}"))
222 }
223 return(x)
224}
225
226styling_latex_position_left <- function(x, table_info) {
227 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
228 longtable_option <- "\\[l\\]"
229 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
230 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
231}
232
233styling_latex_position_right <- function(x, table_info, hold_position) {
234 warning("Position = right is only supported for longtable in LaTeX. ",
235 "Setting back to center...")
236 styling_latex_position_center(x, table_info, hold_position)
237}
238
239styling_latex_position_float <- function(x, table_info, option) {
240 if (table_info$tabular == "longtable") {
241 warning("wraptable is not supported for longtable.")
242 if (option == "l") return(styling_latex_position_left(x, table_info))
243 if (option == "r") return(styling_latex_position_right(x, table_info, F))
244 }
245 if (table_info$table_env) {
246 usepackage_latex("wrapfig")
247 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
248 col_max_length <- apply(size_matrix, 1, max) + 4
249 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
250 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
251 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
252 x <- sub("\\\\begin\\{table\\}", option, x)
253 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
254 return(x)
255 }
256}
257
258styling_latex_font_size <- function(x, table_info, font_size) {
259 row_height <- font_size + 2
260 if (table_info$tabular == "tabular" & table_info$table_env) {
261 return(sub(table_info$begin_tabular,
262 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
263 "\\}\\\\selectfont\n", table_info$begin_tabular),
264 x))
265 }
266 # For longtable and tabular without table environment. Simple wrap around
267 # fontsize is good enough
268 return(paste0(
269 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
270 "\\endgroup"
271 ))
272}