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 | 3e53e60 | 2017-07-26 12:40:57 -0400 | [diff] [blame] | 13 | #' @param monospace A T/F value to control whether the text of the selected column |
| 14 | #' need to be monospaced (verbatim) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 15 | #' @param color A character string for row text color. For example, "red" or |
| 16 | #' "#BBBBBB". |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 17 | #' @param background A character string for row background color. Here please |
Hao Zhu | 53e240f | 2017-09-04 20:04:29 -0400 | [diff] [blame] | 18 | #' pay attention to the differences in color codes between HTML and LaTeX. |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 19 | #' @param align A character string for cell alignment. For HTML, possible values could |
| 20 | #' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit` |
| 21 | #' while for LaTeX, you can only choose from `l`, `c` & `r`. |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 22 | #' @param font_size A numeric input for font size. For HTML, you can also use |
| 23 | #' options including `xx-small`, `x-small`, `small`, `medium`, `large`, |
| 24 | #' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`. |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 25 | #' @param angle 0-360, degree that the text will rotate. |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 26 | #' |
| 27 | #' @examples x <- knitr::kable(head(mtcars), "html") |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 28 | #' row_spec(x, 1:2, bold = TRUE, italic = TRUE) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 29 | #' |
| 30 | #' @export |
| 31 | row_spec <- function(kable_input, row, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 32 | bold = FALSE, italic = FALSE, monospace = FALSE, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 33 | color = NULL, background = NULL, align = NULL, |
| 34 | font_size = NULL, angle = NULL) { |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 35 | if (!is.numeric(row)) { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 36 | stop("row must be numeric. ") |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 37 | } |
| 38 | kable_format <- attr(kable_input, "format") |
| 39 | if (!kable_format %in% c("html", "latex")) { |
| 40 | message("Currently generic markdown table using pandoc is not supported.") |
| 41 | return(kable_input) |
| 42 | } |
| 43 | if (kable_format == "html") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 44 | return(row_spec_html(kable_input, row, bold, italic, monospace, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 45 | color, background, align, font_size, angle)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 46 | } |
| 47 | if (kable_format == "latex") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 48 | return(row_spec_latex(kable_input, row, bold, italic, monospace, |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 49 | color, background, align, font_size, angle)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 53 | row_spec_html <- function(kable_input, row, bold, italic, monospace, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 54 | color, background, align, font_size, angle) { |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 55 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 56 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 57 | |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 58 | if (!is.null(align)) { |
| 59 | if (align %in% c("l", "c", "r")) { |
| 60 | align <- switch(align, r = "right", c = "center", l = "left") |
| 61 | } |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 64 | if (0 %in% row) { |
| 65 | kable_thead <- xml_tpart(kable_xml, "thead") |
| 66 | original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead))) |
| 67 | for (theader_i in 1:length(xml_children(original_header_row))) { |
| 68 | target_header_cell <- xml_child(original_header_row, theader_i) |
Hao Zhu | e7c8f70 | 2017-10-10 13:22:59 -0400 | [diff] [blame] | 69 | xml_cell_style(target_header_cell, bold, italic, monospace, color, background, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 70 | align, font_size, angle) |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 71 | } |
| 72 | row <- row[row != 0] |
| 73 | } |
| 74 | |
| 75 | if (length(row) != 0) { |
| 76 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 77 | |
| 78 | group_header_rows <- attr(kable_input, "group_header_rows") |
| 79 | if (!is.null(group_header_rows)) { |
| 80 | row <- positions_corrector(row, group_header_rows, |
| 81 | length(xml_children(kable_tbody))) |
| 82 | } |
| 83 | |
| 84 | for (j in row) { |
| 85 | target_row <- xml_child(kable_tbody, j) |
| 86 | for (i in 1:length(xml_children(target_row))) { |
| 87 | target_cell <- xml_child(target_row, i) |
Hao Zhu | e7c8f70 | 2017-10-10 13:22:59 -0400 | [diff] [blame] | 88 | xml_cell_style(target_cell, bold, italic, monospace, color, background, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 89 | align, font_size, angle) |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 90 | } |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 91 | } |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 92 | } |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 93 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 94 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 95 | attributes(out) <- kable_attrs |
| 96 | return(out) |
| 97 | } |
| 98 | |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 99 | xml_cell_style <- function(x, bold, italic, monospace, color, background, |
| 100 | align, font_size, angle) { |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 101 | if (bold) { |
| 102 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 103 | "font-weight: bold;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 104 | } |
| 105 | if (italic) { |
| 106 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 107 | "font-style: italic;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 108 | } |
| 109 | if (monospace) { |
| 110 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 111 | "font-family: monospace;") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 112 | } |
| 113 | if (!is.null(color)) { |
| 114 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 115 | "color: ", color, ";") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 116 | } |
| 117 | if (!is.null(background)) { |
| 118 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 119 | "background-color: ", |
| 120 | background, ";") |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 121 | } |
| 122 | if (!is.null(align)) { |
| 123 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 124 | "text-align: ", align, ";") |
| 125 | } |
Hao Zhu | e7c8f70 | 2017-10-10 13:22:59 -0400 | [diff] [blame] | 126 | if (!is.null(font_size)) { |
| 127 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 128 | "font-size: ", font_size, "px;") |
| 129 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 130 | if (!is.null(angle)) { |
| 131 | xml_attr(x, "style") <- paste0(xml_attr(x, "style"), |
| 132 | "-webkit-transform: rotate(", angle, |
| 133 | "deg); -moz-transform: rotate(", angle, |
| 134 | "deg); -ms-transform: rotate(", angle, |
| 135 | "deg); -o-transform: rotate(", angle, |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 136 | "deg); transform: rotate(", angle, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 137 | "deg);") |
| 138 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 139 | return(x) |
| 140 | } |
| 141 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 142 | row_spec_latex <- function(kable_input, row, bold, italic, monospace, |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 143 | color, background, align, font_size, angle) { |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 144 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 145 | out <- enc2utf8(as.character(kable_input)) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 146 | |
| 147 | if (table_info$duplicated_rows) { |
| 148 | dup_fx_out <- fix_duplicated_rows_latex(out, table_info) |
| 149 | out <- dup_fx_out[[1]] |
| 150 | table_info <- dup_fx_out[[2]] |
| 151 | } |
| 152 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 153 | row <- row + 1 |
| 154 | for (i in row) { |
| 155 | target_row <- table_info$contents[i] |
| 156 | new_row <- latex_new_row_builder(target_row, bold, italic, monospace, |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 157 | color, background, align, font_size, angle) |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 158 | out <- sub(target_row, new_row, out, perl = T) |
| 159 | } |
| 160 | |
| 161 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 162 | attr(out, "kable_meta") <- table_info |
| 163 | return(out) |
| 164 | } |
| 165 | |
| 166 | latex_new_row_builder <- function(target_row, bold, italic, monospace, |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 167 | color, background, align, font_size, angle) { |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 168 | new_row <- latex_row_cells(target_row) |
| 169 | if (bold) { |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 170 | new_row <- lapply(new_row, function(x) { |
| 171 | paste0("\\\\bfseries{", x, "}") |
| 172 | }) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 173 | } |
| 174 | if (italic) { |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 175 | new_row <- lapply(new_row, function(x) { |
| 176 | paste0("\\\\em{", x, "}") |
| 177 | }) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 178 | } |
Hao Zhu | 3e53e60 | 2017-07-26 12:40:57 -0400 | [diff] [blame] | 179 | if (monospace) { |
| 180 | new_row <- lapply(new_row, function(x) { |
| 181 | paste0("\\\\ttfamily{", x, "}") |
| 182 | }) |
| 183 | } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 184 | |
| 185 | if (!is.null(color)) { |
| 186 | new_row <- lapply(new_row, function(x) { |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 187 | paste0("\\\\textcolor", latex_color(color), "{", x, "}") |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 188 | }) |
| 189 | } |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 190 | if (!is.null(font_size)) { |
| 191 | new_row <- lapply(new_row, function(x) { |
| 192 | paste0("\\\\begingroup\\\\fontsize{", font_size, "}{", |
| 193 | as.numeric(font_size) + 2, |
| 194 | "}\\\\selectfont ", x, "\\\\endgroup")}) |
| 195 | } |
Hao Zhu | 8d347c4 | 2017-10-10 13:11:19 -0400 | [diff] [blame] | 196 | if (!is.null(align)) { |
| 197 | new_row <- lapply(new_row, function(x) { |
| 198 | paste0("\\\\multicolumn{1}{", align, "}{", x, "}") |
| 199 | }) |
| 200 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 201 | |
| 202 | if (!is.null(angle)) { |
| 203 | new_row <- lapply(new_row, function(x) { |
| 204 | paste0("\\\\rotatebox{", angle, "}{", x, "}") |
| 205 | }) |
| 206 | } |
| 207 | |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 208 | new_row <- paste(unlist(new_row), collapse = " & ") |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 209 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 210 | if (!is.null(background)) { |
Hao Zhu | 915b1b2 | 2017-11-09 14:01:44 -0500 | [diff] [blame] | 211 | new_row <- paste0("\\\\rowcolor", latex_color(background), " ", new_row) |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 214 | return(new_row) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 215 | } |