blob: 2e8a64df6265ffc7eef0f051a56b56f191590ab0 [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
Hao Zhue7c8f702017-10-10 13:22:59 -04004#' its look.
Hao Zhubff01912017-05-23 18:05:00 -04005#'
6#' @param kable_input Output of `knitr::kable()` with `format` specified
Hao Zhu322de082017-09-11 19:25:29 -04007#' @param column A numeric value or vector indicating which column(s) to be selected.
Hao Zhubff01912017-05-23 18:05:00 -04008#' @param width A character string telling HTML & LaTeX how wide the column
9#' needs to be, e.g. "10cm", "3in" or "30em".
10#' @param bold A T/F value to control whether the text of the selected column
11#' need to be bolded.
12#' @param italic A T/F value to control whether the text of the selected column
13#' need to be emphasized.
Hao Zhu8f202992017-07-15 02:20:18 -040014#' @param monospace A T/F value to control whether the text of the selected column
15#' need to be monospaced (verbatim)
Hao Zhuef0c8302018-01-12 13:30:20 -050016#' @param underline A T/F value to control whether the text of the selected row
17#' need to be underlined
18#' @param strikeout A T/F value to control whether the text of the selected row
19#' need to be stricked out.
Hao Zhu53e240f2017-09-04 20:04:29 -040020#' @param color A character string for column text color. Here please pay
21#' attention to the differences in color codes between HTML and LaTeX.
22#' @param background A character string for column background color. Here please
23#' pay attention to the differences in color codes between HTML and LaTeX.
24#' @param border_left A logical variable indicating whether there should be a
25#' border line on the left of the selected column. In HTML, you can also pass
26#' in a character string for the CSS of the border line
27#' @param border_right A logical variable indicating whether there should be a
28#' border line on the right of the selected column. In HTML, you can also pass
29#' in a character string for the CSS of the border line
Hao Zhu6107f372018-05-21 00:23:26 -040030#' @param width_min Only for HTML table. Normal column width will automatically
31#' collapse when the window cannot hold enough contents. With this `width_min`,
Hao Zhub1caa272018-04-14 14:19:46 -040032#' you can set up a column with a width that won't collapse even when the
33#' window is not wide enough.
Hao Zhu6107f372018-05-21 00:23:26 -040034#' @param width_max Only for HTML table. `width_max` defines the maximum width
Hao Zhub1caa272018-04-14 14:19:46 -040035#' of table columns.
Hao Zhub1de9672018-01-08 16:29:24 -050036#' @param extra_css Extra css text to be passed into the cells of the row. Note
37#' that it's not for the whole column but to each individual cells
Hao Zhu907ddfe2018-04-23 15:19:09 -040038#' @param include_thead T/F. A HTML only feature to contoll whether the
39#' header row will be manipulated. Default is `FALSE`.
Duncan Murdoch8bc96222019-04-29 12:46:39 -040040#' @param latex_column_spec Only for LaTeX tables. Code to replace the column
41#' specification. If not `NULL`, will override all other arguments.
42#'
43#' @details Use `latex_column_spec` in a LaTeX table to change or
44#' customize the column specification. Because of the way it is handled
45#' internally, any backslashes must be escaped.
Hao Zhu78e61222017-05-24 20:53:35 -040046#'
47#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040048#' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
Duncan Murdoch8bc96222019-04-29 12:46:39 -040049#' x <- knitr::kable(head(mtcars), "latex", booktabs = TRUE)
50#' column_spec(x, 1, latex_column_spec = ">{\\\\color{red}}c")
Hao Zhubff01912017-05-23 18:05:00 -040051#' @export
Hao Zhu322de082017-09-11 19:25:29 -040052column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040053 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050054 monospace = FALSE, underline = FALSE, strikeout = FALSE,
55 color = NULL, background = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050056 border_left = FALSE, border_right = FALSE,
Hao Zhub1caa272018-04-14 14:19:46 -040057 width_min = NULL, width_max = NULL,
Duncan Murdoch8bc96222019-04-29 12:46:39 -040058 extra_css = NULL, include_thead = FALSE,
59 latex_column_spec = NULL) {
Hao Zhu322de082017-09-11 19:25:29 -040060 if (!is.numeric(column)) {
61 stop("column must be numeric. ")
Hao Zhubff01912017-05-23 18:05:00 -040062 }
63 kable_format <- attr(kable_input, "format")
64 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050065 warning("Please specify format in kable. kableExtra can customize either ",
66 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
67 "for details.")
Hao Zhubff01912017-05-23 18:05:00 -040068 return(kable_input)
69 }
70 if (kable_format == "html") {
Hao Zhu322de082017-09-11 19:25:29 -040071 return(column_spec_html(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040072 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050073 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040074 color, background,
Hao Zhub1caa272018-04-14 14:19:46 -040075 border_left, border_right,
76 width_min, width_max,
Hao Zhu907ddfe2018-04-23 15:19:09 -040077 extra_css, include_thead))
Hao Zhubff01912017-05-23 18:05:00 -040078 }
79 if (kable_format == "latex") {
Hao Zhu322de082017-09-11 19:25:29 -040080 return(column_spec_latex(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040081 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050082 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040083 color, background,
Duncan Murdoch8bc96222019-04-29 12:46:39 -040084 border_left, border_right,
85 latex_column_spec = latex_column_spec))
Hao Zhubff01912017-05-23 18:05:00 -040086 }
87}
88
Hao Zhu322de082017-09-11 19:25:29 -040089column_spec_html <- function(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040090 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050091 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040092 color, background,
Hao Zhub1caa272018-04-14 14:19:46 -040093 border_left, border_right,
94 width_min, width_max,
Hao Zhu907ddfe2018-04-23 15:19:09 -040095 extra_css, include_thead) {
Hao Zhubff01912017-05-23 18:05:00 -040096 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040097 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040098 kable_tbody <- xml_tpart(kable_xml, "tbody")
99
100 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -0400101 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -0400102
Hao Zhubff01912017-05-23 18:05:00 -0400103 if (!is.null(group_header_rows)) {
104 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
105 group_header_rows]
106 }
107
Hao Zhuec7ab922017-08-19 22:56:44 -0400108 # Border css
109 border_l_css <- "1px solid"
110 border_r_css <- "1px solid"
111 if (is.character(border_left)) {
112 border_l_css <- border_left
113 border_left <- T
114 }
115 if (is.character(border_right)) {
116 border_r_css <- border_right
117 border_right <- T
118 }
119
Hao Zhubff01912017-05-23 18:05:00 -0400120 for (i in all_contents_rows) {
Hao Zhu322de082017-09-11 19:25:29 -0400121 for (j in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400122 target_cell <- xml_child(xml_child(kable_tbody, i), j)
Hao Zhu907ddfe2018-04-23 15:19:09 -0400123 column_spec_html_cell(
124 target_cell, width, width_min, width_max,
125 bold, italic, monospace, underline, strikeout,
126 color, background, border_left, border_right,
127 border_l_css, border_r_css,
128 extra_css
129 )
Hao Zhuec7ab922017-08-19 22:56:44 -0400130 }
Hao Zhubff01912017-05-23 18:05:00 -0400131 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400132
Hao Zhu907ddfe2018-04-23 15:19:09 -0400133 if (include_thead) {
134 kable_thead <- xml_tpart(kable_xml, "thead")
135 nrow_thead <- length(xml_children(kable_thead))
136 for (j in column) {
137 target_cell <- xml_child(xml_child(kable_thead, nrow_thead), j)
138 column_spec_html_cell(
139 target_cell, width, width_min, width_max,
140 bold, italic, monospace, underline, strikeout,
141 color, background, border_left, border_right,
142 border_l_css, border_r_css,
143 extra_css
144 )
145 }
146 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400147
Hao Zhuf2dfd142017-07-24 14:43:28 -0400148 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400149 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500150 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhubff01912017-05-23 18:05:00 -0400151 return(out)
152}
153
Hao Zhu907ddfe2018-04-23 15:19:09 -0400154column_spec_html_cell <- function(target_cell, width, width_min, width_max,
155 bold, italic, monospace, underline, strikeout,
156 color, background,
157 border_left, border_right,
158 border_l_css, border_r_css,
159 extra_css) {
Hao Zhu517453c2018-05-13 13:07:18 -0400160 if (is.na(xml_attr(target_cell, "style"))) {
161 xml_attr(target_cell, "style") <- ""
162 }
Hao Zhu907ddfe2018-04-23 15:19:09 -0400163 if (!is.null(width)) {
164 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
165 "width: ", width, "; ")
166 }
167 if (!is.null(width_min)) {
168 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
169 "min-width: ", width_min, "; ")
170 }
171 if (!is.null(width_max)) {
172 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
173 "max-width: ", width_max, "; ")
174 }
175 if (bold) {
176 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
177 "font-weight: bold;")
178 }
179 if (italic) {
180 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
181 "font-style: italic;")
182 }
183 if (monospace) {
184 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
185 "font-family: monospace;")
186 }
187 if (underline) {
188 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
189 "text-decoration: underline;")
190 }
191 if (strikeout) {
192 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
193 "text-decoration: line-through;")
194 }
195 if (!is.null(color)) {
196 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhu72917f92019-03-15 18:41:42 -0400197 "color: ", html_color(color),
198 " !important;")
Hao Zhu907ddfe2018-04-23 15:19:09 -0400199 }
200 if (!is.null(background)) {
201 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
202 "background-color: ",
Hao Zhu72917f92019-03-15 18:41:42 -0400203 html_color(background),
204 " !important;")
Hao Zhu907ddfe2018-04-23 15:19:09 -0400205 }
206 if (border_left) {
207 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
208 "border-left:", border_l_css, ";")
209 }
210 if (border_right) {
211 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
212 "border-right:", border_r_css, ";")
213 }
214 if (!is.null(extra_css)) {
215 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
216 extra_css)
217 }
218}
219
Hao Zhu322de082017-09-11 19:25:29 -0400220column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400221 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500222 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400223 color, background,
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400224 border_left, border_right,
225 latex_column_spec) {
Hao Zhubff01912017-05-23 18:05:00 -0400226 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000227 if (!is.null(table_info$collapse_rows)) {
228 message("Usually it is recommended to use column_spec before collapse_rows,",
229 " especially in LaTeX, to get a desired result. ")
230 }
Hao Zhu67764aa2019-03-15 11:57:50 -0400231 align_collapse <- ifelse(table_info$booktabs | !is.null(table_info$xtable),
232 "", "\\|")
Hao Zhubff01912017-05-23 18:05:00 -0400233 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
234
Hao Zhu322de082017-09-11 19:25:29 -0400235 table_info$align_vector[column] <- unlist(lapply(
236 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400237 function(x) {
238 latex_column_align_builder(
Hao Zhuef0c8302018-01-12 13:30:20 -0500239 x, width, bold, italic, monospace, underline, strikeout,
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400240 color, background, border_left, border_right, latex_column_spec)
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400241 }
242 ))
Hao Zhubff01912017-05-23 18:05:00 -0400243
244 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
245
Duncan Murdoch6eb29502018-12-16 20:21:00 -0500246 out <- sub(paste0("\\{", kable_align_old, "\\}"),
247 paste0("\\{", kable_align_new, "\\}"),
Hao Zhu3fc0e882018-04-03 16:06:41 -0400248 solve_enc(kable_input),
Hao Zhubff01912017-05-23 18:05:00 -0400249 perl = T)
Hao Zhuae80df42018-04-12 15:45:11 -0400250
251 if (!is.null(width)) {
252 fix_newline <- replace_makecell_with_newline(out, table_info, column)
253 out <- fix_newline[[1]]
254 table_info <- fix_newline[[2]]
255 }
256
Hao Zhubff01912017-05-23 18:05:00 -0400257 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000258 if (!is.null(width)) {
259 if (is.null(table_info$column_width)) {
260 table_info$column_width <- list()
261 }
Hao Zhu322de082017-09-11 19:25:29 -0400262 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400263 table_info$column_width[[paste0("column_", i)]] <- width
264 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000265 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400266 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400267 return(out)
268}
Hao Zhu32f43f72017-06-20 18:24:54 -0400269
Hao Zhua73601b2017-08-19 15:31:51 -0400270latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500271 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400272 color, background,
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400273 border_left, border_right,
274 latex_column_spec) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400275 extra_align <- ""
276 if (!is.null(width)) {
277 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400278 "l" = "\\\\raggedright\\\\arraybackslash",
279 "c" = "\\\\centering\\\\arraybackslash",
280 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400281 x <- paste0("p\\{", width, "\\}")
282 }
283
Hao Zhua73601b2017-08-19 15:31:51 -0400284 if (!is.null(color)) {
Stefanie LaZerte994fc562017-12-11 14:14:07 -0600285 color <- paste0("\\\\leavevmode\\\\color", latex_color(color))
Hao Zhu32f43f72017-06-20 18:24:54 -0400286 }
287
Hao Zhuf2ebf9e2017-09-14 11:40:49 -0400288 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500289 background <- paste0("\\\\columncolor", latex_color(background))
Hao Zhuec7ab922017-08-19 22:56:44 -0400290 }
291
Hao Zhuef0c8302018-01-12 13:30:20 -0500292 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily",
293 "\\\\underline", "\\\\sout")[
Hao Zhub49bddf2018-01-12 15:25:23 -0500294 c(bold, italic, monospace, underline, strikeout)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400295 latex_array_options <- c(latex_array_options, extra_align,
296 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400297 latex_array_options <- paste0(
298 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
299 )
300 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400301 if (border_left) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500302 x <- paste0("\\|", x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400303 }
304 if (border_right) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500305 x <- paste0(x, "\\|")
Hao Zhuec7ab922017-08-19 22:56:44 -0400306 }
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400307 if (!is.null(latex_column_spec))
308 x <- latex_column_spec
Hao Zhua73601b2017-08-19 15:31:51 -0400309
Hao Zhu32f43f72017-06-20 18:24:54 -0400310 return(x)
311}
Hao Zhuae80df42018-04-12 15:45:11 -0400312
313replace_makecell_with_newline <- function(kable_input, table_info, column) {
314 if (!str_detect(kable_input, "makecell")) return(list(kable_input, table_info))
315 contents_table <- data.frame(sapply(table_info$contents,
316 function(x) {str_split(x, " \\& ")[[1]]}),
317 stringsAsFactors = F)
318 names(contents_table) <- paste0("x", 1:table_info$nrow)
319 rows_check_makecell <- str_detect(contents_table[column, ], "makecell")
320 if (sum(rows_check_makecell) == 0) return(list(kable_input, table_info))
321 rows_to_replace <- which(rows_check_makecell)
322
323 for (i in column) {
324 target_column <- contents_table[i, ]
325 for (j in which(str_detect(target_column, "\\\\\\\\makecell"))) {
326 contents_table[i, j] <- str_replace(
327 contents_table[i, j], "\\\\\\\\makecell\\\\\\[.\\\\\\]\\\\\\{", "")
328 contents_table[i, j] <- str_replace(
Hao Zhu9ac3e382018-04-12 18:56:32 -0400329 contents_table[i, j], "\\\\\\}$", "")
Hao Zhuae80df42018-04-12 15:45:11 -0400330 contents_table[i, j] <- str_replace_all(
331 contents_table[i, j], "\\\\\\\\\\\\\\\\", "\\\\\\\\newline "
332 )
333 }
334 }
335
336 new_contents <- unlist(lapply(contents_table, paste, collapse = " & "))
337 for (i in rows_to_replace) {
338 kable_input <- sub(table_info$contents[i], new_contents[i], kable_input,
339 perl = T)
340 table_info$contents[i] <- new_contents[i]
341 }
342
343 return(list(kable_input, table_info))
344}