blob: f6b35933dec61f59e62f7ff581df554fcbb4f202 [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 Zhub1caa272018-04-14 14:19:46 -040030#' @param min_width Only for HTML table. Normal column width will automatically
31#' collapse when the window cannot hold enough contents. With this `min_width`,
32#' you can set up a column with a width that won't collapse even when the
33#' window is not wide enough.
34#' @param max_width Only for HTML table. `max_width` defines the maximum width
35#' 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 Zhu78e61222017-05-24 20:53:35 -040038#'
39#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040040#' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
Hao Zhu78e61222017-05-24 20:53:35 -040041#'
Hao Zhubff01912017-05-23 18:05:00 -040042#' @export
Hao Zhu322de082017-09-11 19:25:29 -040043column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040044 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050045 monospace = FALSE, underline = FALSE, strikeout = FALSE,
46 color = NULL, background = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050047 border_left = FALSE, border_right = FALSE,
Hao Zhub1caa272018-04-14 14:19:46 -040048 width_min = NULL, width_max = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050049 extra_css = NULL) {
Hao Zhu322de082017-09-11 19:25:29 -040050 if (!is.numeric(column)) {
51 stop("column must be numeric. ")
Hao Zhubff01912017-05-23 18:05:00 -040052 }
53 kable_format <- attr(kable_input, "format")
54 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050055 warning("Please specify format in kable. kableExtra can customize either ",
56 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
57 "for details.")
Hao Zhubff01912017-05-23 18:05:00 -040058 return(kable_input)
59 }
60 if (kable_format == "html") {
Hao Zhu322de082017-09-11 19:25:29 -040061 return(column_spec_html(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040062 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050063 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040064 color, background,
Hao Zhub1caa272018-04-14 14:19:46 -040065 border_left, border_right,
66 width_min, width_max,
67 extra_css))
Hao Zhubff01912017-05-23 18:05:00 -040068 }
69 if (kable_format == "latex") {
Hao Zhu322de082017-09-11 19:25:29 -040070 return(column_spec_latex(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040071 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050072 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040073 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -040074 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040075 }
76}
77
Hao Zhu322de082017-09-11 19:25:29 -040078column_spec_html <- function(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040079 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050080 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040081 color, background,
Hao Zhub1caa272018-04-14 14:19:46 -040082 border_left, border_right,
83 width_min, width_max,
84 extra_css) {
Hao Zhubff01912017-05-23 18:05:00 -040085 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040086 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040087 kable_tbody <- xml_tpart(kable_xml, "tbody")
88
89 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -040090 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -040091
Hao Zhubff01912017-05-23 18:05:00 -040092 if (!is.null(group_header_rows)) {
93 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
94 group_header_rows]
95 }
96
Hao Zhuec7ab922017-08-19 22:56:44 -040097 # Border css
98 border_l_css <- "1px solid"
99 border_r_css <- "1px solid"
100 if (is.character(border_left)) {
101 border_l_css <- border_left
102 border_left <- T
103 }
104 if (is.character(border_right)) {
105 border_r_css <- border_right
106 border_right <- T
107 }
108
Hao Zhubff01912017-05-23 18:05:00 -0400109 for (i in all_contents_rows) {
Hao Zhu322de082017-09-11 19:25:29 -0400110 for (j in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400111 target_cell <- xml_child(xml_child(kable_tbody, i), j)
112 if (!is.null(width)) {
113 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhu6a14e882017-10-31 17:04:12 -0400114 "width: ", width, "; ")
Hao Zhu9b43f622017-09-11 19:00:08 -0400115 }
Hao Zhub1caa272018-04-14 14:19:46 -0400116 if (!is.null(width_min)) {
117 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
118 "min-width: ", width_min, "; ")
119 }
120 if (!is.null(width_max)) {
121 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
122 "max-width: ", width_max, "; ")
123 }
Hao Zhu9b43f622017-09-11 19:00:08 -0400124 if (bold) {
125 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
126 "font-weight: bold;")
127 }
128 if (italic) {
129 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
130 "font-style: italic;")
131 }
132 if (monospace) {
133 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
134 "font-family: monospace;")
135 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500136 if (underline) {
137 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhub49bddf2018-01-12 15:25:23 -0500138 "text-decoration: underline;")
Hao Zhuef0c8302018-01-12 13:30:20 -0500139 }
140 if (strikeout) {
141 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhub49bddf2018-01-12 15:25:23 -0500142 "text-decoration: line-through;")
Hao Zhuef0c8302018-01-12 13:30:20 -0500143 }
Hao Zhu9b43f622017-09-11 19:00:08 -0400144 if (!is.null(color)) {
145 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
146 "color: ", color, ";")
147 }
148 if (!is.null(background)) {
149 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
150 "background-color: ",
151 background, ";")
152 }
153 if (border_left) {
154 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
155 "border-left:", border_l_css, ";")
156 }
157 if (border_right) {
158 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
159 "border-right:", border_r_css, ";")
160 }
Hao Zhub1de9672018-01-08 16:29:24 -0500161 if (!is.null(extra_css)) {
162 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
163 extra_css)
164 }
Hao Zhuec7ab922017-08-19 22:56:44 -0400165 }
Hao Zhubff01912017-05-23 18:05:00 -0400166 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400167
168
Hao Zhuf2dfd142017-07-24 14:43:28 -0400169 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400170 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500171 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhubff01912017-05-23 18:05:00 -0400172 return(out)
173}
174
Hao Zhu322de082017-09-11 19:25:29 -0400175column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400176 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500177 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400178 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -0400179 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -0400180 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000181 if (!is.null(table_info$collapse_rows)) {
182 message("Usually it is recommended to use column_spec before collapse_rows,",
183 " especially in LaTeX, to get a desired result. ")
184 }
Hao Zhubff01912017-05-23 18:05:00 -0400185 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
186 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
187
Hao Zhu322de082017-09-11 19:25:29 -0400188 table_info$align_vector[column] <- unlist(lapply(
189 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400190 function(x) {
191 latex_column_align_builder(
Hao Zhuef0c8302018-01-12 13:30:20 -0500192 x, width, bold, italic, monospace, underline, strikeout,
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400193 color, background, border_left, border_right)
194 }
195 ))
Hao Zhubff01912017-05-23 18:05:00 -0400196
197 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
198
Hao Zhud2c0f732017-08-26 10:40:14 -0400199 out <- sub(kable_align_old, kable_align_new,
Hao Zhu3fc0e882018-04-03 16:06:41 -0400200 solve_enc(kable_input),
Hao Zhubff01912017-05-23 18:05:00 -0400201 perl = T)
Hao Zhuae80df42018-04-12 15:45:11 -0400202
203 if (!is.null(width)) {
204 fix_newline <- replace_makecell_with_newline(out, table_info, column)
205 out <- fix_newline[[1]]
206 table_info <- fix_newline[[2]]
207 }
208
Hao Zhubff01912017-05-23 18:05:00 -0400209 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000210 if (!is.null(width)) {
211 if (is.null(table_info$column_width)) {
212 table_info$column_width <- list()
213 }
Hao Zhu322de082017-09-11 19:25:29 -0400214 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400215 table_info$column_width[[paste0("column_", i)]] <- width
216 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000217 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400218 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400219 return(out)
220}
Hao Zhu32f43f72017-06-20 18:24:54 -0400221
Hao Zhua73601b2017-08-19 15:31:51 -0400222latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500223 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400224 color, background,
225 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400226 extra_align <- ""
227 if (!is.null(width)) {
228 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400229 "l" = "\\\\raggedright\\\\arraybackslash",
230 "c" = "\\\\centering\\\\arraybackslash",
231 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400232 x <- paste0("p\\{", width, "\\}")
233 }
234
Hao Zhua73601b2017-08-19 15:31:51 -0400235 if (!is.null(color)) {
Stefanie LaZerte994fc562017-12-11 14:14:07 -0600236 color <- paste0("\\\\leavevmode\\\\color", latex_color(color))
Hao Zhu32f43f72017-06-20 18:24:54 -0400237 }
238
Hao Zhuf2ebf9e2017-09-14 11:40:49 -0400239 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500240 background <- paste0("\\\\columncolor", latex_color(background))
Hao Zhuec7ab922017-08-19 22:56:44 -0400241 }
242
Hao Zhuef0c8302018-01-12 13:30:20 -0500243 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily",
244 "\\\\underline", "\\\\sout")[
Hao Zhub49bddf2018-01-12 15:25:23 -0500245 c(bold, italic, monospace, underline, strikeout)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400246 latex_array_options <- c(latex_array_options, extra_align,
247 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400248 latex_array_options <- paste0(
249 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
250 )
251 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400252 if (border_left) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500253 x <- paste0("\\|", x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400254 }
255 if (border_right) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500256 x <- paste0(x, "\\|")
Hao Zhuec7ab922017-08-19 22:56:44 -0400257 }
Hao Zhua73601b2017-08-19 15:31:51 -0400258
Hao Zhu32f43f72017-06-20 18:24:54 -0400259 return(x)
260}
Hao Zhuae80df42018-04-12 15:45:11 -0400261
262replace_makecell_with_newline <- function(kable_input, table_info, column) {
263 if (!str_detect(kable_input, "makecell")) return(list(kable_input, table_info))
264 contents_table <- data.frame(sapply(table_info$contents,
265 function(x) {str_split(x, " \\& ")[[1]]}),
266 stringsAsFactors = F)
267 names(contents_table) <- paste0("x", 1:table_info$nrow)
268 rows_check_makecell <- str_detect(contents_table[column, ], "makecell")
269 if (sum(rows_check_makecell) == 0) return(list(kable_input, table_info))
270 rows_to_replace <- which(rows_check_makecell)
271
272 for (i in column) {
273 target_column <- contents_table[i, ]
274 for (j in which(str_detect(target_column, "\\\\\\\\makecell"))) {
275 contents_table[i, j] <- str_replace(
276 contents_table[i, j], "\\\\\\\\makecell\\\\\\[.\\\\\\]\\\\\\{", "")
277 contents_table[i, j] <- str_replace(
Hao Zhu9ac3e382018-04-12 18:56:32 -0400278 contents_table[i, j], "\\\\\\}$", "")
Hao Zhuae80df42018-04-12 15:45:11 -0400279 contents_table[i, j] <- str_replace_all(
280 contents_table[i, j], "\\\\\\\\\\\\\\\\", "\\\\\\\\newline "
281 )
282 }
283 }
284
285 new_contents <- unlist(lapply(contents_table, paste, collapse = " & "))
286 for (i in rows_to_replace) {
287 kable_input <- sub(table_info$contents[i], new_contents[i], kable_input,
288 perl = T)
289 table_info$contents[i] <- new_contents[i]
290 }
291
292 return(list(kable_input, table_info))
293}