blob: 7a17e8c1df0b03852e0298ec65538c35249f10d3 [file] [log] [blame]
Hao Zhu79f1e2a2017-06-11 20:55:30 -04001#' 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 Zhu909ea382017-06-12 15:43:47 -04008#' @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 Zhu79f1e2a2017-06-11 20:55:30 -040010#' @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 Zhu3e53e602017-07-26 12:40:57 -040014#' @param monospace A T/F value to control whether the text of the selected column
15#' need to be monospaced (verbatim)
Hao Zhu53e240f2017-09-04 20:04:29 -040016#' @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 Zhu79f1e2a2017-06-11 20:55:30 -040020#'
21#' @examples x <- knitr::kable(head(mtcars), "html")
22#' row_spec(x, 1, bold = TRUE, italic = TRUE)
23#'
24#' @export
25row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040026 bold = FALSE, italic = FALSE, monospace = FALSE,
27 color = NULL, background = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040028 if (!is.numeric(row)) {
29 stop("row must be a numeric value")
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 Zhu669bcd22017-08-19 14:53:40 -040037 return(row_spec_html(kable_input, row, bold, italic, monospace,
38 color, background))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040039 }
40 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040041 return(row_spec_latex(kable_input, row, bold, italic, monospace,
42 color, background))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040043 }
44}
45
Hao Zhu669bcd22017-08-19 14:53:40 -040046row_spec_html <- function(kable_input, row, bold, italic, monospace,
47 color, background) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040048 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040049 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040050 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)) {
54 row <- positions_corrector(row, group_header_rows,
55 length(xml_children(kable_tbody)))
56 }
57
58 target_row <- xml_child(kable_tbody, row)
59
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 }
Hao Zhu3e53e602017-07-26 12:40:57 -040070 if (monospace) {
71 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
72 "font-family: monospace;")
73 }
Hao Zhu669bcd22017-08-19 14:53:40 -040074 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 Zhu79f1e2a2017-06-11 20:55:30 -040083 }
Hao Zhuf2dfd142017-07-24 14:43:28 -040084 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040085 attributes(out) <- kable_attrs
86 return(out)
87}
88
Hao Zhua73601b2017-08-19 15:31:51 -040089row_spec_latex <- function(kable_input, row, bold, italic, monospace,
90 color, background) {
Hao Zhu73604282017-06-11 22:08:48 -040091 table_info <- magic_mirror(kable_input)
92 target_row <- table_info$contents[row + 1]
93 new_row <- latex_row_cells(target_row)
94 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -040095 new_row <- lapply(new_row, function(x) {
96 paste0("\\\\bfseries{", x, "}")
97 })
Hao Zhu73604282017-06-11 22:08:48 -040098 }
99 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400100 new_row <- lapply(new_row, function(x) {
101 paste0("\\\\em{", x, "}")
102 })
Hao Zhu73604282017-06-11 22:08:48 -0400103 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400104 if (monospace) {
105 new_row <- lapply(new_row, function(x) {
106 paste0("\\\\ttfamily{", x, "}")
107 })
108 }
Hao Zhua73601b2017-08-19 15:31:51 -0400109
110 if (!is.null(color)) {
111 new_row <- lapply(new_row, function(x) {
112 paste0("\\\\textcolor{", color, "}{", x, "}")
113 })
114 }
Hao Zhu35ecd272017-06-11 22:50:42 -0400115 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400116
Hao Zhua73601b2017-08-19 15:31:51 -0400117 if (!is.null(background)) {
118 new_row <- paste0("\\\\rowcolor{", background, "} ", new_row)
119 }
120
Hao Zhud2c0f732017-08-26 10:40:14 -0400121 out <- sub(target_row, new_row, enc2utf8(as.character(kable_input)),
122 perl = T)
Hao Zhu73604282017-06-11 22:08:48 -0400123 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400124 attr(out, "kable_meta") <- table_info
Hao Zhu73604282017-06-11 22:08:48 -0400125 return(out)
126}