| 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 | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 8 | #' @param row A numeric value indicating which row to be selected. You don't | 
|  | 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. | 
|  | 14 | #' | 
|  | 15 | #' @examples x <- knitr::kable(head(mtcars), "html") | 
|  | 16 | #' row_spec(x, 1, bold = TRUE, italic = TRUE) | 
|  | 17 | #' | 
|  | 18 | #' @export | 
|  | 19 | row_spec <- function(kable_input, row, | 
|  | 20 | bold = FALSE, italic = FALSE) { | 
|  | 21 | if (!is.numeric(row)) { | 
|  | 22 | stop("row must be a numeric value") | 
|  | 23 | } | 
|  | 24 | kable_format <- attr(kable_input, "format") | 
|  | 25 | if (!kable_format %in% c("html", "latex")) { | 
|  | 26 | message("Currently generic markdown table using pandoc is not supported.") | 
|  | 27 | return(kable_input) | 
|  | 28 | } | 
|  | 29 | if (kable_format == "html") { | 
|  | 30 | return(row_spec_html(kable_input, row, bold, italic)) | 
|  | 31 | } | 
|  | 32 | if (kable_format == "latex") { | 
| Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 33 | return(row_spec_latex(kable_input, row, bold, italic)) | 
| Hao Zhu | 79f1e2a | 2017-06-11 20:55:30 -0400 | [diff] [blame] | 34 | } | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | row_spec_html <- function(kable_input, row, bold, italic) { | 
|  | 38 | kable_attrs <- attributes(kable_input) | 
|  | 39 | kable_xml <- read_xml(as.character(kable_input), options = "COMPACT") | 
|  | 40 | kable_tbody <- xml_tpart(kable_xml, "tbody") | 
|  | 41 |  | 
|  | 42 | group_header_rows <- attr(kable_input, "group_header_rows") | 
|  | 43 | if (!is.null(group_header_rows)) { | 
|  | 44 | row <- positions_corrector(row, group_header_rows, | 
|  | 45 | length(xml_children(kable_tbody))) | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | target_row <- xml_child(kable_tbody, row) | 
|  | 49 |  | 
|  | 50 | for (i in 1:length(xml_children(target_row))) { | 
|  | 51 | target_cell <- xml_child(target_row, i) | 
|  | 52 | if (bold) { | 
|  | 53 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), | 
|  | 54 | "font-weight: bold;") | 
|  | 55 | } | 
|  | 56 | if (italic) { | 
|  | 57 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), | 
|  | 58 | "font-style: italic;") | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 | out <- structure(as.character(kable_xml), format = "html", | 
|  | 62 | class = "knitr_kable") | 
|  | 63 | attributes(out) <- kable_attrs | 
|  | 64 | return(out) | 
|  | 65 | } | 
|  | 66 |  | 
| Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 67 | row_spec_latex <- function(kable_input, row, bold, italic) { | 
|  | 68 | table_info <- magic_mirror(kable_input) | 
|  | 69 | target_row <- table_info$contents[row + 1] | 
|  | 70 | new_row <- latex_row_cells(target_row) | 
|  | 71 | if (bold) { | 
| Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 72 | new_row <- lapply(new_row, function(x) { | 
|  | 73 | paste0("\\\\bfseries{", x, "}") | 
|  | 74 | }) | 
| Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 75 | } | 
|  | 76 | if (italic) { | 
| Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 77 | new_row <- lapply(new_row, function(x) { | 
|  | 78 | paste0("\\\\em{", x, "}") | 
|  | 79 | }) | 
| Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 80 | } | 
| Hao Zhu | 35ecd27 | 2017-06-11 22:50:42 -0400 | [diff] [blame] | 81 | new_row <- paste(unlist(new_row), collapse = " & ") | 
| Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 82 |  | 
|  | 83 | out <- sub(target_row, new_row, as.character(kable_input), perl = T) | 
|  | 84 | out <- structure(out, format = "latex", class = "knitr_kable") | 
| Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 85 | attr(out, "kable_meta") <- table_info | 
| Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 86 | return(out) | 
|  | 87 | } |