blob: 409ea0c76163b75e138d1cdfa5b3cb3bb525dc5a [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 Zhuec7ab922017-08-19 22:56:44 -040019#' @param color A character string for column text color.
20#' @param background A character string for column background color.
Hao Zhu78e61222017-05-24 20:53:35 -040021#'
22#' @examples x <- knitr::kable(head(mtcars), "html")
23#' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE)
24#'
Hao Zhubff01912017-05-23 18:05:00 -040025#' @export
Hao Zhuf13a35e2017-05-24 00:55:43 -040026column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040027 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuec7ab922017-08-19 22:56:44 -040028 monospace = FALSE, color = NULL, background = NULL,
29 border_left = FALSE, border_right = FALSE) {
Hao Zhubff01912017-05-23 18:05:00 -040030 if (!is.numeric(column)) {
31 stop("column must be a numeric value")
32 }
33 kable_format <- attr(kable_input, "format")
34 if (!kable_format %in% c("html", "latex")) {
35 message("Currently generic markdown table using pandoc is not supported.")
36 return(kable_input)
37 }
38 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040039 return(column_spec_html(kable_input, column, width,
40 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040041 color, background,
42 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040043 }
44 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040045 return(column_spec_latex(kable_input, column, width,
46 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040047 color, background,
48 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040049 }
50}
51
Hao Zhu669bcd22017-08-19 14:53:40 -040052column_spec_html <- function(kable_input, column, width,
53 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040054 color, background,
55 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -040056 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040057 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040058 kable_tbody <- xml_tpart(kable_xml, "tbody")
59
60 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu32f43f72017-06-20 18:24:54 -040061 if (is.null(kable_attrs$column_adjust)) {
62 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
63 all_contents_array <- rep(column, length(all_contents_rows))
64 } else {
65 column <- column + kable_attrs$column_adjust$count
66 all_contents_array <- colSums(kable_attrs$column_adjust$matrix[1:column, ])
67 all_contents_rows <- which(all_contents_array != 0 &
68 kable_attrs$column_adjust$matrix[column, ])
69 }
70
Hao Zhubff01912017-05-23 18:05:00 -040071 if (!is.null(group_header_rows)) {
72 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
73 group_header_rows]
74 }
75
Hao Zhuec7ab922017-08-19 22:56:44 -040076 # Border css
77 border_l_css <- "1px solid"
78 border_r_css <- "1px solid"
79 if (is.character(border_left)) {
80 border_l_css <- border_left
81 border_left <- T
82 }
83 if (is.character(border_right)) {
84 border_r_css <- border_right
85 border_right <- T
86 }
87
Hao Zhubff01912017-05-23 18:05:00 -040088 for (i in all_contents_rows) {
Hao Zhu32f43f72017-06-20 18:24:54 -040089 target_cell <- xml_child(xml_child(kable_tbody, i), all_contents_array[i])
Hao Zhubff01912017-05-23 18:05:00 -040090 if (!is.null(width)) {
91 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhua73601b2017-08-19 15:31:51 -040092 "width: ", width, "; ")
Hao Zhubff01912017-05-23 18:05:00 -040093 }
94 if (bold) {
95 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
96 "font-weight: bold;")
97 }
98 if (italic) {
99 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
100 "font-style: italic;")
101 }
Hao Zhu8f202992017-07-15 02:20:18 -0400102 if (monospace) {
103 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
104 "font-family: monospace;")
105 }
Hao Zhu669bcd22017-08-19 14:53:40 -0400106 if (!is.null(color)) {
107 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
108 "color: ", color, ";")
109 }
110 if (!is.null(background)) {
111 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
112 "background-color: ",
113 background, ";")
114 }
Hao Zhuec7ab922017-08-19 22:56:44 -0400115 if (border_left) {
116 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
117 "border-left:", border_l_css, ";")
118 }
119 if (border_right) {
120 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
121 "border-right:", border_r_css, ";")
122 }
Hao Zhubff01912017-05-23 18:05:00 -0400123 }
Hao Zhuf2dfd142017-07-24 14:43:28 -0400124 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400125 attributes(out) <- kable_attrs
126 return(out)
127}
128
Hao Zhua73601b2017-08-19 15:31:51 -0400129column_spec_latex <- function(kable_input, column, width,
130 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400131 color, background,
132 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -0400133 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000134 if (!is.null(table_info$collapse_rows)) {
135 message("Usually it is recommended to use column_spec before collapse_rows,",
136 " especially in LaTeX, to get a desired result. ")
137 }
Hao Zhubff01912017-05-23 18:05:00 -0400138 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
139 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
140
Hao Zhu32f43f72017-06-20 18:24:54 -0400141 table_info$align_vector[column] <- latex_column_align_builder(
Hao Zhua73601b2017-08-19 15:31:51 -0400142 table_info$align_vector[column], width, bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400143 color, background, border_left, border_right)
Hao Zhubff01912017-05-23 18:05:00 -0400144
145 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
146
Hao Zhud2c0f732017-08-26 10:40:14 -0400147 out <- sub(kable_align_old, kable_align_new,
148 enc2utf8(as.character(kable_input)),
Hao Zhubff01912017-05-23 18:05:00 -0400149 perl = T)
150 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000151 if (!is.null(width)) {
152 if (is.null(table_info$column_width)) {
153 table_info$column_width <- list()
154 }
155 table_info$column_width[[paste0("column_", column)]] <- width
156 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400157 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400158 return(out)
159}
Hao Zhu32f43f72017-06-20 18:24:54 -0400160
Hao Zhua73601b2017-08-19 15:31:51 -0400161latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400162 color, background,
163 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400164 extra_align <- ""
165 if (!is.null(width)) {
166 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400167 "l" = "\\\\raggedright\\\\arraybackslash",
168 "c" = "\\\\centering\\\\arraybackslash",
169 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400170 x <- paste0("p\\{", width, "\\}")
171 }
172
Hao Zhua73601b2017-08-19 15:31:51 -0400173 if (!is.null(color)) {
174 color <- sprintf("\\\\color{%s}", color)
Hao Zhu32f43f72017-06-20 18:24:54 -0400175 }
176
Hao Zhuec7ab922017-08-19 22:56:44 -0400177 if (!is.null(color)) {
178 background <- sprintf("\\\\columncolor{%s}", background)
179 }
180
Hao Zhua73601b2017-08-19 15:31:51 -0400181 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
182 c(bold, italic, monospace)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400183 latex_array_options <- c(latex_array_options, extra_align,
184 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400185 latex_array_options <- paste0(
186 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
187 )
188 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400189 if (border_left) {
190 x <- paste0("|", x)
191 }
192 if (border_right) {
193 x <- paste0(x, "|")
194 }
Hao Zhua73601b2017-08-19 15:31:51 -0400195
Hao Zhu32f43f72017-06-20 18:24:54 -0400196 return(x)
197}