blob: 07962ed2ecde88c08f197f48ca1897b9a07bdcef [file] [log] [blame]
Hao Zhu79f1e2a2017-06-11 20:55:30 -04001#' Specify the look of the selected row
2#'
3#' @description This function allows users to select a row and then specify
Hao Zhu8d347c42017-10-10 13:11:19 -04004#' its look. It can also specify the format of the header row when `row` = 0.
Hao Zhu79f1e2a2017-06-11 20:55:30 -04005#'
6#' @param kable_input Output of `knitr::kable()` with `format` specified
Hao Zhu322de082017-09-11 19:25:29 -04007#' @param row A numeric value or vector indicating which row(s) to be selected. You don't
Hao Zhu909ea382017-06-12 15:43:47 -04008#' need to count in header rows or group labeling rows.
Hao Zhu79f1e2a2017-06-11 20:55:30 -04009#' @param bold A T/F value to control whether the text of the selected row
10#' need to be bolded.
11#' @param italic A T/F value to control whether the text of the selected row
12#' need to be emphasized.
Hao Zhuef0c8302018-01-12 13:30:20 -050013#' @param monospace A T/F value to control whether the text of the selected row
Hao Zhu3e53e602017-07-26 12:40:57 -040014#' need to be monospaced (verbatim)
Hao Zhuef0c8302018-01-12 13:30:20 -050015#' @param underline A T/F value to control whether the text of the selected row
16#' need to be underlined
17#' @param strikeout A T/F value to control whether the text of the selected row
18#' need to be stricked out.
Hao Zhu457acb42017-10-14 17:37:02 -040019#' @param color A character string for row text color. For example, "red" or
20#' "#BBBBBB".
Hao Zhu8d347c42017-10-10 13:11:19 -040021#' @param background A character string for row background color. Here please
Hao Zhu53e240f2017-09-04 20:04:29 -040022#' pay attention to the differences in color codes between HTML and LaTeX.
Hao Zhu8d347c42017-10-10 13:11:19 -040023#' @param align A character string for cell alignment. For HTML, possible values could
24#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
25#' while for LaTeX, you can only choose from `l`, `c` & `r`.
Hao Zhu8b32b192017-10-24 14:51:48 -040026#' @param font_size A numeric input for font size. For HTML, you can also use
27#' options including `xx-small`, `x-small`, `small`, `medium`, `large`,
28#' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`.
Hao Zhubacd2f32017-10-11 14:06:36 -040029#' @param angle 0-360, degree that the text will rotate.
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 row.
Hao Zhu53454f02018-01-14 16:29:10 -050032#' @param hline_after T/F. A replicate of `hline.after` in xtable. It
33#' addes a hline after ther row
34#' @param extra_latex_after Extra LaTeX text to be added after the row. Similar
35#' with `add.to.row` in xtable
Hao Zhu79f1e2a2017-06-11 20:55:30 -040036#'
37#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040038#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040039#'
40#' @export
41row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040042 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050043 underline = FALSE, strikeout = FALSE,
Hao Zhubacd2f32017-10-11 14:06:36 -040044 color = NULL, background = NULL, align = NULL,
Hao Zhu53454f02018-01-14 16:29:10 -050045 font_size = NULL, angle = NULL, extra_css = NULL,
46 hline_after = FALSE, extra_latex_after = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040047 if (!is.numeric(row)) {
Hao Zhu322de082017-09-11 19:25:29 -040048 stop("row must be numeric. ")
Hao Zhu79f1e2a2017-06-11 20:55:30 -040049 }
50 kable_format <- attr(kable_input, "format")
51 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050052 warning("Please specify format in kable. kableExtra can customize either ",
53 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
54 "for details.")
Hao Zhu79f1e2a2017-06-11 20:55:30 -040055 return(kable_input)
56 }
57 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040058 return(row_spec_html(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050059 underline, strikeout,
Hao Zhub1de9672018-01-08 16:29:24 -050060 color, background, align, font_size, angle,
61 extra_css))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040062 }
63 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040064 return(row_spec_latex(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050065 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -050066 color, background, align, font_size, angle,
67 hline_after, extra_latex_after))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040068 }
69}
70
Hao Zhu669bcd22017-08-19 14:53:40 -040071row_spec_html <- function(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050072 underline, strikeout,
Hao Zhub1de9672018-01-08 16:29:24 -050073 color, background, align, font_size, angle,
74 extra_css) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040075 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040076 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040077
Hao Zhu8d347c42017-10-10 13:11:19 -040078 if (!is.null(align)) {
79 if (align %in% c("l", "c", "r")) {
80 align <- switch(align, r = "right", c = "center", l = "left")
81 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040082 }
83
Hao Zhu8d347c42017-10-10 13:11:19 -040084 if (0 %in% row) {
85 kable_thead <- xml_tpart(kable_xml, "thead")
86 original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
87 for (theader_i in 1:length(xml_children(original_header_row))) {
88 target_header_cell <- xml_child(original_header_row, theader_i)
Hao Zhuef0c8302018-01-12 13:30:20 -050089 xml_cell_style(target_header_cell, bold, italic, monospace,
90 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050091 align, font_size, angle, extra_css)
Hao Zhu8d347c42017-10-10 13:11:19 -040092 }
93 row <- row[row != 0]
94 }
95
96 if (length(row) != 0) {
97 kable_tbody <- xml_tpart(kable_xml, "tbody")
98
99 group_header_rows <- attr(kable_input, "group_header_rows")
100 if (!is.null(group_header_rows)) {
101 row <- positions_corrector(row, group_header_rows,
102 length(xml_children(kable_tbody)))
103 }
104
105 for (j in row) {
106 target_row <- xml_child(kable_tbody, j)
107 for (i in 1:length(xml_children(target_row))) {
108 target_cell <- xml_child(target_row, i)
Hao Zhuef0c8302018-01-12 13:30:20 -0500109 xml_cell_style(target_cell, bold, italic, monospace,
110 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -0500111 align, font_size, angle, extra_css)
Hao Zhu322de082017-09-11 19:25:29 -0400112 }
Hao Zhu669bcd22017-08-19 14:53:40 -0400113 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400114 }
Hao Zhu322de082017-09-11 19:25:29 -0400115
Hao Zhuf2dfd142017-07-24 14:43:28 -0400116 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400117 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500118 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400119 return(out)
120}
121
Hao Zhuef0c8302018-01-12 13:30:20 -0500122xml_cell_style <- function(x, bold, italic, monospace,
123 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -0500124 align, font_size, angle, extra_css) {
Hao Zhu8d347c42017-10-10 13:11:19 -0400125 if (bold) {
126 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400127 "font-weight: bold;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400128 }
129 if (italic) {
130 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400131 "font-style: italic;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400132 }
133 if (monospace) {
134 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400135 "font-family: monospace;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400136 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500137 if (underline) {
138 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
139 "text-decoration: underline;")
140 }
141 if (strikeout) {
142 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
143 "text-decoration: line-through;")
144 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400145 if (!is.null(color)) {
146 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400147 "color: ", color, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400148 }
149 if (!is.null(background)) {
150 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400151 "background-color: ",
152 background, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400153 }
154 if (!is.null(align)) {
155 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
156 "text-align: ", align, ";")
157 }
Hao Zhue7c8f702017-10-10 13:22:59 -0400158 if (!is.null(font_size)) {
159 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
160 "font-size: ", font_size, "px;")
161 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400162 if (!is.null(angle)) {
163 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
164 "-webkit-transform: rotate(", angle,
165 "deg); -moz-transform: rotate(", angle,
166 "deg); -ms-transform: rotate(", angle,
167 "deg); -o-transform: rotate(", angle,
Hao Zhu457acb42017-10-14 17:37:02 -0400168 "deg); transform: rotate(", angle,
Hao Zhubacd2f32017-10-11 14:06:36 -0400169 "deg);")
170 }
Hao Zhub1de9672018-01-08 16:29:24 -0500171 if (!is.null(extra_css)) {
172 xml_attr(x, "style") <- paste0(xml_attr(x, "style"), extra_css)
173 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400174 return(x)
175}
176
Hao Zhua73601b2017-08-19 15:31:51 -0400177row_spec_latex <- function(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500178 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -0500179 color, background, align, font_size, angle,
180 hline_after, extra_latex_after) {
Hao Zhu73604282017-06-11 22:08:48 -0400181 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -0400182 out <- solve_enc(kable_input)
Hao Zhu064990d2017-10-17 18:08:42 -0400183
184 if (table_info$duplicated_rows) {
185 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
186 out <- dup_fx_out[[1]]
187 table_info <- dup_fx_out[[2]]
188 }
189
Hao Zhu37dbe3f2018-05-14 11:16:06 -0400190 row <- row + table_info$position_offset
Hao Zhu322de082017-09-11 19:25:29 -0400191 for (i in row) {
192 target_row <- table_info$contents[i]
Hao Zhue13a3e72018-04-23 23:52:13 -0400193 new_row <- latex_new_row_builder(target_row, table_info,
194 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500195 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -0500196 color, background, align, font_size, angle,
197 hline_after, extra_latex_after)
Hao Zhuae80df42018-04-12 15:45:11 -0400198 if (length(new_row) == 1) {
199 out <- sub(target_row, new_row, out, perl = T)
200 } else {
201 out <- sub(paste0(target_row, "\\\\\\\\"),
202 paste(new_row, collapse = ""), out, perl = T)
203 }
204 table_info$contents[i] <- new_row
Hao Zhu322de082017-09-11 19:25:29 -0400205 }
206
207 out <- structure(out, format = "latex", class = "knitr_kable")
208 attr(out, "kable_meta") <- table_info
209 return(out)
210}
211
Hao Zhue13a3e72018-04-23 23:52:13 -0400212latex_new_row_builder <- function(target_row, table_info,
213 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500214 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -0500215 color, background, align, font_size, angle,
216 hline_after, extra_latex_after) {
Hao Zhu73604282017-06-11 22:08:48 -0400217 new_row <- latex_row_cells(target_row)
218 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400219 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400220 paste0("\\\\textbf\\{", x, "\\}")
Hao Zhu35ecd272017-06-11 22:50:42 -0400221 })
Hao Zhu73604282017-06-11 22:08:48 -0400222 }
223 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400224 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400225 paste0("\\\\em\\{", x, "\\}")
Hao Zhu35ecd272017-06-11 22:50:42 -0400226 })
Hao Zhu73604282017-06-11 22:08:48 -0400227 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400228 if (monospace) {
229 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400230 paste0("\\\\ttfamily\\{", x, "\\}")
Hao Zhu3e53e602017-07-26 12:40:57 -0400231 })
232 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500233 if (underline) {
234 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400235 paste0("\\\\underline\\{", x, "\\}")
Hao Zhuef0c8302018-01-12 13:30:20 -0500236 })
237 }
238 if (strikeout) {
239 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400240 paste0("\\\\sout\\{", x, "\\}")
Hao Zhuef0c8302018-01-12 13:30:20 -0500241 })
242 }
Hao Zhua73601b2017-08-19 15:31:51 -0400243 if (!is.null(color)) {
244 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400245 paste0("\\\\textcolor", latex_color(color), "\\{", x, "\\}")
Hao Zhua73601b2017-08-19 15:31:51 -0400246 })
247 }
Hao Zhu8b32b192017-10-24 14:51:48 -0400248 if (!is.null(font_size)) {
249 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400250 paste0("\\\\begingroup\\\\fontsize\\{", font_size, "\\}\\{",
Hao Zhu8b32b192017-10-24 14:51:48 -0400251 as.numeric(font_size) + 2,
Hao Zhuae80df42018-04-12 15:45:11 -0400252 "\\}\\\\selectfont ", x, "\\\\endgroup")})
Hao Zhu8b32b192017-10-24 14:51:48 -0400253 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400254 if (!is.null(align)) {
Hao Zhue13a3e72018-04-23 23:52:13 -0400255 if (!is.null(table_info$column_width)) {
256 p_align <- switch(align,
257 "l" = "\\\\raggedright\\\\arraybackslash",
258 "c" = "\\\\centering\\\\arraybackslash",
259 "r" = "\\\\raggedleft\\\\arraybackslash")
260 align <- rep(align, table_info$ncol)
261 p_cols <- as.numeric(sub("column_", "", names(table_info$column_width)))
262 for (i in 1:length(p_cols)) {
263 align[p_cols[i]] <- paste0("\\>\\{", p_align, "\\}p\\{",
264 table_info$column_width[[i]], "\\}")
265 }
266 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400267 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400268 paste0("\\\\multicolumn\\{1\\}\\{", align, "\\}\\{", x, "\\}")
Hao Zhu8d347c42017-10-10 13:11:19 -0400269 })
270 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400271
272 if (!is.null(angle)) {
273 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400274 paste0("\\\\rotatebox\\{", angle, "\\}\\{", x, "\\}")
Hao Zhubacd2f32017-10-11 14:06:36 -0400275 })
276 }
277
Hao Zhu35ecd272017-06-11 22:50:42 -0400278 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400279
Hao Zhua73601b2017-08-19 15:31:51 -0400280 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500281 new_row <- paste0("\\\\rowcolor", latex_color(background), " ", new_row)
Hao Zhua73601b2017-08-19 15:31:51 -0400282 }
283
Hao Zhuae80df42018-04-12 15:45:11 -0400284 if (!hline_after & is.null(extra_latex_after)) {
285 return(new_row)
286 } else {
287 latex_after <- "\\\\\\\\"
288 if (hline_after) {
289 latex_after <- paste0(latex_after, "\n\\\\hline")
290 }
291 if (!is.null(extra_latex_after)) {
292 latex_after <- paste0(latex_after, "\n",
293 regex_escape(extra_latex_after,
294 double_backslash = TRUE))
295 }
296 return(c(new_row, latex_after))
Hao Zhu53454f02018-01-14 16:29:10 -0500297 }
Hao Zhu73604282017-06-11 22:08:48 -0400298}