blob: 76df91b33c963676cc48a92fb3bb1af884f3c3be [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 Zhu53e240f2017-09-04 20:04:29 -040019#' @param color A character string for column text color. Here please pay
20#' attention to the differences in color codes between HTML and LaTeX.
21#' @param background A character string for column background color. Here please
22#' pay attention to the differences in color codes between HTML and LaTeX.
23#' @param border_left A logical variable indicating whether there should be a
24#' border line on the left of the selected column. In HTML, you can also pass
25#' in a character string for the CSS of the border line
26#' @param border_right A logical variable indicating whether there should be a
27#' border line on the right of the selected column. In HTML, you can also pass
28#' in a character string for the CSS of the border line
Hao Zhu78e61222017-05-24 20:53:35 -040029#'
30#' @examples x <- knitr::kable(head(mtcars), "html")
31#' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE)
32#'
Hao Zhubff01912017-05-23 18:05:00 -040033#' @export
Hao Zhuf13a35e2017-05-24 00:55:43 -040034column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040035 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuec7ab922017-08-19 22:56:44 -040036 monospace = FALSE, color = NULL, background = NULL,
37 border_left = FALSE, border_right = FALSE) {
Hao Zhubff01912017-05-23 18:05:00 -040038 if (!is.numeric(column)) {
39 stop("column must be a numeric value")
40 }
41 kable_format <- attr(kable_input, "format")
42 if (!kable_format %in% c("html", "latex")) {
43 message("Currently generic markdown table using pandoc is not supported.")
44 return(kable_input)
45 }
46 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040047 return(column_spec_html(kable_input, column, width,
48 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040049 color, background,
50 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040051 }
52 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040053 return(column_spec_latex(kable_input, column, width,
54 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040055 color, background,
56 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040057 }
58}
59
Hao Zhu669bcd22017-08-19 14:53:40 -040060column_spec_html <- function(kable_input, column, width,
61 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040062 color, background,
63 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -040064 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040065 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040066 kable_tbody <- xml_tpart(kable_xml, "tbody")
67
68 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu32f43f72017-06-20 18:24:54 -040069 if (is.null(kable_attrs$column_adjust)) {
70 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
71 all_contents_array <- rep(column, length(all_contents_rows))
72 } else {
73 column <- column + kable_attrs$column_adjust$count
74 all_contents_array <- colSums(kable_attrs$column_adjust$matrix[1:column, ])
75 all_contents_rows <- which(all_contents_array != 0 &
76 kable_attrs$column_adjust$matrix[column, ])
77 }
78
Hao Zhubff01912017-05-23 18:05:00 -040079 if (!is.null(group_header_rows)) {
80 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
81 group_header_rows]
82 }
83
Hao Zhuec7ab922017-08-19 22:56:44 -040084 # Border css
85 border_l_css <- "1px solid"
86 border_r_css <- "1px solid"
87 if (is.character(border_left)) {
88 border_l_css <- border_left
89 border_left <- T
90 }
91 if (is.character(border_right)) {
92 border_r_css <- border_right
93 border_right <- T
94 }
95
Hao Zhubff01912017-05-23 18:05:00 -040096 for (i in all_contents_rows) {
Hao Zhu32f43f72017-06-20 18:24:54 -040097 target_cell <- xml_child(xml_child(kable_tbody, i), all_contents_array[i])
Hao Zhubff01912017-05-23 18:05:00 -040098 if (!is.null(width)) {
99 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhua73601b2017-08-19 15:31:51 -0400100 "width: ", width, "; ")
Hao Zhubff01912017-05-23 18:05:00 -0400101 }
102 if (bold) {
103 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
104 "font-weight: bold;")
105 }
106 if (italic) {
107 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
108 "font-style: italic;")
109 }
Hao Zhu8f202992017-07-15 02:20:18 -0400110 if (monospace) {
111 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
112 "font-family: monospace;")
113 }
Hao Zhu669bcd22017-08-19 14:53:40 -0400114 if (!is.null(color)) {
115 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
116 "color: ", color, ";")
117 }
118 if (!is.null(background)) {
119 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
120 "background-color: ",
121 background, ";")
122 }
Hao Zhuec7ab922017-08-19 22:56:44 -0400123 if (border_left) {
124 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
125 "border-left:", border_l_css, ";")
126 }
127 if (border_right) {
128 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
129 "border-right:", border_r_css, ";")
130 }
Hao Zhubff01912017-05-23 18:05:00 -0400131 }
Hao Zhuf2dfd142017-07-24 14:43:28 -0400132 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400133 attributes(out) <- kable_attrs
134 return(out)
135}
136
Hao Zhua73601b2017-08-19 15:31:51 -0400137column_spec_latex <- function(kable_input, column, width,
138 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400139 color, background,
140 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -0400141 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000142 if (!is.null(table_info$collapse_rows)) {
143 message("Usually it is recommended to use column_spec before collapse_rows,",
144 " especially in LaTeX, to get a desired result. ")
145 }
Hao Zhubff01912017-05-23 18:05:00 -0400146 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
147 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
148
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400149 table_info$align_vector[column] <- unlist(lapply(
150 table_info$align_vector_origin[column],
151 function(x) {
152 latex_column_align_builder(
153 x, width, bold, italic, monospace,
154 color, background, border_left, border_right)
155 }
156 ))
Hao Zhubff01912017-05-23 18:05:00 -0400157
158 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
159
Hao Zhud2c0f732017-08-26 10:40:14 -0400160 out <- sub(kable_align_old, kable_align_new,
161 enc2utf8(as.character(kable_input)),
Hao Zhubff01912017-05-23 18:05:00 -0400162 perl = T)
163 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000164 if (!is.null(width)) {
165 if (is.null(table_info$column_width)) {
166 table_info$column_width <- list()
167 }
168 table_info$column_width[[paste0("column_", column)]] <- width
169 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400170 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400171 return(out)
172}
Hao Zhu32f43f72017-06-20 18:24:54 -0400173
Hao Zhua73601b2017-08-19 15:31:51 -0400174latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400175 color, background,
176 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400177 extra_align <- ""
178 if (!is.null(width)) {
179 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400180 "l" = "\\\\raggedright\\\\arraybackslash",
181 "c" = "\\\\centering\\\\arraybackslash",
182 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400183 x <- paste0("p\\{", width, "\\}")
184 }
185
Hao Zhua73601b2017-08-19 15:31:51 -0400186 if (!is.null(color)) {
187 color <- sprintf("\\\\color{%s}", color)
Hao Zhu32f43f72017-06-20 18:24:54 -0400188 }
189
Hao Zhuec7ab922017-08-19 22:56:44 -0400190 if (!is.null(color)) {
191 background <- sprintf("\\\\columncolor{%s}", background)
192 }
193
Hao Zhua73601b2017-08-19 15:31:51 -0400194 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
195 c(bold, italic, monospace)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400196 latex_array_options <- c(latex_array_options, extra_align,
197 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400198 latex_array_options <- paste0(
199 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
200 )
201 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400202 if (border_left) {
203 x <- paste0("|", x)
204 }
205 if (border_right) {
206 x <- paste0(x, "|")
207 }
Hao Zhua73601b2017-08-19 15:31:51 -0400208
Hao Zhu32f43f72017-06-20 18:24:54 -0400209 return(x)
210}