Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 1 | #' Specify the look of the selected row |
| 2 | #' |
| 3 | #' @description This function allows users to select a row and then specify |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 4 | #' its look. It can also specify the format of the header row when `row` = 0. |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 5 | #' |
| 6 | #' @param kable_input Output of `knitr::kable()` with `format` specified |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 7 | #' @param row A numeric value or vector indicating which row(s) to be selected. You don't |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 8 | #' need to count in header rows or group labeling rows. |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 9 | #' @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 Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 13 | #' @param monospace A T/F value to control whether the text of the selected row |
Hao Zhu | 3e53e60 | 2017-07-26 12:40:57 -0400 | [diff] [blame] | 14 | #' need to be monospaced (verbatim) |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 15 | #' @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 Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 19 | #' @param color A character string for row text color. For example, "red" or |
| 20 | #' "#BBBBBB". |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 21 | #' @param background A character string for row background color. Here please |
Hao Zhu | 53e240f | 2017-09-04 20:04:29 -0400 | [diff] [blame] | 22 | #' pay attention to the differences in color codes between HTML and LaTeX. |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 23 | #' @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 Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 26 | #' @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 Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 29 | #' @param angle 0-360, degree that the text will rotate. |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 30 | #' @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 Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 32 | #' @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 Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 36 | #' |
Hao Zhu | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame^] | 37 | #' @examples |
| 38 | #' \dontrun{ |
| 39 | #' x <- knitr::kable(head(mtcars), "html") |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 40 | #' row_spec(x, 1:2, bold = TRUE, italic = TRUE) |
Hao Zhu | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame^] | 41 | #' } |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 42 | #' |
| 43 | #' @export |
| 44 | row_spec <- function(kable_input, row, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 45 | bold = FALSE, italic = FALSE, monospace = FALSE, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 46 | underline = FALSE, strikeout = FALSE, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 47 | color = NULL, background = NULL, align = NULL, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 48 | font_size = NULL, angle = NULL, extra_css = NULL, |
| 49 | hline_after = FALSE, extra_latex_after = NULL) { |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 50 | if (!is.numeric(row)) { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 51 | stop("row must be numeric. ") |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 52 | } |
| 53 | kable_format <- attr(kable_input, "format") |
| 54 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 55 | 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 Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 58 | return(kable_input) |
| 59 | } |
| 60 | if (kable_format == "html") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 61 | return(row_spec_html(kable_input, row, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 62 | underline, strikeout, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 63 | color, background, align, font_size, angle, |
| 64 | extra_css)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 65 | } |
| 66 | if (kable_format == "latex") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 67 | return(row_spec_latex(kable_input, row, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 68 | underline, strikeout, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 69 | color, background, align, font_size, angle, |
| 70 | hline_after, extra_latex_after)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 74 | row_spec_html <- function(kable_input, row, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 75 | underline, strikeout, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 76 | color, background, align, font_size, angle, |
| 77 | extra_css) { |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 78 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 79 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 80 | |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 81 | if (!is.null(align)) { |
| 82 | if (align %in% c("l", "c", "r")) { |
| 83 | align <- switch(align, r = "right", c = "center", l = "left") |
| 84 | } |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 87 | 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 Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 92 | xml_cell_style(target_header_cell, bold, italic, monospace, |
| 93 | underline, strikeout, color, background, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 94 | align, font_size, angle, extra_css) |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 95 | } |
| 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 Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 112 | xml_cell_style(target_cell, bold, italic, monospace, |
| 113 | underline, strikeout, color, background, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 114 | align, font_size, angle, extra_css) |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 115 | } |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 116 | } |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 117 | } |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 118 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 119 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 120 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 121 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 122 | return(out) |
| 123 | } |
| 124 | |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 125 | xml_cell_style <- function(x, bold, italic, monospace, |
| 126 | underline, strikeout, color, background, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 127 | align, font_size, angle, extra_css) { |
Hao Zhu | 517453c | 2018-05-13 13:07:18 -0400 | [diff] [blame] | 128 | if (is.na(xml_attr(x, "style"))) { |
| 129 | xml_attr(x, "style") <- "" |
| 130 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 131 | if (bold) { |
| 132 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 133 | "font-weight: bold;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 134 | } |
| 135 | if (italic) { |
| 136 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 137 | "font-style: italic;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 138 | } |
| 139 | if (monospace) { |
| 140 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 141 | "font-family: monospace;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 142 | } |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 143 | 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 Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 151 | if (!is.null(color)) { |
| 152 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 153 | "color: ", html_color(color), " !important;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 154 | } |
| 155 | if (!is.null(background)) { |
| 156 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 157 | "background-color: ", |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 158 | html_color(background), " !important;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 159 | } |
| 160 | if (!is.null(align)) { |
| 161 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 162 | "text-align: ", align, ";") |
| 163 | } |
Hao Zhu | e7c8f70 | 2017-10-10 13:22:59 -0400 | [diff] [blame] | 164 | if (!is.null(font_size)) { |
| 165 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 166 | "font-size: ", font_size, "px;") |
| 167 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 168 | 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 Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 174 | "deg); transform: rotate(", angle, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 175 | "deg);") |
| 176 | } |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 177 | if (!is.null(extra_css)) { |
| 178 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), extra_css) |
| 179 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 180 | return(x) |
| 181 | } |
| 182 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 183 | row_spec_latex <- function(kable_input, row, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 184 | underline, strikeout, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 185 | color, background, align, font_size, angle, |
| 186 | hline_after, extra_latex_after) { |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 187 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 188 | out <- solve_enc(kable_input) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 189 | |
| 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 Zhu | 37dbe3f | 2018-05-14 11:16:06 -0400 | [diff] [blame] | 196 | row <- row + table_info$position_offset |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 197 | for (i in row) { |
| 198 | target_row <- table_info$contents[i] |
Hao Zhu | e13a3e7 | 2018-04-23 23:52:13 -0400 | [diff] [blame] | 199 | new_row <- latex_new_row_builder(target_row, table_info, |
| 200 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 201 | underline, strikeout, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 202 | color, background, align, font_size, angle, |
| 203 | hline_after, extra_latex_after) |
Hao Zhu | 069fcef | 2020-08-04 00:12:29 -0400 | [diff] [blame] | 204 | temp_sub <- ifelse(i == 1 & (table_info$tabular == "longtable" | |
| 205 | !is.null(table_info$repeat_header_latex)), |
| 206 | gsub, sub) |
Hao Zhu | 17dbad1 | 2018-05-20 18:02:42 -0400 | [diff] [blame] | 207 | if (length(new_row) == 1) { |
| 208 | out <- temp_sub(target_row, new_row, out, perl = T) |
| 209 | table_info$contents[i] <- new_row |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 210 | } else { |
Hao Zhu | 17dbad1 | 2018-05-20 18:02:42 -0400 | [diff] [blame] | 211 | out <- temp_sub(paste0(target_row, "\\\\\\\\"), |
| 212 | paste(new_row, collapse = ""), out, perl = T) |
| 213 | table_info$contents[i] <- new_row[1] |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 214 | } |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 218 | attr(out, "kable_meta") <- table_info |
| 219 | return(out) |
| 220 | } |
| 221 | |
Hao Zhu | e13a3e7 | 2018-04-23 23:52:13 -0400 | [diff] [blame] | 222 | latex_new_row_builder <- function(target_row, table_info, |
| 223 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 224 | underline, strikeout, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 225 | color, background, align, font_size, angle, |
| 226 | hline_after, extra_latex_after) { |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 227 | new_row <- latex_row_cells(target_row) |
| 228 | if (bold) { |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 229 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 230 | paste0("\\\\textbf\\{", x, "\\}") |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 231 | }) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 232 | } |
| 233 | if (italic) { |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 234 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 235 | paste0("\\\\em\\{", x, "\\}") |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 236 | }) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 237 | } |
Hao Zhu | 3e53e60 | 2017-07-26 12:40:57 -0400 | [diff] [blame] | 238 | if (monospace) { |
| 239 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 240 | paste0("\\\\ttfamily\\{", x, "\\}") |
Hao Zhu | 3e53e60 | 2017-07-26 12:40:57 -0400 | [diff] [blame] | 241 | }) |
| 242 | } |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 243 | if (underline) { |
| 244 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 245 | paste0("\\\\underline\\{", x, "\\}") |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 246 | }) |
| 247 | } |
| 248 | if (strikeout) { |
| 249 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 250 | paste0("\\\\sout\\{", x, "\\}") |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 251 | }) |
| 252 | } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 253 | if (!is.null(color)) { |
Hao Zhu | ebeec79 | 2020-08-19 09:45:16 -0400 | [diff] [blame] | 254 | 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 Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 259 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | c79d854 | 2020-08-18 03:19:21 -0400 | [diff] [blame] | 260 | x <- clear_color_latex(x) |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 261 | paste0("\\\\textcolor", latex_color(color), "\\{", x, "\\}") |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 262 | }) |
| 263 | } |
Hao Zhu | c79d854 | 2020-08-18 03:19:21 -0400 | [diff] [blame] | 264 | if (!is.null(background)) { |
Hao Zhu | ebeec79 | 2020-08-19 09:45:16 -0400 | [diff] [blame] | 265 | 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 Zhu | c79d854 | 2020-08-18 03:19:21 -0400 | [diff] [blame] | 270 | 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 Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 275 | if (!is.null(font_size)) { |
| 276 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 277 | paste0("\\\\begingroup\\\\fontsize\\{", font_size, "\\}\\{", |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 278 | as.numeric(font_size) + 2, |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 279 | "\\}\\\\selectfont ", x, "\\\\endgroup")}) |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 280 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 281 | if (!is.null(align)) { |
Hao Zhu | e13a3e7 | 2018-04-23 23:52:13 -0400 | [diff] [blame] | 282 | 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 Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 294 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 295 | paste0("\\\\multicolumn\\{1\\}\\{", align, "\\}\\{", x, "\\}") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 296 | }) |
| 297 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 298 | |
| 299 | if (!is.null(angle)) { |
| 300 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 301 | paste0("\\\\rotatebox\\{", angle, "\\}\\{", x, "\\}") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 302 | }) |
| 303 | } |
| 304 | |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 305 | new_row <- paste(unlist(new_row), collapse = " & ") |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 306 | |
Hao Zhu | c79d854 | 2020-08-18 03:19:21 -0400 | [diff] [blame] | 307 | # if (!is.null(background)) { |
| 308 | # new_row <- paste0("\\\\rowcolor", latex_color(background), " ", new_row) |
| 309 | # } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 310 | |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 311 | 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 Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 324 | } |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 325 | } |
Hao Zhu | c79d854 | 2020-08-18 03:19:21 -0400 | [diff] [blame] | 326 | |
| 327 | |