blob: d4dd1ac9682ff93fa3e11e45566eb4088b1b644b [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 Zhu32f43f72017-06-20 18:24:54 -040044 if (is.null(kable_attrs$column_adjust)) {
45 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
46 all_contents_array <- rep(column, length(all_contents_rows))
47 } else {
48 column <- column + kable_attrs$column_adjust$count
49 all_contents_array <- colSums(kable_attrs$column_adjust$matrix[1:column, ])
50 all_contents_rows <- which(all_contents_array != 0 &
51 kable_attrs$column_adjust$matrix[column, ])
52 }
53
Hao Zhubff01912017-05-23 18:05:00 -040054 if (!is.null(group_header_rows)) {
55 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
56 group_header_rows]
57 }
58
59 for (i in all_contents_rows) {
Hao Zhu32f43f72017-06-20 18:24:54 -040060 target_cell <- xml_child(xml_child(kable_tbody, i), all_contents_array[i])
Hao Zhubff01912017-05-23 18:05:00 -040061 if (!is.null(width)) {
62 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
63 "width: ", width, "; ")
64 }
65 if (bold) {
66 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
67 "font-weight: bold;")
68 }
69 if (italic) {
70 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
71 "font-style: italic;")
72 }
73 }
74 out <- structure(as.character(kable_xml), format = "html",
75 class = "knitr_kable")
76 attributes(out) <- kable_attrs
77 return(out)
78}
79
80column_spec_latex <- function(kable_input, column, width, bold, italic) {
81 table_info <- magic_mirror(kable_input)
82 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
83 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
84
Hao Zhu32f43f72017-06-20 18:24:54 -040085 table_info$align_vector[column] <- latex_column_align_builder(
86 table_info$align_vector[column], width, bold, italic)
Hao Zhubff01912017-05-23 18:05:00 -040087
88 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
89
90 out <- sub(kable_align_old, kable_align_new, as.character(kable_input),
91 perl = T)
92 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -040093 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -040094 return(out)
95}
Hao Zhu32f43f72017-06-20 18:24:54 -040096
97latex_column_align_builder <- function(x, width, bold, italic) {
98 extra_align <- ""
99 if (!is.null(width)) {
100 extra_align <- switch(x,
101 "l" = "\\\\raggedright",
102 "c" = "\\\\centering",
103 "r" = "\\\\raggedleft")
104 x <- paste0("p\\{", width, "\\}")
105 }
106
107 if (bold | italic | extra_align != "") {
108 latex_array_options <- c("\\\\bfseries", "\\\\em")[c(bold, italic)]
109 latex_array_options <- c(latex_array_options, extra_align)
110 latex_array_options <- paste0(
111 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
112 )
113 x <- paste0(latex_array_options, x)
114 }
115
116 return(x)
117}