blob: 97dcd06bcc5e15cac3e153f6475013ce9a02a366 [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")
29#' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE)
30#'
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 Zhu9b43f622017-09-11 19:00:08 -040035 border_left = FALSE, border_right = FALSE,
Hao Zhu322de082017-09-11 19:25:29 -040036 ...) {
37 if (!is.numeric(column)) {
38 stop("column must be numeric. ")
Hao Zhubff01912017-05-23 18:05:00 -040039 }
40 kable_format <- attr(kable_input, "format")
41 if (!kable_format %in% c("html", "latex")) {
42 message("Currently generic markdown table using pandoc is not supported.")
43 return(kable_input)
44 }
45 if (kable_format == "html") {
Hao Zhu322de082017-09-11 19:25:29 -040046 return(column_spec_html(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040047 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040048 color, background,
49 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040050 }
51 if (kable_format == "latex") {
Hao Zhu322de082017-09-11 19:25:29 -040052 return(column_spec_latex(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040053 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040054 color, background,
Hao Zhu9b43f622017-09-11 19:00:08 -040055 border_left, border_right, ...))
Hao Zhubff01912017-05-23 18:05:00 -040056 }
57}
58
Hao Zhu322de082017-09-11 19:25:29 -040059column_spec_html <- function(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040060 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040061 color, background,
62 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -040063 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040064 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040065 kable_tbody <- xml_tpart(kable_xml, "tbody")
66
67 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -040068 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -040069
Hao Zhubff01912017-05-23 18:05:00 -040070 if (!is.null(group_header_rows)) {
71 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
72 group_header_rows]
73 }
74
Hao Zhuec7ab922017-08-19 22:56:44 -040075 # Border css
76 border_l_css <- "1px solid"
77 border_r_css <- "1px solid"
78 if (is.character(border_left)) {
79 border_l_css <- border_left
80 border_left <- T
81 }
82 if (is.character(border_right)) {
83 border_r_css <- border_right
84 border_right <- T
85 }
86
Hao Zhubff01912017-05-23 18:05:00 -040087 for (i in all_contents_rows) {
Hao Zhu322de082017-09-11 19:25:29 -040088 for (j in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -040089 target_cell <- xml_child(xml_child(kable_tbody, i), j)
90 if (!is.null(width)) {
91 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
92 "width: ", width, "; ")
93 }
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 }
102 if (monospace) {
103 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
104 "font-family: monospace;")
105 }
106 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 }
115 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 Zhuec7ab922017-08-19 22:56:44 -0400123 }
Hao Zhubff01912017-05-23 18:05:00 -0400124 }
Hao Zhuf2dfd142017-07-24 14:43:28 -0400125 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400126 attributes(out) <- kable_attrs
127 return(out)
128}
129
Hao Zhu322de082017-09-11 19:25:29 -0400130column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400131 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400132 color, background,
Hao Zhu9b43f622017-09-11 19:00:08 -0400133 border_left, border_right,
134 decimal_align = F) {
Hao Zhubff01912017-05-23 18:05:00 -0400135 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000136 if (!is.null(table_info$collapse_rows)) {
137 message("Usually it is recommended to use column_spec before collapse_rows,",
138 " especially in LaTeX, to get a desired result. ")
139 }
Hao Zhubff01912017-05-23 18:05:00 -0400140 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
141 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
142
Hao Zhu322de082017-09-11 19:25:29 -0400143 table_info$align_vector[column] <- unlist(lapply(
144 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400145 function(x) {
146 latex_column_align_builder(
147 x, width, bold, italic, monospace,
148 color, background, border_left, border_right)
149 }
150 ))
Hao Zhubff01912017-05-23 18:05:00 -0400151
152 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
153
Hao Zhud2c0f732017-08-26 10:40:14 -0400154 out <- sub(kable_align_old, kable_align_new,
155 enc2utf8(as.character(kable_input)),
Hao Zhubff01912017-05-23 18:05:00 -0400156 perl = T)
157 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000158 if (!is.null(width)) {
159 if (is.null(table_info$column_width)) {
160 table_info$column_width <- list()
161 }
Hao Zhu322de082017-09-11 19:25:29 -0400162 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400163 table_info$column_width[[paste0("column_", i)]] <- width
164 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000165 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400166 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400167 return(out)
168}
Hao Zhu32f43f72017-06-20 18:24:54 -0400169
Hao Zhua73601b2017-08-19 15:31:51 -0400170latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400171 color, background,
172 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400173 extra_align <- ""
174 if (!is.null(width)) {
175 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400176 "l" = "\\\\raggedright\\\\arraybackslash",
177 "c" = "\\\\centering\\\\arraybackslash",
178 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400179 x <- paste0("p\\{", width, "\\}")
180 }
181
Hao Zhua73601b2017-08-19 15:31:51 -0400182 if (!is.null(color)) {
183 color <- sprintf("\\\\color{%s}", color)
Hao Zhu32f43f72017-06-20 18:24:54 -0400184 }
185
Hao Zhuf2ebf9e2017-09-14 11:40:49 -0400186 if (!is.null(background)) {
Hao Zhuec7ab922017-08-19 22:56:44 -0400187 background <- sprintf("\\\\columncolor{%s}", background)
188 }
189
Hao Zhua73601b2017-08-19 15:31:51 -0400190 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
191 c(bold, italic, monospace)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400192 latex_array_options <- c(latex_array_options, extra_align,
193 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400194 latex_array_options <- paste0(
195 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
196 )
197 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400198 if (border_left) {
199 x <- paste0("|", x)
200 }
201 if (border_right) {
202 x <- paste0(x, "|")
203 }
Hao Zhua73601b2017-08-19 15:31:51 -0400204
Hao Zhu32f43f72017-06-20 18:24:54 -0400205 return(x)
206}