blob: 27c76b6635204ccf5d04cfb54c5e6848b3138b9f [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 Zhu9b43f622017-09-11 19:00:08 -04008#' @param columns A numeric value or vector indicating which column(s) to be selected.
Hao Zhubff01912017-05-23 18:05:00 -04009#' @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 Zhu8f202992017-07-15 02:20:18 -040015#' @param monospace A T/F value to control whether the text of the selected column
16#' need to be monospaced (verbatim)
Hao Zhu53e240f2017-09-04 20:04:29 -040017#' @param color A character string for column text color. Here please pay
18#' attention to the differences in color codes between HTML and LaTeX.
19#' @param background A character string for column background color. Here please
20#' pay attention to the differences in color codes between HTML and LaTeX.
21#' @param border_left A logical variable indicating whether there should be a
22#' border line on the left of the selected column. In HTML, you can also pass
23#' in a character string for the CSS of the border line
24#' @param border_right A logical variable indicating whether there should be a
25#' border line on the right of the selected column. In HTML, you can also pass
26#' in a character string for the CSS of the border line
Hao Zhu9b43f622017-09-11 19:00:08 -040027#' @param column Deprecating. Same with columns
Hao Zhu78e61222017-05-24 20:53:35 -040028#'
29#' @examples x <- knitr::kable(head(mtcars), "html")
30#' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE)
31#'
Hao Zhubff01912017-05-23 18:05:00 -040032#' @export
Hao Zhu9b43f622017-09-11 19:00:08 -040033column_spec <- function(kable_input, columns = NULL,
Hao Zhu8f202992017-07-15 02:20:18 -040034 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuec7ab922017-08-19 22:56:44 -040035 monospace = FALSE, color = NULL, background = NULL,
Hao Zhu9b43f622017-09-11 19:00:08 -040036 border_left = FALSE, border_right = FALSE,
37 column, ...) {
38 if (is.null(columns)) {
39 columns <- column
40 }
41 if (!is.numeric(columns)) {
42 stop("columns/column must be numeric. Note that column is deprecating.")
Hao Zhubff01912017-05-23 18:05:00 -040043 }
44 kable_format <- attr(kable_input, "format")
45 if (!kable_format %in% c("html", "latex")) {
46 message("Currently generic markdown table using pandoc is not supported.")
47 return(kable_input)
48 }
49 if (kable_format == "html") {
Hao Zhu9b43f622017-09-11 19:00:08 -040050 return(column_spec_html(kable_input, columns, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040051 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040052 color, background,
53 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040054 }
55 if (kable_format == "latex") {
Hao Zhu9b43f622017-09-11 19:00:08 -040056 return(column_spec_latex(kable_input, columns, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040057 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040058 color, background,
Hao Zhu9b43f622017-09-11 19:00:08 -040059 border_left, border_right, ...))
Hao Zhubff01912017-05-23 18:05:00 -040060 }
61}
62
Hao Zhu9b43f622017-09-11 19:00:08 -040063column_spec_html <- function(kable_input, columns, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040064 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040065 color, background,
66 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -040067 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040068 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040069 kable_tbody <- xml_tpart(kable_xml, "tbody")
70
71 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -040072 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -040073
Hao Zhubff01912017-05-23 18:05:00 -040074 if (!is.null(group_header_rows)) {
75 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
76 group_header_rows]
77 }
78
Hao Zhuec7ab922017-08-19 22:56:44 -040079 # Border css
80 border_l_css <- "1px solid"
81 border_r_css <- "1px solid"
82 if (is.character(border_left)) {
83 border_l_css <- border_left
84 border_left <- T
85 }
86 if (is.character(border_right)) {
87 border_r_css <- border_right
88 border_right <- T
89 }
90
Hao Zhubff01912017-05-23 18:05:00 -040091 for (i in all_contents_rows) {
Hao Zhu9b43f622017-09-11 19:00:08 -040092 for (j in columns) {
93 target_cell <- xml_child(xml_child(kable_tbody, i), j)
94 if (!is.null(width)) {
95 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
96 "width: ", width, "; ")
97 }
98 if (bold) {
99 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
100 "font-weight: bold;")
101 }
102 if (italic) {
103 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
104 "font-style: italic;")
105 }
106 if (monospace) {
107 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
108 "font-family: monospace;")
109 }
110 if (!is.null(color)) {
111 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
112 "color: ", color, ";")
113 }
114 if (!is.null(background)) {
115 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
116 "background-color: ",
117 background, ";")
118 }
119 if (border_left) {
120 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
121 "border-left:", border_l_css, ";")
122 }
123 if (border_right) {
124 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
125 "border-right:", border_r_css, ";")
126 }
Hao Zhuec7ab922017-08-19 22:56:44 -0400127 }
Hao Zhubff01912017-05-23 18:05:00 -0400128 }
Hao Zhuf2dfd142017-07-24 14:43:28 -0400129 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400130 attributes(out) <- kable_attrs
131 return(out)
132}
133
Hao Zhu9b43f622017-09-11 19:00:08 -0400134column_spec_latex <- function(kable_input, columns, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400135 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400136 color, background,
Hao Zhu9b43f622017-09-11 19:00:08 -0400137 border_left, border_right,
138 decimal_align = F) {
Hao Zhubff01912017-05-23 18:05:00 -0400139 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000140 if (!is.null(table_info$collapse_rows)) {
141 message("Usually it is recommended to use column_spec before collapse_rows,",
142 " especially in LaTeX, to get a desired result. ")
143 }
Hao Zhubff01912017-05-23 18:05:00 -0400144 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
145 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
146
Hao Zhu9b43f622017-09-11 19:00:08 -0400147 table_info$align_vector[columns] <- unlist(lapply(
148 table_info$align_vector_origin[columns],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400149 function(x) {
150 latex_column_align_builder(
151 x, width, bold, italic, monospace,
152 color, background, border_left, border_right)
153 }
154 ))
Hao Zhubff01912017-05-23 18:05:00 -0400155
156 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
157
Hao Zhud2c0f732017-08-26 10:40:14 -0400158 out <- sub(kable_align_old, kable_align_new,
159 enc2utf8(as.character(kable_input)),
Hao Zhubff01912017-05-23 18:05:00 -0400160 perl = T)
161 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000162 if (!is.null(width)) {
163 if (is.null(table_info$column_width)) {
164 table_info$column_width <- list()
165 }
Hao Zhu9b43f622017-09-11 19:00:08 -0400166 for (i in columns) {
167 table_info$column_width[[paste0("column_", i)]] <- width
168 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000169 }
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}