blob: 2a97fee81411e6fbfdfc66dde66e1583bef219d2 [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.
Hao Zhu2b739ac2020-08-15 01:38:51 -040042#' @param latex_valign vertical alignment. Only works when you specified column
43#' width. Choose among `p`, `m`, `b`.
Duncan Murdoch8bc96222019-04-29 12:46:39 -040044#'
45#' @details Use `latex_column_spec` in a LaTeX table to change or
46#' customize the column specification. Because of the way it is handled
47#' internally, any backslashes must be escaped.
Hao Zhu78e61222017-05-24 20:53:35 -040048#'
49#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040050#' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
Duncan Murdoch8bc96222019-04-29 12:46:39 -040051#' x <- knitr::kable(head(mtcars), "latex", booktabs = TRUE)
52#' column_spec(x, 1, latex_column_spec = ">{\\\\color{red}}c")
Hao Zhubff01912017-05-23 18:05:00 -040053#' @export
Hao Zhu322de082017-09-11 19:25:29 -040054column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040055 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050056 monospace = FALSE, underline = FALSE, strikeout = FALSE,
57 color = NULL, background = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050058 border_left = FALSE, border_right = FALSE,
Hao Zhub1caa272018-04-14 14:19:46 -040059 width_min = NULL, width_max = NULL,
Duncan Murdoch8bc96222019-04-29 12:46:39 -040060 extra_css = NULL, include_thead = FALSE,
Hao Zhu2b739ac2020-08-15 01:38:51 -040061 latex_column_spec = NULL, latex_valign = 'p') {
Hao Zhu322de082017-09-11 19:25:29 -040062 if (!is.numeric(column)) {
63 stop("column must be numeric. ")
Hao Zhubff01912017-05-23 18:05:00 -040064 }
65 kable_format <- attr(kable_input, "format")
66 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050067 warning("Please specify format in kable. kableExtra can customize either ",
68 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
69 "for details.")
Hao Zhubff01912017-05-23 18:05:00 -040070 return(kable_input)
71 }
72 if (kable_format == "html") {
Hao Zhu322de082017-09-11 19:25:29 -040073 return(column_spec_html(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040074 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050075 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040076 color, background,
Hao Zhub1caa272018-04-14 14:19:46 -040077 border_left, border_right,
78 width_min, width_max,
Hao Zhu907ddfe2018-04-23 15:19:09 -040079 extra_css, include_thead))
Hao Zhubff01912017-05-23 18:05:00 -040080 }
81 if (kable_format == "latex") {
Hao Zhu322de082017-09-11 19:25:29 -040082 return(column_spec_latex(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040083 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050084 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040085 color, background,
Duncan Murdoch8bc96222019-04-29 12:46:39 -040086 border_left, border_right,
Hao Zhu2b739ac2020-08-15 01:38:51 -040087 latex_column_spec = latex_column_spec,
88 latex_valign = latex_valign))
Hao Zhubff01912017-05-23 18:05:00 -040089 }
90}
91
Hao Zhu322de082017-09-11 19:25:29 -040092column_spec_html <- function(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040093 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050094 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040095 color, background,
Hao Zhub1caa272018-04-14 14:19:46 -040096 border_left, border_right,
97 width_min, width_max,
Hao Zhu907ddfe2018-04-23 15:19:09 -040098 extra_css, include_thead) {
Hao Zhubff01912017-05-23 18:05:00 -040099 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -0400100 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -0400101 kable_tbody <- xml_tpart(kable_xml, "tbody")
102
103 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -0400104 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -0400105
Hao Zhubff01912017-05-23 18:05:00 -0400106 if (!is.null(group_header_rows)) {
107 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
108 group_header_rows]
109 }
110
Hao Zhuec7ab922017-08-19 22:56:44 -0400111 # Border css
112 border_l_css <- "1px solid"
113 border_r_css <- "1px solid"
114 if (is.character(border_left)) {
115 border_l_css <- border_left
116 border_left <- T
117 }
118 if (is.character(border_right)) {
119 border_r_css <- border_right
120 border_right <- T
121 }
122
Hao Zhubff01912017-05-23 18:05:00 -0400123 for (i in all_contents_rows) {
Hao Zhu322de082017-09-11 19:25:29 -0400124 for (j in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400125 target_cell <- xml_child(xml_child(kable_tbody, i), j)
Hao Zhu907ddfe2018-04-23 15:19:09 -0400126 column_spec_html_cell(
127 target_cell, width, width_min, width_max,
128 bold, italic, monospace, underline, strikeout,
129 color, background, border_left, border_right,
130 border_l_css, border_r_css,
131 extra_css
132 )
Hao Zhuec7ab922017-08-19 22:56:44 -0400133 }
Hao Zhubff01912017-05-23 18:05:00 -0400134 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400135
Hao Zhu907ddfe2018-04-23 15:19:09 -0400136 if (include_thead) {
137 kable_thead <- xml_tpart(kable_xml, "thead")
138 nrow_thead <- length(xml_children(kable_thead))
139 for (j in column) {
140 target_cell <- xml_child(xml_child(kable_thead, nrow_thead), j)
141 column_spec_html_cell(
142 target_cell, width, width_min, width_max,
143 bold, italic, monospace, underline, strikeout,
144 color, background, border_left, border_right,
145 border_l_css, border_r_css,
146 extra_css
147 )
148 }
149 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400150
Hao Zhuf2dfd142017-07-24 14:43:28 -0400151 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400152 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500153 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhubff01912017-05-23 18:05:00 -0400154 return(out)
155}
156
Hao Zhu907ddfe2018-04-23 15:19:09 -0400157column_spec_html_cell <- function(target_cell, width, width_min, width_max,
158 bold, italic, monospace, underline, strikeout,
159 color, background,
160 border_left, border_right,
161 border_l_css, border_r_css,
162 extra_css) {
Hao Zhu517453c2018-05-13 13:07:18 -0400163 if (is.na(xml_attr(target_cell, "style"))) {
164 xml_attr(target_cell, "style") <- ""
165 }
Hao Zhu907ddfe2018-04-23 15:19:09 -0400166 if (!is.null(width)) {
167 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
168 "width: ", width, "; ")
169 }
170 if (!is.null(width_min)) {
171 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
172 "min-width: ", width_min, "; ")
173 }
174 if (!is.null(width_max)) {
175 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
176 "max-width: ", width_max, "; ")
177 }
178 if (bold) {
179 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
180 "font-weight: bold;")
181 }
182 if (italic) {
183 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
184 "font-style: italic;")
185 }
186 if (monospace) {
187 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
188 "font-family: monospace;")
189 }
190 if (underline) {
191 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
192 "text-decoration: underline;")
193 }
194 if (strikeout) {
195 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
196 "text-decoration: line-through;")
197 }
198 if (!is.null(color)) {
199 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhu72917f92019-03-15 18:41:42 -0400200 "color: ", html_color(color),
201 " !important;")
Hao Zhu907ddfe2018-04-23 15:19:09 -0400202 }
203 if (!is.null(background)) {
204 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
205 "background-color: ",
Hao Zhu72917f92019-03-15 18:41:42 -0400206 html_color(background),
207 " !important;")
Hao Zhu907ddfe2018-04-23 15:19:09 -0400208 }
209 if (border_left) {
210 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
211 "border-left:", border_l_css, ";")
212 }
213 if (border_right) {
214 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
215 "border-right:", border_r_css, ";")
216 }
217 if (!is.null(extra_css)) {
218 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
219 extra_css)
220 }
221}
222
Hao Zhu322de082017-09-11 19:25:29 -0400223column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400224 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500225 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400226 color, background,
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400227 border_left, border_right,
Hao Zhu2b739ac2020-08-15 01:38:51 -0400228 latex_column_spec, latex_valign) {
Hao Zhubff01912017-05-23 18:05:00 -0400229 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000230 if (!is.null(table_info$collapse_rows)) {
231 message("Usually it is recommended to use column_spec before collapse_rows,",
232 " especially in LaTeX, to get a desired result. ")
233 }
Hao Zhu67764aa2019-03-15 11:57:50 -0400234 align_collapse <- ifelse(table_info$booktabs | !is.null(table_info$xtable),
235 "", "\\|")
Hao Zhubff01912017-05-23 18:05:00 -0400236 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
237
Hao Zhu322de082017-09-11 19:25:29 -0400238 table_info$align_vector[column] <- unlist(lapply(
239 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400240 function(x) {
241 latex_column_align_builder(
Hao Zhuef0c8302018-01-12 13:30:20 -0500242 x, width, bold, italic, monospace, underline, strikeout,
Hao Zhu2b739ac2020-08-15 01:38:51 -0400243 color, background, border_left, border_right, latex_column_spec,
244 latex_valign)
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400245 }
246 ))
Hao Zhubff01912017-05-23 18:05:00 -0400247
248 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
249
Duncan Murdoch6eb29502018-12-16 20:21:00 -0500250 out <- sub(paste0("\\{", kable_align_old, "\\}"),
251 paste0("\\{", kable_align_new, "\\}"),
Hao Zhu3fc0e882018-04-03 16:06:41 -0400252 solve_enc(kable_input),
Hao Zhubff01912017-05-23 18:05:00 -0400253 perl = T)
Hao Zhuae80df42018-04-12 15:45:11 -0400254
255 if (!is.null(width)) {
256 fix_newline <- replace_makecell_with_newline(out, table_info, column)
257 out <- fix_newline[[1]]
258 table_info <- fix_newline[[2]]
259 }
260
Hao Zhubff01912017-05-23 18:05:00 -0400261 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000262 if (!is.null(width)) {
263 if (is.null(table_info$column_width)) {
264 table_info$column_width <- list()
265 }
Hao Zhu322de082017-09-11 19:25:29 -0400266 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400267 table_info$column_width[[paste0("column_", i)]] <- width
268 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000269 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400270 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400271 return(out)
272}
Hao Zhu32f43f72017-06-20 18:24:54 -0400273
Hao Zhua73601b2017-08-19 15:31:51 -0400274latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500275 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400276 color, background,
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400277 border_left, border_right,
Hao Zhu2b739ac2020-08-15 01:38:51 -0400278 latex_column_spec, latex_valign) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400279 extra_align <- ""
280 if (!is.null(width)) {
281 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400282 "l" = "\\\\raggedright\\\\arraybackslash",
283 "c" = "\\\\centering\\\\arraybackslash",
284 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu2b739ac2020-08-15 01:38:51 -0400285 x <- paste0(latex_valign, "\\{", width, "\\}")
Hao Zhu32f43f72017-06-20 18:24:54 -0400286 }
287
Hao Zhua73601b2017-08-19 15:31:51 -0400288 if (!is.null(color)) {
Stefanie LaZerte994fc562017-12-11 14:14:07 -0600289 color <- paste0("\\\\leavevmode\\\\color", latex_color(color))
Hao Zhu32f43f72017-06-20 18:24:54 -0400290 }
291
Hao Zhuf2ebf9e2017-09-14 11:40:49 -0400292 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500293 background <- paste0("\\\\columncolor", latex_color(background))
Hao Zhuec7ab922017-08-19 22:56:44 -0400294 }
295
Hao Zhuef0c8302018-01-12 13:30:20 -0500296 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily",
297 "\\\\underline", "\\\\sout")[
Hao Zhub49bddf2018-01-12 15:25:23 -0500298 c(bold, italic, monospace, underline, strikeout)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400299 latex_array_options <- c(latex_array_options, extra_align,
300 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400301 latex_array_options <- paste0(
302 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
303 )
304 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400305 if (border_left) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500306 x <- paste0("\\|", x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400307 }
308 if (border_right) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500309 x <- paste0(x, "\\|")
Hao Zhuec7ab922017-08-19 22:56:44 -0400310 }
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400311 if (!is.null(latex_column_spec))
312 x <- latex_column_spec
Hao Zhua73601b2017-08-19 15:31:51 -0400313
Hao Zhu32f43f72017-06-20 18:24:54 -0400314 return(x)
315}
Hao Zhuae80df42018-04-12 15:45:11 -0400316
317replace_makecell_with_newline <- function(kable_input, table_info, column) {
318 if (!str_detect(kable_input, "makecell")) return(list(kable_input, table_info))
319 contents_table <- data.frame(sapply(table_info$contents,
320 function(x) {str_split(x, " \\& ")[[1]]}),
321 stringsAsFactors = F)
322 names(contents_table) <- paste0("x", 1:table_info$nrow)
323 rows_check_makecell <- str_detect(contents_table[column, ], "makecell")
324 if (sum(rows_check_makecell) == 0) return(list(kable_input, table_info))
325 rows_to_replace <- which(rows_check_makecell)
326
327 for (i in column) {
328 target_column <- contents_table[i, ]
329 for (j in which(str_detect(target_column, "\\\\\\\\makecell"))) {
330 contents_table[i, j] <- str_replace(
331 contents_table[i, j], "\\\\\\\\makecell\\\\\\[.\\\\\\]\\\\\\{", "")
332 contents_table[i, j] <- str_replace(
Hao Zhu9ac3e382018-04-12 18:56:32 -0400333 contents_table[i, j], "\\\\\\}$", "")
Hao Zhuae80df42018-04-12 15:45:11 -0400334 contents_table[i, j] <- str_replace_all(
335 contents_table[i, j], "\\\\\\\\\\\\\\\\", "\\\\\\\\newline "
336 )
337 }
338 }
339
340 new_contents <- unlist(lapply(contents_table, paste, collapse = " & "))
341 for (i in rows_to_replace) {
342 kable_input <- sub(table_info$contents[i], new_contents[i], kable_input,
343 perl = T)
344 table_info$contents[i] <- new_contents[i]
345 }
346
347 return(list(kable_input, table_info))
348}