blob: 3af165b481d2d9fc8dabc887ead02d0bfdf98b80 [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 Zhua73601b2017-08-19 15:31:51 -0400138 if (!is.null(background)) {
139 warning("Column background color for LaTeX has not yet been implemented.")
140 }
Hao Zhubff01912017-05-23 18:05:00 -0400141 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
142 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
143
Hao Zhu32f43f72017-06-20 18:24:54 -0400144 table_info$align_vector[column] <- latex_column_align_builder(
Hao Zhua73601b2017-08-19 15:31:51 -0400145 table_info$align_vector[column], width, bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400146 color, background, border_left, border_right)
Hao Zhubff01912017-05-23 18:05:00 -0400147
148 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
149
150 out <- sub(kable_align_old, kable_align_new, as.character(kable_input),
151 perl = T)
152 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000153 if (!is.null(width)) {
154 if (is.null(table_info$column_width)) {
155 table_info$column_width <- list()
156 }
157 table_info$column_width[[paste0("column_", column)]] <- width
158 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400159 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400160 return(out)
161}
Hao Zhu32f43f72017-06-20 18:24:54 -0400162
Hao Zhua73601b2017-08-19 15:31:51 -0400163latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400164 color, background,
165 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400166 extra_align <- ""
167 if (!is.null(width)) {
168 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400169 "l" = "\\\\raggedright\\\\arraybackslash",
170 "c" = "\\\\centering\\\\arraybackslash",
171 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400172 x <- paste0("p\\{", width, "\\}")
173 }
174
Hao Zhua73601b2017-08-19 15:31:51 -0400175 if (!is.null(color)) {
176 color <- sprintf("\\\\color{%s}", color)
Hao Zhu32f43f72017-06-20 18:24:54 -0400177 }
178
Hao Zhuec7ab922017-08-19 22:56:44 -0400179 if (!is.null(color)) {
180 background <- sprintf("\\\\columncolor{%s}", background)
181 }
182
Hao Zhua73601b2017-08-19 15:31:51 -0400183 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
184 c(bold, italic, monospace)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400185 latex_array_options <- c(latex_array_options, extra_align,
186 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400187 latex_array_options <- paste0(
188 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
189 )
190 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400191 if (border_left) {
192 x <- paste0("|", x)
193 }
194 if (border_right) {
195 x <- paste0(x, "|")
196 }
Hao Zhua73601b2017-08-19 15:31:51 -0400197
Hao Zhu32f43f72017-06-20 18:24:54 -0400198 return(x)
199}