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 | #' |
| 37 | #' @examples x <- knitr::kable(head(mtcars), "html") |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 38 | #' row_spec(x, 1:2, bold = TRUE, italic = TRUE) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 39 | #' |
| 40 | #' @export |
| 41 | row_spec <- function(kable_input, row, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 42 | bold = FALSE, italic = FALSE, monospace = FALSE, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 43 | underline = FALSE, strikeout = FALSE, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 44 | color = NULL, background = NULL, align = NULL, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 45 | font_size = NULL, angle = NULL, extra_css = NULL, |
| 46 | hline_after = FALSE, extra_latex_after = NULL) { |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 47 | if (!is.numeric(row)) { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 48 | stop("row must be numeric. ") |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 49 | } |
| 50 | kable_format <- attr(kable_input, "format") |
| 51 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 52 | 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 Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 55 | return(kable_input) |
| 56 | } |
| 57 | if (kable_format == "html") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 58 | return(row_spec_html(kable_input, row, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 59 | underline, strikeout, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 60 | color, background, align, font_size, angle, |
| 61 | extra_css)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 62 | } |
| 63 | if (kable_format == "latex") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 64 | return(row_spec_latex(kable_input, row, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 65 | underline, strikeout, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 66 | color, background, align, font_size, angle, |
| 67 | hline_after, extra_latex_after)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 71 | row_spec_html <- function(kable_input, row, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 72 | underline, strikeout, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 73 | color, background, align, font_size, angle, |
| 74 | extra_css) { |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 75 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 76 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 77 | |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 78 | if (!is.null(align)) { |
| 79 | if (align %in% c("l", "c", "r")) { |
| 80 | align <- switch(align, r = "right", c = "center", l = "left") |
| 81 | } |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 84 | 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 Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 89 | xml_cell_style(target_header_cell, bold, italic, monospace, |
| 90 | underline, strikeout, color, background, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 91 | align, font_size, angle, extra_css) |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 92 | } |
| 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 Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 109 | xml_cell_style(target_cell, bold, italic, monospace, |
| 110 | underline, strikeout, color, background, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 111 | align, font_size, angle, extra_css) |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 112 | } |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 113 | } |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 114 | } |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 115 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 116 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 117 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 118 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 119 | return(out) |
| 120 | } |
| 121 | |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 122 | xml_cell_style <- function(x, bold, italic, monospace, |
| 123 | underline, strikeout, color, background, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 124 | align, font_size, angle, extra_css) { |
Hao Zhu | 517453c | 2018-05-13 13:07:18 -0400 | [diff] [blame] | 125 | if (is.na(xml_attr(x, "style"))) { |
| 126 | xml_attr(x, "style") <- "" |
| 127 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 128 | if (bold) { |
| 129 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 130 | "font-weight: bold;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 131 | } |
| 132 | if (italic) { |
| 133 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 134 | "font-style: italic;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 135 | } |
| 136 | if (monospace) { |
| 137 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 138 | "font-family: monospace;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 139 | } |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 140 | if (underline) { |
| 141 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 142 | "text-decoration: underline;") |
| 143 | } |
| 144 | if (strikeout) { |
| 145 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 146 | "text-decoration: line-through;") |
| 147 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 148 | if (!is.null(color)) { |
| 149 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 150 | "color: ", color, ";") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 151 | } |
| 152 | if (!is.null(background)) { |
| 153 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 154 | "background-color: ", |
| 155 | background, ";") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 156 | } |
| 157 | if (!is.null(align)) { |
| 158 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 159 | "text-align: ", align, ";") |
| 160 | } |
Hao Zhu | e7c8f70 | 2017-10-10 13:22:59 -0400 | [diff] [blame] | 161 | if (!is.null(font_size)) { |
| 162 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 163 | "font-size: ", font_size, "px;") |
| 164 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 165 | if (!is.null(angle)) { |
| 166 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 167 | "-webkit-transform: rotate(", angle, |
| 168 | "deg); -moz-transform: rotate(", angle, |
| 169 | "deg); -ms-transform: rotate(", angle, |
| 170 | "deg); -o-transform: rotate(", angle, |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 171 | "deg); transform: rotate(", angle, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 172 | "deg);") |
| 173 | } |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 174 | if (!is.null(extra_css)) { |
| 175 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), extra_css) |
| 176 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 177 | return(x) |
| 178 | } |
| 179 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 180 | row_spec_latex <- function(kable_input, row, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 181 | underline, strikeout, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 182 | color, background, align, font_size, angle, |
| 183 | hline_after, extra_latex_after) { |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 184 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 185 | out <- solve_enc(kable_input) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 186 | |
| 187 | if (table_info$duplicated_rows) { |
| 188 | dup_fx_out <- fix_duplicated_rows_latex(out, table_info) |
| 189 | out <- dup_fx_out[[1]] |
| 190 | table_info <- dup_fx_out[[2]] |
| 191 | } |
| 192 | |
Hao Zhu | 37dbe3f | 2018-05-14 11:16:06 -0400 | [diff] [blame] | 193 | row <- row + table_info$position_offset |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 194 | for (i in row) { |
| 195 | target_row <- table_info$contents[i] |
Hao Zhu | e13a3e7 | 2018-04-23 23:52:13 -0400 | [diff] [blame] | 196 | new_row <- latex_new_row_builder(target_row, table_info, |
| 197 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 198 | underline, strikeout, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 199 | color, background, align, font_size, angle, |
| 200 | hline_after, extra_latex_after) |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 201 | if (length(new_row) == 1) { |
| 202 | out <- sub(target_row, new_row, out, perl = T) |
| 203 | } else { |
| 204 | out <- sub(paste0(target_row, "\\\\\\\\"), |
| 205 | paste(new_row, collapse = ""), out, perl = T) |
| 206 | } |
| 207 | table_info$contents[i] <- new_row |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 211 | attr(out, "kable_meta") <- table_info |
| 212 | return(out) |
| 213 | } |
| 214 | |
Hao Zhu | e13a3e7 | 2018-04-23 23:52:13 -0400 | [diff] [blame] | 215 | latex_new_row_builder <- function(target_row, table_info, |
| 216 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 217 | underline, strikeout, |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 218 | color, background, align, font_size, angle, |
| 219 | hline_after, extra_latex_after) { |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 220 | new_row <- latex_row_cells(target_row) |
| 221 | if (bold) { |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 222 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 223 | paste0("\\\\textbf\\{", x, "\\}") |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 224 | }) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 225 | } |
| 226 | if (italic) { |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 227 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 228 | paste0("\\\\em\\{", x, "\\}") |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 229 | }) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 230 | } |
Hao Zhu | 3e53e60 | 2017-07-26 12:40:57 -0400 | [diff] [blame] | 231 | if (monospace) { |
| 232 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 233 | paste0("\\\\ttfamily\\{", x, "\\}") |
Hao Zhu | 3e53e60 | 2017-07-26 12:40:57 -0400 | [diff] [blame] | 234 | }) |
| 235 | } |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 236 | if (underline) { |
| 237 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 238 | paste0("\\\\underline\\{", x, "\\}") |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 239 | }) |
| 240 | } |
| 241 | if (strikeout) { |
| 242 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 243 | paste0("\\\\sout\\{", x, "\\}") |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 244 | }) |
| 245 | } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 246 | if (!is.null(color)) { |
| 247 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 248 | paste0("\\\\textcolor", latex_color(color), "\\{", x, "\\}") |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 249 | }) |
| 250 | } |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 251 | if (!is.null(font_size)) { |
| 252 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 253 | paste0("\\\\begingroup\\\\fontsize\\{", font_size, "\\}\\{", |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 254 | as.numeric(font_size) + 2, |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 255 | "\\}\\\\selectfont ", x, "\\\\endgroup")}) |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 256 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 257 | if (!is.null(align)) { |
Hao Zhu | e13a3e7 | 2018-04-23 23:52:13 -0400 | [diff] [blame] | 258 | if (!is.null(table_info$column_width)) { |
| 259 | p_align <- switch(align, |
| 260 | "l" = "\\\\raggedright\\\\arraybackslash", |
| 261 | "c" = "\\\\centering\\\\arraybackslash", |
| 262 | "r" = "\\\\raggedleft\\\\arraybackslash") |
| 263 | align <- rep(align, table_info$ncol) |
| 264 | p_cols <- as.numeric(sub("column_", "", names(table_info$column_width))) |
| 265 | for (i in 1:length(p_cols)) { |
| 266 | align[p_cols[i]] <- paste0("\\>\\{", p_align, "\\}p\\{", |
| 267 | table_info$column_width[[i]], "\\}") |
| 268 | } |
| 269 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 270 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 271 | paste0("\\\\multicolumn\\{1\\}\\{", align, "\\}\\{", x, "\\}") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 272 | }) |
| 273 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 274 | |
| 275 | if (!is.null(angle)) { |
| 276 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 277 | paste0("\\\\rotatebox\\{", angle, "\\}\\{", x, "\\}") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 278 | }) |
| 279 | } |
| 280 | |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 281 | new_row <- paste(unlist(new_row), collapse = " & ") |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 282 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 283 | if (!is.null(background)) { |
Hao Zhu | 915b1b2 | 2017-11-09 14:01:44 -0500 | [diff] [blame] | 284 | new_row <- paste0("\\\\rowcolor", latex_color(background), " ", new_row) |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 287 | if (!hline_after & is.null(extra_latex_after)) { |
| 288 | return(new_row) |
| 289 | } else { |
| 290 | latex_after <- "\\\\\\\\" |
| 291 | if (hline_after) { |
| 292 | latex_after <- paste0(latex_after, "\n\\\\hline") |
| 293 | } |
| 294 | if (!is.null(extra_latex_after)) { |
| 295 | latex_after <- paste0(latex_after, "\n", |
| 296 | regex_escape(extra_latex_after, |
| 297 | double_backslash = TRUE)) |
| 298 | } |
| 299 | return(c(new_row, latex_after)) |
Hao Zhu | 53454f0 | 2018-01-14 16:29:10 -0500 | [diff] [blame] | 300 | } |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 301 | } |