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 |
| 4 | #' its look. Right now it supports the following two properties: bold text and |
| 5 | #' italic text. |
| 6 | #' |
| 7 | #' @param kable_input Output of `knitr::kable()` with `format` specified |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 8 | #' @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] | 9 | #' need to count in header rows or group labeling rows. |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 10 | #' @param bold A T/F value to control whether the text of the selected row |
| 11 | #' need to be bolded. |
| 12 | #' @param italic A T/F value to control whether the text of the selected row |
| 13 | #' need to be emphasized. |
Hao Zhu | 3e53e60 | 2017-07-26 12:40:57 -0400 | [diff] [blame] | 14 | #' @param monospace A T/F value to control whether the text of the selected column |
| 15 | #' need to be monospaced (verbatim) |
Hao Zhu | 53e240f | 2017-09-04 20:04:29 -0400 | [diff] [blame] | 16 | #' @param color A character string for column text color. Here please pay |
| 17 | #' attention to the differences in color codes between HTML and LaTeX. |
| 18 | #' @param background A character string for column background color. Here please |
| 19 | #' pay attention to the differences in color codes between HTML and LaTeX. |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 20 | #' |
| 21 | #' @examples x <- knitr::kable(head(mtcars), "html") |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 22 | #' row_spec(x, 1:2, bold = TRUE, italic = TRUE) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 23 | #' |
| 24 | #' @export |
| 25 | row_spec <- function(kable_input, row, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 26 | bold = FALSE, italic = FALSE, monospace = FALSE, |
| 27 | color = NULL, background = NULL) { |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 28 | if (!is.numeric(row)) { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 29 | stop("row must be numeric. ") |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 30 | } |
| 31 | kable_format <- attr(kable_input, "format") |
| 32 | if (!kable_format %in% c("html", "latex")) { |
| 33 | message("Currently generic markdown table using pandoc is not supported.") |
| 34 | return(kable_input) |
| 35 | } |
| 36 | if (kable_format == "html") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 37 | return(row_spec_html(kable_input, row, bold, italic, monospace, |
| 38 | color, background)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 39 | } |
| 40 | if (kable_format == "latex") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 41 | return(row_spec_latex(kable_input, row, bold, italic, monospace, |
| 42 | color, background)) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 46 | row_spec_html <- function(kable_input, row, bold, italic, monospace, |
| 47 | color, background) { |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 48 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 49 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 50 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 51 | |
| 52 | group_header_rows <- attr(kable_input, "group_header_rows") |
| 53 | if (!is.null(group_header_rows)) { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 54 | row <- positions_corrector(row, group_header_rows, |
| 55 | length(xml_children(kable_tbody))) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 58 | for (j in row) { |
| 59 | target_row <- xml_child(kable_tbody, j) |
| 60 | for (i in 1:length(xml_children(target_row))) { |
| 61 | target_cell <- xml_child(target_row, i) |
| 62 | if (bold) { |
| 63 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 64 | "font-weight: bold;") |
| 65 | } |
| 66 | if (italic) { |
| 67 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 68 | "font-style: italic;") |
| 69 | } |
| 70 | if (monospace) { |
| 71 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 72 | "font-family: monospace;") |
| 73 | } |
| 74 | if (!is.null(color)) { |
| 75 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 76 | "color: ", color, ";") |
| 77 | } |
| 78 | if (!is.null(background)) { |
| 79 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 80 | "background-color: ", |
| 81 | background, ";") |
| 82 | } |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 83 | } |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 84 | } |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 85 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 86 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 87 | attributes(out) <- kable_attrs |
| 88 | return(out) |
| 89 | } |
| 90 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 91 | row_spec_latex <- function(kable_input, row, bold, italic, monospace, |
| 92 | color, background) { |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 93 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 94 | out <- enc2utf8(as.character(kable_input)) |
| 95 | row <- row + 1 |
| 96 | for (i in row) { |
| 97 | target_row <- table_info$contents[i] |
| 98 | new_row <- latex_new_row_builder(target_row, bold, italic, monospace, |
| 99 | color, background) |
| 100 | out <- sub(target_row, new_row, out, perl = T) |
| 101 | } |
| 102 | |
| 103 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 104 | attr(out, "kable_meta") <- table_info |
| 105 | return(out) |
| 106 | } |
| 107 | |
| 108 | latex_new_row_builder <- function(target_row, bold, italic, monospace, |
| 109 | color, background) { |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 110 | new_row <- latex_row_cells(target_row) |
| 111 | if (bold) { |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 112 | new_row <- lapply(new_row, function(x) { |
| 113 | paste0("\\\\bfseries{", x, "}") |
| 114 | }) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 115 | } |
| 116 | if (italic) { |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 117 | new_row <- lapply(new_row, function(x) { |
| 118 | paste0("\\\\em{", x, "}") |
| 119 | }) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 120 | } |
Hao Zhu | 3e53e60 | 2017-07-26 12:40:57 -0400 | [diff] [blame] | 121 | if (monospace) { |
| 122 | new_row <- lapply(new_row, function(x) { |
| 123 | paste0("\\\\ttfamily{", x, "}") |
| 124 | }) |
| 125 | } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 126 | |
| 127 | if (!is.null(color)) { |
| 128 | new_row <- lapply(new_row, function(x) { |
| 129 | paste0("\\\\textcolor{", color, "}{", x, "}") |
| 130 | }) |
| 131 | } |
Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 132 | new_row <- paste(unlist(new_row), collapse = " & ") |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 133 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 134 | if (!is.null(background)) { |
| 135 | new_row <- paste0("\\\\rowcolor{", background, "} ", new_row) |
| 136 | } |
| 137 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 138 | return(new_row) |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 139 | } |