blob: 49b64b6c1251db697951011f8cea71f1b164afbb [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#'
Hao Zhu9399dcc2020-08-26 17:27:38 -040037#' @examples
38#' \dontrun{
39#' x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040040#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
Hao Zhu9399dcc2020-08-26 17:27:38 -040041#' }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040042#'
43#' @export
44row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040045 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050046 underline = FALSE, strikeout = FALSE,
Hao Zhubacd2f32017-10-11 14:06:36 -040047 color = NULL, background = NULL, align = NULL,
Hao Zhu53454f02018-01-14 16:29:10 -050048 font_size = NULL, angle = NULL, extra_css = NULL,
49 hline_after = FALSE, extra_latex_after = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040050 if (!is.numeric(row)) {
Hao Zhu322de082017-09-11 19:25:29 -040051 stop("row must be numeric. ")
Hao Zhu79f1e2a2017-06-11 20:55:30 -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 Zhu79f1e2a2017-06-11 20:55:30 -040058 return(kable_input)
59 }
60 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040061 return(row_spec_html(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050062 underline, strikeout,
Hao Zhub1de9672018-01-08 16:29:24 -050063 color, background, align, font_size, angle,
64 extra_css))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040065 }
66 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040067 return(row_spec_latex(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050068 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -050069 color, background, align, font_size, angle,
70 hline_after, extra_latex_after))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040071 }
72}
73
Hao Zhu669bcd22017-08-19 14:53:40 -040074row_spec_html <- function(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050075 underline, strikeout,
Hao Zhub1de9672018-01-08 16:29:24 -050076 color, background, align, font_size, angle,
77 extra_css) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040078 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040079 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040080
Hao Zhu8d347c42017-10-10 13:11:19 -040081 if (!is.null(align)) {
82 if (align %in% c("l", "c", "r")) {
83 align <- switch(align, r = "right", c = "center", l = "left")
84 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040085 }
86
Hao Zhu8d347c42017-10-10 13:11:19 -040087 if (0 %in% row) {
88 kable_thead <- xml_tpart(kable_xml, "thead")
89 original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
90 for (theader_i in 1:length(xml_children(original_header_row))) {
91 target_header_cell <- xml_child(original_header_row, theader_i)
Hao Zhuef0c8302018-01-12 13:30:20 -050092 xml_cell_style(target_header_cell, bold, italic, monospace,
93 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050094 align, font_size, angle, extra_css)
Hao Zhu8d347c42017-10-10 13:11:19 -040095 }
96 row <- row[row != 0]
97 }
98
99 if (length(row) != 0) {
100 kable_tbody <- xml_tpart(kable_xml, "tbody")
101
102 group_header_rows <- attr(kable_input, "group_header_rows")
103 if (!is.null(group_header_rows)) {
104 row <- positions_corrector(row, group_header_rows,
105 length(xml_children(kable_tbody)))
106 }
107
108 for (j in row) {
109 target_row <- xml_child(kable_tbody, j)
110 for (i in 1:length(xml_children(target_row))) {
111 target_cell <- xml_child(target_row, i)
Hao Zhuef0c8302018-01-12 13:30:20 -0500112 xml_cell_style(target_cell, bold, italic, monospace,
113 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -0500114 align, font_size, angle, extra_css)
Hao Zhu322de082017-09-11 19:25:29 -0400115 }
Hao Zhu669bcd22017-08-19 14:53:40 -0400116 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400117 }
Hao Zhu322de082017-09-11 19:25:29 -0400118
Hao Zhuf2dfd142017-07-24 14:43:28 -0400119 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400120 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500121 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400122 return(out)
123}
124
Hao Zhuef0c8302018-01-12 13:30:20 -0500125xml_cell_style <- function(x, bold, italic, monospace,
126 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -0500127 align, font_size, angle, extra_css) {
Hao Zhu517453c2018-05-13 13:07:18 -0400128 if (is.na(xml_attr(x, "style"))) {
129 xml_attr(x, "style") <- ""
130 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400131 if (bold) {
132 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400133 "font-weight: bold;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400134 }
135 if (italic) {
136 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400137 "font-style: italic;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400138 }
139 if (monospace) {
140 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400141 "font-family: monospace;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400142 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500143 if (underline) {
144 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
145 "text-decoration: underline;")
146 }
147 if (strikeout) {
148 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
149 "text-decoration: line-through;")
150 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400151 if (!is.null(color)) {
152 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhu72917f92019-03-15 18:41:42 -0400153 "color: ", html_color(color), " !important;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400154 }
155 if (!is.null(background)) {
156 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400157 "background-color: ",
Hao Zhu72917f92019-03-15 18:41:42 -0400158 html_color(background), " !important;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400159 }
160 if (!is.null(align)) {
161 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
162 "text-align: ", align, ";")
163 }
Hao Zhue7c8f702017-10-10 13:22:59 -0400164 if (!is.null(font_size)) {
165 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
166 "font-size: ", font_size, "px;")
167 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400168 if (!is.null(angle)) {
169 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
170 "-webkit-transform: rotate(", angle,
171 "deg); -moz-transform: rotate(", angle,
172 "deg); -ms-transform: rotate(", angle,
173 "deg); -o-transform: rotate(", angle,
Hao Zhu457acb42017-10-14 17:37:02 -0400174 "deg); transform: rotate(", angle,
Hao Zhubacd2f32017-10-11 14:06:36 -0400175 "deg);")
176 }
Hao Zhub1de9672018-01-08 16:29:24 -0500177 if (!is.null(extra_css)) {
178 xml_attr(x, "style") <- paste0(xml_attr(x, "style"), extra_css)
179 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400180 return(x)
181}
182
Hao Zhua73601b2017-08-19 15:31:51 -0400183row_spec_latex <- function(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500184 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -0500185 color, background, align, font_size, angle,
186 hline_after, extra_latex_after) {
Hao Zhu73604282017-06-11 22:08:48 -0400187 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -0400188 out <- solve_enc(kable_input)
Hao Zhu064990d2017-10-17 18:08:42 -0400189
190 if (table_info$duplicated_rows) {
191 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
192 out <- dup_fx_out[[1]]
193 table_info <- dup_fx_out[[2]]
194 }
195
Hao Zhu37dbe3f2018-05-14 11:16:06 -0400196 row <- row + table_info$position_offset
Hao Zhu322de082017-09-11 19:25:29 -0400197 for (i in row) {
198 target_row <- table_info$contents[i]
Hao Zhue13a3e72018-04-23 23:52:13 -0400199 new_row <- latex_new_row_builder(target_row, table_info,
200 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500201 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -0500202 color, background, align, font_size, angle,
203 hline_after, extra_latex_after)
Hao Zhu069fcef2020-08-04 00:12:29 -0400204 temp_sub <- ifelse(i == 1 & (table_info$tabular == "longtable" |
205 !is.null(table_info$repeat_header_latex)),
206 gsub, sub)
Hao Zhu17dbad12018-05-20 18:02:42 -0400207 if (length(new_row) == 1) {
208 out <- temp_sub(target_row, new_row, out, perl = T)
209 table_info$contents[i] <- new_row
Hao Zhuae80df42018-04-12 15:45:11 -0400210 } else {
Hao Zhu17dbad12018-05-20 18:02:42 -0400211 out <- temp_sub(paste0(target_row, "\\\\\\\\"),
212 paste(new_row, collapse = ""), out, perl = T)
213 table_info$contents[i] <- new_row[1]
Hao Zhuae80df42018-04-12 15:45:11 -0400214 }
Hao Zhu322de082017-09-11 19:25:29 -0400215 }
216
217 out <- structure(out, format = "latex", class = "knitr_kable")
218 attr(out, "kable_meta") <- table_info
219 return(out)
220}
221
Hao Zhue13a3e72018-04-23 23:52:13 -0400222latex_new_row_builder <- function(target_row, table_info,
223 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500224 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -0500225 color, background, align, font_size, angle,
226 hline_after, extra_latex_after) {
Hao Zhu73604282017-06-11 22:08:48 -0400227 new_row <- latex_row_cells(target_row)
228 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400229 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400230 paste0("\\\\textbf\\{", x, "\\}")
Hao Zhu35ecd272017-06-11 22:50:42 -0400231 })
Hao Zhu73604282017-06-11 22:08:48 -0400232 }
233 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400234 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400235 paste0("\\\\em\\{", x, "\\}")
Hao Zhu35ecd272017-06-11 22:50:42 -0400236 })
Hao Zhu73604282017-06-11 22:08:48 -0400237 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400238 if (monospace) {
239 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400240 paste0("\\\\ttfamily\\{", x, "\\}")
Hao Zhu3e53e602017-07-26 12:40:57 -0400241 })
242 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500243 if (underline) {
244 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400245 paste0("\\\\underline\\{", x, "\\}")
Hao Zhuef0c8302018-01-12 13:30:20 -0500246 })
247 }
248 if (strikeout) {
249 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400250 paste0("\\\\sout\\{", x, "\\}")
Hao Zhuef0c8302018-01-12 13:30:20 -0500251 })
252 }
Hao Zhua73601b2017-08-19 15:31:51 -0400253 if (!is.null(color)) {
Hao Zhuebeec792020-08-19 09:45:16 -0400254 if (table_info$tabular == "tabu") {
255 warning("Setting full_width = TRUE will turn the table into a tabu ",
256 "environment where colors are not really easily configable ",
257 "with this package. Please consider turn off full_width.")
258 }
Hao Zhua73601b2017-08-19 15:31:51 -0400259 new_row <- lapply(new_row, function(x) {
Hao Zhuc79d8542020-08-18 03:19:21 -0400260 x <- clear_color_latex(x)
Hao Zhuae80df42018-04-12 15:45:11 -0400261 paste0("\\\\textcolor", latex_color(color), "\\{", x, "\\}")
Hao Zhua73601b2017-08-19 15:31:51 -0400262 })
263 }
Hao Zhuc79d8542020-08-18 03:19:21 -0400264 if (!is.null(background)) {
Hao Zhuebeec792020-08-19 09:45:16 -0400265 if (table_info$tabular == "tabu") {
266 warning("Setting full_width = TRUE will turn the table into a tabu ",
267 "environment where colors are not really easily configable ",
268 "with this package. Please consider turn off full_width.")
269 }
Hao Zhuc79d8542020-08-18 03:19:21 -0400270 new_row <- lapply(new_row, function(x) {
271 x <- clear_color_latex(x, background = TRUE)
272 paste0("\\\\cellcolor", latex_color(background), "\\{", x, "\\}")
273 })
274 }
Hao Zhu8b32b192017-10-24 14:51:48 -0400275 if (!is.null(font_size)) {
276 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400277 paste0("\\\\begingroup\\\\fontsize\\{", font_size, "\\}\\{",
Hao Zhu8b32b192017-10-24 14:51:48 -0400278 as.numeric(font_size) + 2,
Hao Zhuae80df42018-04-12 15:45:11 -0400279 "\\}\\\\selectfont ", x, "\\\\endgroup")})
Hao Zhu8b32b192017-10-24 14:51:48 -0400280 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400281 if (!is.null(align)) {
Hao Zhue13a3e72018-04-23 23:52:13 -0400282 if (!is.null(table_info$column_width)) {
283 p_align <- switch(align,
284 "l" = "\\\\raggedright\\\\arraybackslash",
285 "c" = "\\\\centering\\\\arraybackslash",
286 "r" = "\\\\raggedleft\\\\arraybackslash")
287 align <- rep(align, table_info$ncol)
288 p_cols <- as.numeric(sub("column_", "", names(table_info$column_width)))
289 for (i in 1:length(p_cols)) {
290 align[p_cols[i]] <- paste0("\\>\\{", p_align, "\\}p\\{",
291 table_info$column_width[[i]], "\\}")
292 }
293 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400294 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400295 paste0("\\\\multicolumn\\{1\\}\\{", align, "\\}\\{", x, "\\}")
Hao Zhu8d347c42017-10-10 13:11:19 -0400296 })
297 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400298
299 if (!is.null(angle)) {
300 new_row <- lapply(new_row, function(x) {
Hao Zhuae80df42018-04-12 15:45:11 -0400301 paste0("\\\\rotatebox\\{", angle, "\\}\\{", x, "\\}")
Hao Zhubacd2f32017-10-11 14:06:36 -0400302 })
303 }
304
Hao Zhu35ecd272017-06-11 22:50:42 -0400305 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400306
Hao Zhuc79d8542020-08-18 03:19:21 -0400307 # if (!is.null(background)) {
308 # new_row <- paste0("\\\\rowcolor", latex_color(background), " ", new_row)
309 # }
Hao Zhua73601b2017-08-19 15:31:51 -0400310
Hao Zhuae80df42018-04-12 15:45:11 -0400311 if (!hline_after & is.null(extra_latex_after)) {
312 return(new_row)
313 } else {
314 latex_after <- "\\\\\\\\"
315 if (hline_after) {
316 latex_after <- paste0(latex_after, "\n\\\\hline")
317 }
318 if (!is.null(extra_latex_after)) {
319 latex_after <- paste0(latex_after, "\n",
320 regex_escape(extra_latex_after,
321 double_backslash = TRUE))
322 }
323 return(c(new_row, latex_after))
Hao Zhu53454f02018-01-14 16:29:10 -0500324 }
Hao Zhu73604282017-06-11 22:08:48 -0400325}
Hao Zhuc79d8542020-08-18 03:19:21 -0400326
327