blob: 693bc808656cbb2d2ba1cffa44600b0a75b51bd9 [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`.
Hao Zhu78e61222017-05-24 20:53:35 -040040#'
41#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040042#' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
Hao Zhu78e61222017-05-24 20:53:35 -040043#'
Hao Zhubff01912017-05-23 18:05:00 -040044#' @export
Hao Zhu322de082017-09-11 19:25:29 -040045column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040046 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050047 monospace = FALSE, underline = FALSE, strikeout = FALSE,
48 color = NULL, background = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050049 border_left = FALSE, border_right = FALSE,
Hao Zhub1caa272018-04-14 14:19:46 -040050 width_min = NULL, width_max = NULL,
Hao Zhu907ddfe2018-04-23 15:19:09 -040051 extra_css = NULL, include_thead = FALSE) {
Hao Zhu322de082017-09-11 19:25:29 -040052 if (!is.numeric(column)) {
53 stop("column must be numeric. ")
Hao Zhubff01912017-05-23 18:05:00 -040054 }
55 kable_format <- attr(kable_input, "format")
56 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050057 warning("Please specify format in kable. kableExtra can customize either ",
58 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
59 "for details.")
Hao Zhubff01912017-05-23 18:05:00 -040060 return(kable_input)
61 }
62 if (kable_format == "html") {
Hao Zhu322de082017-09-11 19:25:29 -040063 return(column_spec_html(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040064 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050065 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040066 color, background,
Hao Zhub1caa272018-04-14 14:19:46 -040067 border_left, border_right,
68 width_min, width_max,
Hao Zhu907ddfe2018-04-23 15:19:09 -040069 extra_css, include_thead))
Hao Zhubff01912017-05-23 18:05:00 -040070 }
71 if (kable_format == "latex") {
Hao Zhu322de082017-09-11 19:25:29 -040072 return(column_spec_latex(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040073 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050074 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040075 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -040076 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040077 }
78}
79
Hao Zhu322de082017-09-11 19:25:29 -040080column_spec_html <- function(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,
Hao Zhub1caa272018-04-14 14:19:46 -040084 border_left, border_right,
85 width_min, width_max,
Hao Zhu907ddfe2018-04-23 15:19:09 -040086 extra_css, include_thead) {
Hao Zhubff01912017-05-23 18:05:00 -040087 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040088 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040089 kable_tbody <- xml_tpart(kable_xml, "tbody")
90
91 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -040092 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -040093
Hao Zhubff01912017-05-23 18:05:00 -040094 if (!is.null(group_header_rows)) {
95 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
96 group_header_rows]
97 }
98
Hao Zhuec7ab922017-08-19 22:56:44 -040099 # Border css
100 border_l_css <- "1px solid"
101 border_r_css <- "1px solid"
102 if (is.character(border_left)) {
103 border_l_css <- border_left
104 border_left <- T
105 }
106 if (is.character(border_right)) {
107 border_r_css <- border_right
108 border_right <- T
109 }
110
Hao Zhubff01912017-05-23 18:05:00 -0400111 for (i in all_contents_rows) {
Hao Zhu322de082017-09-11 19:25:29 -0400112 for (j in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400113 target_cell <- xml_child(xml_child(kable_tbody, i), j)
Hao Zhu907ddfe2018-04-23 15:19:09 -0400114 column_spec_html_cell(
115 target_cell, width, width_min, width_max,
116 bold, italic, monospace, underline, strikeout,
117 color, background, border_left, border_right,
118 border_l_css, border_r_css,
119 extra_css
120 )
Hao Zhuec7ab922017-08-19 22:56:44 -0400121 }
Hao Zhubff01912017-05-23 18:05:00 -0400122 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400123
Hao Zhu907ddfe2018-04-23 15:19:09 -0400124 if (include_thead) {
125 kable_thead <- xml_tpart(kable_xml, "thead")
126 nrow_thead <- length(xml_children(kable_thead))
127 for (j in column) {
128 target_cell <- xml_child(xml_child(kable_thead, nrow_thead), j)
129 column_spec_html_cell(
130 target_cell, width, width_min, width_max,
131 bold, italic, monospace, underline, strikeout,
132 color, background, border_left, border_right,
133 border_l_css, border_r_css,
134 extra_css
135 )
136 }
137 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400138
Hao Zhuf2dfd142017-07-24 14:43:28 -0400139 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400140 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500141 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhubff01912017-05-23 18:05:00 -0400142 return(out)
143}
144
Hao Zhu907ddfe2018-04-23 15:19:09 -0400145column_spec_html_cell <- function(target_cell, width, width_min, width_max,
146 bold, italic, monospace, underline, strikeout,
147 color, background,
148 border_left, border_right,
149 border_l_css, border_r_css,
150 extra_css) {
Hao Zhu517453c2018-05-13 13:07:18 -0400151 if (is.na(xml_attr(target_cell, "style"))) {
152 xml_attr(target_cell, "style") <- ""
153 }
Hao Zhu907ddfe2018-04-23 15:19:09 -0400154 if (!is.null(width)) {
155 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
156 "width: ", width, "; ")
157 }
158 if (!is.null(width_min)) {
159 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
160 "min-width: ", width_min, "; ")
161 }
162 if (!is.null(width_max)) {
163 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
164 "max-width: ", width_max, "; ")
165 }
166 if (bold) {
167 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
168 "font-weight: bold;")
169 }
170 if (italic) {
171 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
172 "font-style: italic;")
173 }
174 if (monospace) {
175 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
176 "font-family: monospace;")
177 }
178 if (underline) {
179 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
180 "text-decoration: underline;")
181 }
182 if (strikeout) {
183 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
184 "text-decoration: line-through;")
185 }
186 if (!is.null(color)) {
187 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
188 "color: ", color, ";")
189 }
190 if (!is.null(background)) {
191 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
192 "background-color: ",
193 background, ";")
194 }
195 if (border_left) {
196 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
197 "border-left:", border_l_css, ";")
198 }
199 if (border_right) {
200 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
201 "border-right:", border_r_css, ";")
202 }
203 if (!is.null(extra_css)) {
204 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
205 extra_css)
206 }
207}
208
Hao Zhu322de082017-09-11 19:25:29 -0400209column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400210 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500211 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400212 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -0400213 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -0400214 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000215 if (!is.null(table_info$collapse_rows)) {
216 message("Usually it is recommended to use column_spec before collapse_rows,",
217 " especially in LaTeX, to get a desired result. ")
218 }
Hao Zhu67764aa2019-03-15 11:57:50 -0400219 align_collapse <- ifelse(table_info$booktabs | !is.null(table_info$xtable),
220 "", "\\|")
Hao Zhubff01912017-05-23 18:05:00 -0400221 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
222
Hao Zhu322de082017-09-11 19:25:29 -0400223 table_info$align_vector[column] <- unlist(lapply(
224 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400225 function(x) {
226 latex_column_align_builder(
Hao Zhuef0c8302018-01-12 13:30:20 -0500227 x, width, bold, italic, monospace, underline, strikeout,
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400228 color, background, border_left, border_right)
229 }
230 ))
Hao Zhubff01912017-05-23 18:05:00 -0400231
232 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
233
Duncan Murdoch6eb29502018-12-16 20:21:00 -0500234 out <- sub(paste0("\\{", kable_align_old, "\\}"),
235 paste0("\\{", kable_align_new, "\\}"),
Hao Zhu3fc0e882018-04-03 16:06:41 -0400236 solve_enc(kable_input),
Hao Zhubff01912017-05-23 18:05:00 -0400237 perl = T)
Hao Zhuae80df42018-04-12 15:45:11 -0400238
239 if (!is.null(width)) {
240 fix_newline <- replace_makecell_with_newline(out, table_info, column)
241 out <- fix_newline[[1]]
242 table_info <- fix_newline[[2]]
243 }
244
Hao Zhubff01912017-05-23 18:05:00 -0400245 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000246 if (!is.null(width)) {
247 if (is.null(table_info$column_width)) {
248 table_info$column_width <- list()
249 }
Hao Zhu322de082017-09-11 19:25:29 -0400250 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400251 table_info$column_width[[paste0("column_", i)]] <- width
252 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000253 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400254 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400255 return(out)
256}
Hao Zhu32f43f72017-06-20 18:24:54 -0400257
Hao Zhua73601b2017-08-19 15:31:51 -0400258latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500259 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400260 color, background,
261 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400262 extra_align <- ""
263 if (!is.null(width)) {
264 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400265 "l" = "\\\\raggedright\\\\arraybackslash",
266 "c" = "\\\\centering\\\\arraybackslash",
267 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400268 x <- paste0("p\\{", width, "\\}")
269 }
270
Hao Zhua73601b2017-08-19 15:31:51 -0400271 if (!is.null(color)) {
Stefanie LaZerte994fc562017-12-11 14:14:07 -0600272 color <- paste0("\\\\leavevmode\\\\color", latex_color(color))
Hao Zhu32f43f72017-06-20 18:24:54 -0400273 }
274
Hao Zhuf2ebf9e2017-09-14 11:40:49 -0400275 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500276 background <- paste0("\\\\columncolor", latex_color(background))
Hao Zhuec7ab922017-08-19 22:56:44 -0400277 }
278
Hao Zhuef0c8302018-01-12 13:30:20 -0500279 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily",
280 "\\\\underline", "\\\\sout")[
Hao Zhub49bddf2018-01-12 15:25:23 -0500281 c(bold, italic, monospace, underline, strikeout)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400282 latex_array_options <- c(latex_array_options, extra_align,
283 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400284 latex_array_options <- paste0(
285 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
286 )
287 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400288 if (border_left) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500289 x <- paste0("\\|", x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400290 }
291 if (border_right) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500292 x <- paste0(x, "\\|")
Hao Zhuec7ab922017-08-19 22:56:44 -0400293 }
Hao Zhua73601b2017-08-19 15:31:51 -0400294
Hao Zhu32f43f72017-06-20 18:24:54 -0400295 return(x)
296}
Hao Zhuae80df42018-04-12 15:45:11 -0400297
298replace_makecell_with_newline <- function(kable_input, table_info, column) {
299 if (!str_detect(kable_input, "makecell")) return(list(kable_input, table_info))
300 contents_table <- data.frame(sapply(table_info$contents,
301 function(x) {str_split(x, " \\& ")[[1]]}),
302 stringsAsFactors = F)
303 names(contents_table) <- paste0("x", 1:table_info$nrow)
304 rows_check_makecell <- str_detect(contents_table[column, ], "makecell")
305 if (sum(rows_check_makecell) == 0) return(list(kable_input, table_info))
306 rows_to_replace <- which(rows_check_makecell)
307
308 for (i in column) {
309 target_column <- contents_table[i, ]
310 for (j in which(str_detect(target_column, "\\\\\\\\makecell"))) {
311 contents_table[i, j] <- str_replace(
312 contents_table[i, j], "\\\\\\\\makecell\\\\\\[.\\\\\\]\\\\\\{", "")
313 contents_table[i, j] <- str_replace(
Hao Zhu9ac3e382018-04-12 18:56:32 -0400314 contents_table[i, j], "\\\\\\}$", "")
Hao Zhuae80df42018-04-12 15:45:11 -0400315 contents_table[i, j] <- str_replace_all(
316 contents_table[i, j], "\\\\\\\\\\\\\\\\", "\\\\\\\\newline "
317 )
318 }
319 }
320
321 new_contents <- unlist(lapply(contents_table, paste, collapse = " & "))
322 for (i in rows_to_replace) {
323 kable_input <- sub(table_info$contents[i], new_contents[i], kable_input,
324 perl = T)
325 table_info$contents[i] <- new_contents[i]
326 }
327
328 return(list(kable_input, table_info))
329}