blob: ffd23d5b101c664a916a0ad70b5c641fffb5b00d [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 Zhu322de082017-09-11 19:25:29 -04008#' @param column 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 Zhu78e61222017-05-24 20:53:35 -040027#'
28#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040029#' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
Hao Zhu78e61222017-05-24 20:53:35 -040030#'
Hao Zhubff01912017-05-23 18:05:00 -040031#' @export
Hao Zhu322de082017-09-11 19:25:29 -040032column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040033 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuec7ab922017-08-19 22:56:44 -040034 monospace = FALSE, color = NULL, background = NULL,
Hao Zhu4840bc92017-09-15 15:55:05 -040035 border_left = FALSE, border_right = FALSE) {
Hao Zhu322de082017-09-11 19:25:29 -040036 if (!is.numeric(column)) {
37 stop("column must be numeric. ")
Hao Zhubff01912017-05-23 18:05:00 -040038 }
39 kable_format <- attr(kable_input, "format")
40 if (!kable_format %in% c("html", "latex")) {
41 message("Currently generic markdown table using pandoc is not supported.")
42 return(kable_input)
43 }
44 if (kable_format == "html") {
Hao Zhu322de082017-09-11 19:25:29 -040045 return(column_spec_html(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040046 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 if (kable_format == "latex") {
Hao Zhu322de082017-09-11 19:25:29 -040051 return(column_spec_latex(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040052 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040053 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -040054 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040055 }
56}
57
Hao Zhu322de082017-09-11 19:25:29 -040058column_spec_html <- function(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040059 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040060 color, background,
61 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -040062 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040063 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040064 kable_tbody <- xml_tpart(kable_xml, "tbody")
65
66 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -040067 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -040068
Hao Zhubff01912017-05-23 18:05:00 -040069 if (!is.null(group_header_rows)) {
70 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
71 group_header_rows]
72 }
73
Hao Zhuec7ab922017-08-19 22:56:44 -040074 # Border css
75 border_l_css <- "1px solid"
76 border_r_css <- "1px solid"
77 if (is.character(border_left)) {
78 border_l_css <- border_left
79 border_left <- T
80 }
81 if (is.character(border_right)) {
82 border_r_css <- border_right
83 border_right <- T
84 }
85
Hao Zhubff01912017-05-23 18:05:00 -040086 for (i in all_contents_rows) {
Hao Zhu322de082017-09-11 19:25:29 -040087 for (j in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -040088 target_cell <- xml_child(xml_child(kable_tbody, i), j)
89 if (!is.null(width)) {
90 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
91 "width: ", width, "; ")
92 }
93 if (bold) {
94 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
95 "font-weight: bold;")
96 }
97 if (italic) {
98 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
99 "font-style: italic;")
100 }
101 if (monospace) {
102 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
103 "font-family: monospace;")
104 }
105 if (!is.null(color)) {
106 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
107 "color: ", color, ";")
108 }
109 if (!is.null(background)) {
110 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
111 "background-color: ",
112 background, ";")
113 }
114 if (border_left) {
115 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
116 "border-left:", border_l_css, ";")
117 }
118 if (border_right) {
119 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
120 "border-right:", border_r_css, ";")
121 }
Hao Zhuec7ab922017-08-19 22:56:44 -0400122 }
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 Zhu322de082017-09-11 19:25:29 -0400129column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400130 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400131 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -0400132 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 Zhu322de082017-09-11 19:25:29 -0400141 table_info$align_vector[column] <- unlist(lapply(
142 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400143 function(x) {
144 latex_column_align_builder(
145 x, width, bold, italic, monospace,
146 color, background, border_left, border_right)
147 }
148 ))
Hao Zhubff01912017-05-23 18:05:00 -0400149
150 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
151
Hao Zhud2c0f732017-08-26 10:40:14 -0400152 out <- sub(kable_align_old, kable_align_new,
153 enc2utf8(as.character(kable_input)),
Hao Zhubff01912017-05-23 18:05:00 -0400154 perl = T)
155 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000156 if (!is.null(width)) {
157 if (is.null(table_info$column_width)) {
158 table_info$column_width <- list()
159 }
Hao Zhu322de082017-09-11 19:25:29 -0400160 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400161 table_info$column_width[[paste0("column_", i)]] <- width
162 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000163 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400164 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400165 return(out)
166}
Hao Zhu32f43f72017-06-20 18:24:54 -0400167
Hao Zhua73601b2017-08-19 15:31:51 -0400168latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400169 color, background,
170 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400171 extra_align <- ""
172 if (!is.null(width)) {
173 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400174 "l" = "\\\\raggedright\\\\arraybackslash",
175 "c" = "\\\\centering\\\\arraybackslash",
176 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400177 x <- paste0("p\\{", width, "\\}")
178 }
179
Hao Zhua73601b2017-08-19 15:31:51 -0400180 if (!is.null(color)) {
181 color <- sprintf("\\\\color{%s}", color)
Hao Zhu32f43f72017-06-20 18:24:54 -0400182 }
183
Hao Zhuf2ebf9e2017-09-14 11:40:49 -0400184 if (!is.null(background)) {
Hao Zhuec7ab922017-08-19 22:56:44 -0400185 background <- sprintf("\\\\columncolor{%s}", background)
186 }
187
Hao Zhua73601b2017-08-19 15:31:51 -0400188 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
189 c(bold, italic, monospace)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400190 latex_array_options <- c(latex_array_options, extra_align,
191 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400192 latex_array_options <- paste0(
193 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
194 )
195 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400196 if (border_left) {
197 x <- paste0("|", x)
198 }
199 if (border_right) {
200 x <- paste0(x, "|")
201 }
Hao Zhua73601b2017-08-19 15:31:51 -0400202
Hao Zhu32f43f72017-06-20 18:24:54 -0400203 return(x)
204}