blob: 33027a0119bf71ca35943d432689e72d0bdd5cc8 [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 Zhuf7994dd2017-02-27 16:58:42 -05007#' @param kable_input Output of `knitr::kable()` with `format` specified
8#' @param bootstrap_options A character vector for bootstrap table options.
9#' Please see package documentation site or visit the w3schools'
10#' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page}
11#' for more information. Possible options include `basic`, `striped`,
12#' `bordered`, `hover`, `condensed` and `responsive`.
13#' @param latex_options A character vector for LaTeX table options. Please see
14#' package documentation site for more information. Possible options include
15#' `basic`, `striped`, `hold_position`, `scale_down`. `striped` will add
16#' alternative row colors to the table. It will imports `LaTeX` package `xcolor`
17#' if enabled. `hold_position` will "hold" the floating table to the exact
18#' position. It is useful when the `LaTeX` table is contained in a `table`
19#' environment after you specified captions in `kable()`. It will force the
20#' table to stay in the position where it was created in the document.
21#' `scale_down` is useful for super wide table. It will automatically adjust
22#' the table to page width.
Hao Zhu59f5fe02017-02-22 11:27:14 -050023#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
Hao Zhuf7994dd2017-02-27 16:58:42 -050024#' table should have 100\% width. Since HTML and pdf have different flavors on
25#' the preferable format for `full_width`. If not specified, a HTML table will
26#' have full width by default but this option will be set to `FALSE` for a
27#' LaTeX table
28#' @param position A character string determining how to position the table
29#' on a page. Possible values include `left`, `center`, `right`, `float_left`
30#' and `float_right`. Please see the package doc site for demonstrations. For
31#' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be
32#' imported.
Hao Zhu94956582017-02-21 18:18:29 -050033#' @param font_size A numeric input for table font size
Hao Zhue10cfd32017-02-21 16:41:14 -050034#'
35#' @export
Hao Zhuc1f38412017-02-23 12:13:48 -050036kable_styling <- function(kable_input,
37 bootstrap_options = "basic",
Hao Zhuc05e1812017-02-25 01:45:35 -050038 latex_options = "basic",
39 full_width = NULL,
40 position = c("center", "left", "right",
41 "float_left", "float_right"),
42 font_size = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -050043 kable_format <- attr(kable_input, "format")
44 if (!kable_format %in% c("html", "latex")) {
45 stop("Please specify output format in your kable function. Currently ",
46 "generic markdown table using pandoc is not supported.")
47 }
48 if (kable_format == "html") {
Hao Zhuc05e1812017-02-25 01:45:35 -050049 if (is.null(full_width)) full_width <- T
Hao Zhuc1f38412017-02-23 12:13:48 -050050 return(htmlTable_styling(kable_input,
51 bootstrap_options = bootstrap_options,
52 full_width = full_width,
Hao Zhuc05e1812017-02-25 01:45:35 -050053 position = position,
Hao Zhuc1f38412017-02-23 12:13:48 -050054 font_size = font_size))
55 }
56 if (kable_format == "latex") {
Hao Zhuc05e1812017-02-25 01:45:35 -050057 if (is.null(full_width)) full_width <- F
58 return(pdfTable_styling(kable_input,
59 latex_options = latex_options,
60 full_width = full_width,
61 position = position,
62 font_size = font_size))
Hao Zhuc1f38412017-02-23 12:13:48 -050063 }
64}
65
66# htmlTable Styling ------------
Hao Zhu26234122017-02-22 15:34:33 -050067htmlTable_styling <- function(kable_input,
68 bootstrap_options = "basic",
69 full_width = T,
Hao Zhuc05e1812017-02-25 01:45:35 -050070 position = c("center", "left", "right",
71 "float_left", "float_right"),
Hao Zhu26234122017-02-22 15:34:33 -050072 font_size = NULL) {
Hao Zhuc05e1812017-02-25 01:45:35 -050073
Hao Zhu26234122017-02-22 15:34:33 -050074 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
75
76 # Modify class
Hao Zhue10cfd32017-02-21 16:41:14 -050077 bootstrap_options <- match.arg(
78 bootstrap_options,
Hao Zhu26234122017-02-22 15:34:33 -050079 c("basic", "striped", "bordered", "hover", "condensed", "responsive"),
Hao Zhue10cfd32017-02-21 16:41:14 -050080 several.ok = T
81 )
82
Hao Zhu26234122017-02-22 15:34:33 -050083 kable_xml_class <- NULL
84 if (xml_has_attr(kable_xml, "class")) {
85 kable_xml_class <- xml_attr(kable_xml, "class")
Hao Zhue10cfd32017-02-21 16:41:14 -050086 }
Hao Zhu26234122017-02-22 15:34:33 -050087 if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
88 bootstrap_options <- "table"
89 } else {
90 bootstrap_options <- bootstrap_options[bootstrap_options != "basic"]
91 bootstrap_options <- paste0("table-", bootstrap_options)
92 bootstrap_options <- c("table", bootstrap_options)
93 }
94 xml_attr(kable_xml, "class") <- paste(c(kable_xml_class, bootstrap_options),
95 collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -050096
Hao Zhu26234122017-02-22 15:34:33 -050097 # Modify style
98 kable_xml_style <- NULL
99 if (xml_has_attr(kable_xml, "style")) {
100 kable_xml_style <- xml_attr(kable_xml, "style")
101 }
Hao Zhue10cfd32017-02-21 16:41:14 -0500102 if (!is.null(font_size)) {
Hao Zhu26234122017-02-22 15:34:33 -0500103 kable_xml_style <- c(kable_xml_style,
Hao Zhuc1f38412017-02-23 12:13:48 -0500104 paste0("font-size: ", font_size, "px;"))
Hao Zhue10cfd32017-02-21 16:41:14 -0500105 }
106 if (!full_width) {
Hao Zhu26234122017-02-22 15:34:33 -0500107 kable_xml_style <- c(kable_xml_style, "width: auto !important;")
Hao Zhue10cfd32017-02-21 16:41:14 -0500108 }
Hao Zhu94956582017-02-21 18:18:29 -0500109
Hao Zhuc05e1812017-02-25 01:45:35 -0500110 position <- match.arg(position)
111 position_style <- switch(
112 position,
113 center = "margin-left: auto; margin-right: auto;",
114 left = "text-align: right;",
115 right = "margin-right: 0; margin-left: auto",
116 float_left = "float: left; margin-right: 10px;",
117 float_right = "float: right; margin-left: 10px;"
118 )
119 kable_xml_style <- c(kable_xml_style, position_style)
120
Hao Zhu26234122017-02-22 15:34:33 -0500121 if (length(kable_xml_style) != 0) {
122 xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
Hao Zhue10cfd32017-02-21 16:41:14 -0500123 }
Hao Zhu26234122017-02-22 15:34:33 -0500124 return(structure(as.character(kable_xml), format = "html",
125 class = "knitr_kable"))
Hao Zhue10cfd32017-02-21 16:41:14 -0500126}
Hao Zhuc1f38412017-02-23 12:13:48 -0500127
128# LaTeX table style
129pdfTable_styling <- function(kable_input,
Hao Zhuc05e1812017-02-25 01:45:35 -0500130 latex_options = "basic",
131 full_width = F,
132 position = c("center", "left", "right",
Hao Zhua3fc0c42017-02-27 12:04:59 -0500133 "float_left", "float_right"),
Hao Zhuc05e1812017-02-25 01:45:35 -0500134 font_size = NULL) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500135
Hao Zhuc05e1812017-02-25 01:45:35 -0500136 latex_options <- match.arg(
137 latex_options,
138 c("basic", "striped", "hold_position", "scale_down"),
139 several.ok = T
140 )
141
Hao Zhua3fc0c42017-02-27 12:04:59 -0500142 out <- NULL
Hao Zhuc05e1812017-02-25 01:45:35 -0500143 out <- as.character(kable_input)
144 table_info <- magic_mirror(kable_input)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500145 table_info$valign2 <- sub("\\[", "\\\\[", table_info$valign)
146 table_info$valign2 <- sub("\\]", "\\\\]", table_info$valign2)
147 table_info$valign3 <- sub("\\[", "", table_info$valign)
148 table_info$valign3 <- sub("\\]", "", table_info$valign3)
149 table_info$begin_tabular <- paste0("\\\\begin\\{", table_info$tabular, "\\}",
150 table_info$valign2)
151 table_info$end_tabular <- paste0("\\\\end\\{", table_info$tabular, "\\}")
Hao Zhuc05e1812017-02-25 01:45:35 -0500152
153
154 if ("striped" %in% latex_options) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500155 out <- styling_latex_striped(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500156 }
157
158 # hold_position is only meaningful in a table environment
159 if ("hold_position" %in% latex_options & table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500160 out <- styling_latex_hold_position(out)
Hao Zhuc05e1812017-02-25 01:45:35 -0500161 }
162
Hao Zhua3fc0c42017-02-27 12:04:59 -0500163 if ("scale_down" %in% latex_options) {
164 out <- styling_latex_scale_down(out, table_info)
Hao Zhuc05e1812017-02-25 01:45:35 -0500165 }
166
167 if (full_width) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500168 out <- styling_latex_full_width(out, table_info)
169 }
Hao Zhuc05e1812017-02-25 01:45:35 -0500170
Hao Zhua3fc0c42017-02-27 12:04:59 -0500171 if (!is.null(font_size)) {
172 out <- styling_latex_font_size(out, table_info, font_size)
Hao Zhuc05e1812017-02-25 01:45:35 -0500173 }
174
175 position <- match.arg(position)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500176 out <- styling_latex_position(out, table_info, position, latex_options)
Hao Zhuc05e1812017-02-25 01:45:35 -0500177
178 out <- structure(out, format = "latex", class = "knitr_kable")
179 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500180}
Hao Zhua3fc0c42017-02-27 12:04:59 -0500181
182styling_latex_striped <- function(x) {
183 usepackage_latex("xcolor", "table")
184 paste0(
185 # gray!6 is the same as shadecolor ({RGB}{248, 248, 248}) in pdf_document
186 "\\rowcolors{2}{gray!6}{white}\n", x, "\n\\rowcolors{2}{white}{white}")
187}
188
189styling_latex_hold_position <- function(x) {
190 sub("\\\\begin\\{table\\}", "\\\\begin\\{table\\}[!h]", x)
191}
192
193styling_latex_scale_down <- function(x, table_info) {
194 # You cannot put longtable in a resizebox
195 # http://tex.stackexchange.com/questions/83457/how-to-resize-or-scale-a-longtable-revised
196 if (table_info$tabular == "longtable") {
197 warning("Longtable cannot be resized.")
198 return(x)
199 }
200 x <- sub(table_info$begin_tabular,
201 paste0("\\\\resizebox\\{\\\\textwidth\\}\\{\\!\\}\\{",
202 table_info$begin_tabular),
203 x)
204 sub(table_info$end_tabular, paste0(table_info$end_tabular, "\\}"), x)
205}
206
207styling_latex_full_width <- function(x, table_info) {
208 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
209 col_max_length <- apply(size_matrix, 1, max) + 4
210 col_ratio <- round(col_max_length / sum(col_max_length), 2)
211 col_align <- paste0("p{\\\\dimexpr", col_ratio,
212 "\\\\linewidth-2\\\\tabcolsep}")
213 col_align <- paste0("{", paste(col_align, collapse = ""), "}")
214 x <- sub(paste0(table_info$begin_tabular, "\\{[^\\\\n]*\\}"),
215 table_info$begin_tabular, x)
216 sub(table_info$begin_tabular,
217 paste0(table_info$begin_tabular, col_align), x)
218}
219
220styling_latex_position <- function(x, table_info, position, latex_options) {
221 hold_position <- "hold_position" %in% latex_options
222 switch(
223 position,
224 center = styling_latex_position_center(x, table_info, hold_position),
225 left = styling_latex_position_left(x, table_info),
226 right = styling_latex_position_right(x, table_info, hold_position),
227 float_left = styling_latex_position_float(x, table_info, "l"),
228 float_right = styling_latex_position_float(x, table_info, "r")
229 )
230}
231
232styling_latex_position_center <- function(x, table_info, hold_position) {
233 if (!table_info$table_env & table_info$tabular == "tabular") {
Hao Zhuf7994dd2017-02-27 16:58:42 -0500234 return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}"))
Hao Zhua3fc0c42017-02-27 12:04:59 -0500235 }
236 return(x)
237}
238
239styling_latex_position_left <- function(x, table_info) {
240 if (table_info$tabular != "longtable") return(sub("\\\\centering\\n", "", x))
241 longtable_option <- "\\[l\\]"
242 sub(paste0("\\\\begin\\{longtable\\}", table_info$valign2),
243 paste0("\\\\begin\\{longtable\\}", longtable_option), x)
244}
245
246styling_latex_position_right <- function(x, table_info, hold_position) {
247 warning("Position = right is only supported for longtable in LaTeX. ",
248 "Setting back to center...")
249 styling_latex_position_center(x, table_info, hold_position)
250}
251
252styling_latex_position_float <- function(x, table_info, option) {
253 if (table_info$tabular == "longtable") {
254 warning("wraptable is not supported for longtable.")
255 if (option == "l") return(styling_latex_position_left(x, table_info))
256 if (option == "r") return(styling_latex_position_right(x, table_info, F))
257 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500258 usepackage_latex("wrapfig")
259 size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
260 col_max_length <- apply(size_matrix, 1, max) + 4
Hao Zhua3fc0c42017-02-27 12:04:59 -0500261 if (table_info$table_env) {
Hao Zhua3fc0c42017-02-27 12:04:59 -0500262 option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
263 option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
264 x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
265 x <- sub("\\\\begin\\{table\\}", option, x)
266 x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
Hao Zhuf7994dd2017-02-27 16:58:42 -0500267 } else {
268 option <- sprintf("\\begin{wraptable}{%s}", option)
269 option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
270 x <- paste0(option, x, "\\end{wraptable}")
Hao Zhua3fc0c42017-02-27 12:04:59 -0500271 }
Hao Zhuf7994dd2017-02-27 16:58:42 -0500272 return(x)
Hao Zhua3fc0c42017-02-27 12:04:59 -0500273}
274
275styling_latex_font_size <- function(x, table_info, font_size) {
276 row_height <- font_size + 2
277 if (table_info$tabular == "tabular" & table_info$table_env) {
278 return(sub(table_info$begin_tabular,
279 paste0("\\\\fontsize\\{", font_size, "\\}\\{", row_height,
280 "\\}\\\\selectfont\n", table_info$begin_tabular),
281 x))
282 }
283 # For longtable and tabular without table environment. Simple wrap around
284 # fontsize is good enough
285 return(paste0(
286 "\\begingroup\\fontsize{", font_size, "}{", row_height, "}\\selectfont\n", x,
287 "\\endgroup"
288 ))
289}