blob: 7af74315d12b0f659470d93fb55c83f53a02f4a0 [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
Hao Zhubf5bfe22017-06-21 14:37:41 -04008#' @param column A numeric value indicating which column to be selected. When
9#' you do the counting, ignore the extra header columns you added through
10#' add_header_left.
Hao Zhubff01912017-05-23 18:05:00 -040011#' @param width A character string telling HTML & LaTeX how wide the column
12#' needs to be, e.g. "10cm", "3in" or "30em".
13#' @param bold A T/F value to control whether the text of the selected column
14#' need to be bolded.
15#' @param italic A T/F value to control whether the text of the selected column
16#' need to be emphasized.
Hao Zhu8f202992017-07-15 02:20:18 -040017#' @param monospace A T/F value to control whether the text of the selected column
18#' need to be monospaced (verbatim)
Hao Zhu78e61222017-05-24 20:53:35 -040019#'
20#' @examples x <- knitr::kable(head(mtcars), "html")
21#' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE)
22#'
Hao Zhubff01912017-05-23 18:05:00 -040023#' @export
Hao Zhuf13a35e2017-05-24 00:55:43 -040024column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040025 width = NULL, bold = FALSE, italic = FALSE,
26 monospace = FALSE) {
Hao Zhubff01912017-05-23 18:05:00 -040027 if (!is.numeric(column)) {
28 stop("column must be a numeric value")
29 }
30 kable_format <- attr(kable_input, "format")
31 if (!kable_format %in% c("html", "latex")) {
32 message("Currently generic markdown table using pandoc is not supported.")
33 return(kable_input)
34 }
35 if (kable_format == "html") {
Hao Zhu8f202992017-07-15 02:20:18 -040036 return(column_spec_html(kable_input, column, width, bold, italic, monospace))
Hao Zhubff01912017-05-23 18:05:00 -040037 }
38 if (kable_format == "latex") {
Hao Zhu8f202992017-07-15 02:20:18 -040039 return(column_spec_latex(kable_input, column, width, bold, italic, monospace))
Hao Zhubff01912017-05-23 18:05:00 -040040 }
41}
42
Hao Zhu8f202992017-07-15 02:20:18 -040043column_spec_html <- function(kable_input, column, width, bold, italic, monospace) {
Hao Zhubff01912017-05-23 18:05:00 -040044 kable_attrs <- attributes(kable_input)
45 kable_xml <- read_xml(as.character(kable_input), options = "COMPACT")
46 kable_tbody <- xml_tpart(kable_xml, "tbody")
47
48 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu32f43f72017-06-20 18:24:54 -040049 if (is.null(kable_attrs$column_adjust)) {
50 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
51 all_contents_array <- rep(column, length(all_contents_rows))
52 } else {
53 column <- column + kable_attrs$column_adjust$count
54 all_contents_array <- colSums(kable_attrs$column_adjust$matrix[1:column, ])
55 all_contents_rows <- which(all_contents_array != 0 &
56 kable_attrs$column_adjust$matrix[column, ])
57 }
58
Hao Zhubff01912017-05-23 18:05:00 -040059 if (!is.null(group_header_rows)) {
60 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
61 group_header_rows]
62 }
63
64 for (i in all_contents_rows) {
Hao Zhu32f43f72017-06-20 18:24:54 -040065 target_cell <- xml_child(xml_child(kable_tbody, i), all_contents_array[i])
Hao Zhubff01912017-05-23 18:05:00 -040066 if (!is.null(width)) {
67 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
68 "width: ", width, "; ")
69 }
70 if (bold) {
71 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
72 "font-weight: bold;")
73 }
74 if (italic) {
75 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
76 "font-style: italic;")
77 }
Hao Zhu8f202992017-07-15 02:20:18 -040078 if (monospace) {
79 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
80 "font-family: monospace;")
81 }
Hao Zhubff01912017-05-23 18:05:00 -040082 }
Hao Zhuf2dfd142017-07-24 14:43:28 -040083 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -040084 attributes(out) <- kable_attrs
85 return(out)
86}
87
Hao Zhu8f202992017-07-15 02:20:18 -040088column_spec_latex <- function(kable_input, column, width, bold, italic, monospace) {
Hao Zhubff01912017-05-23 18:05:00 -040089 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -100090 if (!is.null(table_info$collapse_rows)) {
91 message("Usually it is recommended to use column_spec before collapse_rows,",
92 " especially in LaTeX, to get a desired result. ")
93 }
Hao Zhubff01912017-05-23 18:05:00 -040094 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
95 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
96
Hao Zhu32f43f72017-06-20 18:24:54 -040097 table_info$align_vector[column] <- latex_column_align_builder(
Hao Zhu8f202992017-07-15 02:20:18 -040098 table_info$align_vector[column], width, bold, italic, monospace)
Hao Zhubff01912017-05-23 18:05:00 -040099
100 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
101
102 out <- sub(kable_align_old, kable_align_new, as.character(kable_input),
103 perl = T)
104 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000105 if (!is.null(width)) {
106 if (is.null(table_info$column_width)) {
107 table_info$column_width <- list()
108 }
109 table_info$column_width[[paste0("column_", column)]] <- width
110 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400111 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400112 return(out)
113}
Hao Zhu32f43f72017-06-20 18:24:54 -0400114
Hao Zhu8f202992017-07-15 02:20:18 -0400115latex_column_align_builder <- function(x, width, bold, italic, monospace) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400116 extra_align <- ""
117 if (!is.null(width)) {
118 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400119 "l" = "\\\\raggedright\\\\arraybackslash",
120 "c" = "\\\\centering\\\\arraybackslash",
121 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400122 x <- paste0("p\\{", width, "\\}")
123 }
124
Hao Zhu8f202992017-07-15 02:20:18 -0400125 if (bold | italic | monospace | extra_align != "") {
126 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
127 c(bold, italic, monospace)]
Hao Zhu32f43f72017-06-20 18:24:54 -0400128 latex_array_options <- c(latex_array_options, extra_align)
129 latex_array_options <- paste0(
130 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
131 )
132 x <- paste0(latex_array_options, x)
133 }
134
135 return(x)
136}