blob: 0c383d566d571b605df5be845477f271cae4208c [file] [log] [blame]
Hao Zhubff01912017-05-23 18:05:00 -04001#' Specify the look of the selected column
2#'
3#' @description This function allows users to select a column and then specify
4#' its look. Right now it supports the following three properties: column width,
5#' bold text and italic text.
6#'
7#' @param kable_input Output of `knitr::kable()` with `format` specified
8#' @param column A numeric value indicating which column to be selected
9#' @param width A character string telling HTML & LaTeX how wide the column
10#' needs to be, e.g. "10cm", "3in" or "30em".
11#' @param bold A T/F value to control whether the text of the selected column
12#' need to be bolded.
13#' @param italic A T/F value to control whether the text of the selected column
14#' need to be emphasized.
Hao Zhu78e61222017-05-24 20:53:35 -040015#'
16#' @examples x <- knitr::kable(head(mtcars), "html")
17#' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE)
18#'
Hao Zhubff01912017-05-23 18:05:00 -040019#' @export
Hao Zhuf13a35e2017-05-24 00:55:43 -040020column_spec <- function(kable_input, column,
Hao Zhu78e61222017-05-24 20:53:35 -040021 width = NULL, bold = FALSE, italic = FALSE) {
Hao Zhubff01912017-05-23 18:05:00 -040022 if (!is.numeric(column)) {
23 stop("column must be a numeric value")
24 }
25 kable_format <- attr(kable_input, "format")
26 if (!kable_format %in% c("html", "latex")) {
27 message("Currently generic markdown table using pandoc is not supported.")
28 return(kable_input)
29 }
30 if (kable_format == "html") {
31 return(column_spec_html(kable_input, column, width, bold, italic))
32 }
33 if (kable_format == "latex") {
34 return(column_spec_latex(kable_input, column, width, bold, italic))
35 }
36}
37
38column_spec_html <- function(kable_input, column, width, bold, italic) {
39 kable_attrs <- attributes(kable_input)
40 kable_xml <- read_xml(as.character(kable_input), options = "COMPACT")
41 kable_tbody <- xml_tpart(kable_xml, "tbody")
42
43 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhuf13a35e2017-05-24 00:55:43 -040044 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhubff01912017-05-23 18:05:00 -040045 if (!is.null(group_header_rows)) {
46 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
47 group_header_rows]
48 }
49
50 for (i in all_contents_rows) {
51 target_cell <- xml_child(xml_child(kable_tbody, i), column)
52 if (!is.null(width)) {
53 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
54 "width: ", width, "; ")
55 }
56 if (bold) {
57 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
58 "font-weight: bold;")
59 }
60 if (italic) {
61 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
62 "font-style: italic;")
63 }
64 }
65 out <- structure(as.character(kable_xml), format = "html",
66 class = "knitr_kable")
67 attributes(out) <- kable_attrs
68 return(out)
69}
70
71column_spec_latex <- function(kable_input, column, width, bold, italic) {
72 table_info <- magic_mirror(kable_input)
73 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
74 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
75
76 if (!is.null(width)) {
77 table_info$align_vector[column] <- paste0("p\\{", width, "\\}")
78 }
79
80 if (bold | italic) {
81 usepackage_latex("array")
82 latex_array_options <- c("\\\\bfseries", "\\\\em")[c(bold, italic)]
83 latex_array_options <- paste0(
84 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
85 )
86 table_info$align_vector[column] <- paste0(latex_array_options,
87 table_info$align_vector[column])
88 }
89
90 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
91
92 out <- sub(kable_align_old, kable_align_new, as.character(kable_input),
93 perl = T)
94 out <- structure(out, format = "latex", class = "knitr_kable")
95 attr(out, "original_kable_meta") <- table_info
96 return(out)
97}