blob: 377ceb56cffb21af3a68734ec1d0da6c488c31fc [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 Zhub1de9672018-01-08 16:29:24 -050030#' @param extra_css Extra css text to be passed into the cells of the row. Note
31#' that it's not for the whole column but to each individual cells
Hao Zhu78e61222017-05-24 20:53:35 -040032#'
33#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040034#' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
Hao Zhu78e61222017-05-24 20:53:35 -040035#'
Hao Zhubff01912017-05-23 18:05:00 -040036#' @export
Hao Zhu322de082017-09-11 19:25:29 -040037column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040038 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050039 monospace = FALSE, underline = FALSE, strikeout = FALSE,
40 color = NULL, background = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050041 border_left = FALSE, border_right = FALSE,
42 extra_css = NULL) {
Hao Zhu322de082017-09-11 19:25:29 -040043 if (!is.numeric(column)) {
44 stop("column must be numeric. ")
Hao Zhubff01912017-05-23 18:05:00 -040045 }
46 kable_format <- attr(kable_input, "format")
47 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050048 warning("Please specify format in kable. kableExtra can customize either ",
49 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
50 "for details.")
Hao Zhubff01912017-05-23 18:05:00 -040051 return(kable_input)
52 }
53 if (kable_format == "html") {
Hao Zhu322de082017-09-11 19:25:29 -040054 return(column_spec_html(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040055 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050056 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040057 color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050058 border_left, border_right, extra_css))
Hao Zhubff01912017-05-23 18:05:00 -040059 }
60 if (kable_format == "latex") {
Hao Zhu322de082017-09-11 19:25:29 -040061 return(column_spec_latex(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 Zhu4840bc92017-09-15 15:55:05 -040065 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040066 }
67}
68
Hao Zhu322de082017-09-11 19:25:29 -040069column_spec_html <- function(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040070 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050071 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040072 color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050073 border_left, border_right, extra_css) {
Hao Zhubff01912017-05-23 18:05:00 -040074 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040075 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040076 kable_tbody <- xml_tpart(kable_xml, "tbody")
77
78 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -040079 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -040080
Hao Zhubff01912017-05-23 18:05:00 -040081 if (!is.null(group_header_rows)) {
82 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
83 group_header_rows]
84 }
85
Hao Zhuec7ab922017-08-19 22:56:44 -040086 # Border css
87 border_l_css <- "1px solid"
88 border_r_css <- "1px solid"
89 if (is.character(border_left)) {
90 border_l_css <- border_left
91 border_left <- T
92 }
93 if (is.character(border_right)) {
94 border_r_css <- border_right
95 border_right <- T
96 }
97
Hao Zhubff01912017-05-23 18:05:00 -040098 for (i in all_contents_rows) {
Hao Zhu322de082017-09-11 19:25:29 -040099 for (j in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400100 target_cell <- xml_child(xml_child(kable_tbody, i), j)
101 if (!is.null(width)) {
102 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhu6a14e882017-10-31 17:04:12 -0400103 "width: ", width, "; ")
Hao Zhu9b43f622017-09-11 19:00:08 -0400104 }
105 if (bold) {
106 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
107 "font-weight: bold;")
108 }
109 if (italic) {
110 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
111 "font-style: italic;")
112 }
113 if (monospace) {
114 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
115 "font-family: monospace;")
116 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500117 if (underline) {
118 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhub49bddf2018-01-12 15:25:23 -0500119 "text-decoration: underline;")
Hao Zhuef0c8302018-01-12 13:30:20 -0500120 }
121 if (strikeout) {
122 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhub49bddf2018-01-12 15:25:23 -0500123 "text-decoration: line-through;")
Hao Zhuef0c8302018-01-12 13:30:20 -0500124 }
Hao Zhu9b43f622017-09-11 19:00:08 -0400125 if (!is.null(color)) {
126 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
127 "color: ", color, ";")
128 }
129 if (!is.null(background)) {
130 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
131 "background-color: ",
132 background, ";")
133 }
134 if (border_left) {
135 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
136 "border-left:", border_l_css, ";")
137 }
138 if (border_right) {
139 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
140 "border-right:", border_r_css, ";")
141 }
Hao Zhub1de9672018-01-08 16:29:24 -0500142 if (!is.null(extra_css)) {
143 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
144 extra_css)
145 }
Hao Zhuec7ab922017-08-19 22:56:44 -0400146 }
Hao Zhubff01912017-05-23 18:05:00 -0400147 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400148
149
Hao Zhuf2dfd142017-07-24 14:43:28 -0400150 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400151 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500152 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhubff01912017-05-23 18:05:00 -0400153 return(out)
154}
155
Hao Zhu322de082017-09-11 19:25:29 -0400156column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400157 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500158 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400159 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -0400160 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -0400161 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000162 if (!is.null(table_info$collapse_rows)) {
163 message("Usually it is recommended to use column_spec before collapse_rows,",
164 " especially in LaTeX, to get a desired result. ")
165 }
Hao Zhubff01912017-05-23 18:05:00 -0400166 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
167 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
168
Hao Zhu322de082017-09-11 19:25:29 -0400169 table_info$align_vector[column] <- unlist(lapply(
170 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400171 function(x) {
172 latex_column_align_builder(
Hao Zhuef0c8302018-01-12 13:30:20 -0500173 x, width, bold, italic, monospace, underline, strikeout,
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400174 color, background, border_left, border_right)
175 }
176 ))
Hao Zhubff01912017-05-23 18:05:00 -0400177
178 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
179
Hao Zhud2c0f732017-08-26 10:40:14 -0400180 out <- sub(kable_align_old, kable_align_new,
Hao Zhu3fc0e882018-04-03 16:06:41 -0400181 solve_enc(kable_input),
Hao Zhubff01912017-05-23 18:05:00 -0400182 perl = T)
Hao Zhuae80df42018-04-12 15:45:11 -0400183
184 if (!is.null(width)) {
185 fix_newline <- replace_makecell_with_newline(out, table_info, column)
186 out <- fix_newline[[1]]
187 table_info <- fix_newline[[2]]
188 }
189
Hao Zhubff01912017-05-23 18:05:00 -0400190 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000191 if (!is.null(width)) {
192 if (is.null(table_info$column_width)) {
193 table_info$column_width <- list()
194 }
Hao Zhu322de082017-09-11 19:25:29 -0400195 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400196 table_info$column_width[[paste0("column_", i)]] <- width
197 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000198 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400199 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400200 return(out)
201}
Hao Zhu32f43f72017-06-20 18:24:54 -0400202
Hao Zhua73601b2017-08-19 15:31:51 -0400203latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500204 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400205 color, background,
206 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400207 extra_align <- ""
208 if (!is.null(width)) {
209 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400210 "l" = "\\\\raggedright\\\\arraybackslash",
211 "c" = "\\\\centering\\\\arraybackslash",
212 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400213 x <- paste0("p\\{", width, "\\}")
214 }
215
Hao Zhua73601b2017-08-19 15:31:51 -0400216 if (!is.null(color)) {
Stefanie LaZerte994fc562017-12-11 14:14:07 -0600217 color <- paste0("\\\\leavevmode\\\\color", latex_color(color))
Hao Zhu32f43f72017-06-20 18:24:54 -0400218 }
219
Hao Zhuf2ebf9e2017-09-14 11:40:49 -0400220 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500221 background <- paste0("\\\\columncolor", latex_color(background))
Hao Zhuec7ab922017-08-19 22:56:44 -0400222 }
223
Hao Zhuef0c8302018-01-12 13:30:20 -0500224 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily",
225 "\\\\underline", "\\\\sout")[
Hao Zhub49bddf2018-01-12 15:25:23 -0500226 c(bold, italic, monospace, underline, strikeout)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400227 latex_array_options <- c(latex_array_options, extra_align,
228 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400229 latex_array_options <- paste0(
230 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
231 )
232 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400233 if (border_left) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500234 x <- paste0("\\|", x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400235 }
236 if (border_right) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500237 x <- paste0(x, "\\|")
Hao Zhuec7ab922017-08-19 22:56:44 -0400238 }
Hao Zhua73601b2017-08-19 15:31:51 -0400239
Hao Zhu32f43f72017-06-20 18:24:54 -0400240 return(x)
241}
Hao Zhuae80df42018-04-12 15:45:11 -0400242
243replace_makecell_with_newline <- function(kable_input, table_info, column) {
244 if (!str_detect(kable_input, "makecell")) return(list(kable_input, table_info))
245 contents_table <- data.frame(sapply(table_info$contents,
246 function(x) {str_split(x, " \\& ")[[1]]}),
247 stringsAsFactors = F)
248 names(contents_table) <- paste0("x", 1:table_info$nrow)
249 rows_check_makecell <- str_detect(contents_table[column, ], "makecell")
250 if (sum(rows_check_makecell) == 0) return(list(kable_input, table_info))
251 rows_to_replace <- which(rows_check_makecell)
252
253 for (i in column) {
254 target_column <- contents_table[i, ]
255 for (j in which(str_detect(target_column, "\\\\\\\\makecell"))) {
256 contents_table[i, j] <- str_replace(
257 contents_table[i, j], "\\\\\\\\makecell\\\\\\[.\\\\\\]\\\\\\{", "")
258 contents_table[i, j] <- str_replace(
259 contents_table[i, j], "\\}$", "")
260 contents_table[i, j] <- str_replace_all(
261 contents_table[i, j], "\\\\\\\\\\\\\\\\", "\\\\\\\\newline "
262 )
263 }
264 }
265
266 new_contents <- unlist(lapply(contents_table, paste, collapse = " & "))
267 for (i in rows_to_replace) {
268 kable_input <- sub(table_info$contents[i], new_contents[i], kable_input,
269 perl = T)
270 table_info$contents[i] <- new_contents[i]
271 }
272
273 return(list(kable_input, table_info))
274}