blob: 1d5d500f01c9bc5efea47d03d784bc0305f355e7 [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 Zhu79f1e2a2017-06-11 20:55:30 -040016#'
17#' @examples x <- knitr::kable(head(mtcars), "html")
18#' row_spec(x, 1, bold = TRUE, italic = TRUE)
19#'
20#' @export
21row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040022 bold = FALSE, italic = FALSE, monospace = FALSE,
23 color = NULL, background = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040024 if (!is.numeric(row)) {
25 stop("row must be a numeric value")
26 }
27 kable_format <- attr(kable_input, "format")
28 if (!kable_format %in% c("html", "latex")) {
29 message("Currently generic markdown table using pandoc is not supported.")
30 return(kable_input)
31 }
32 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040033 return(row_spec_html(kable_input, row, bold, italic, monospace,
34 color, background))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040035 }
36 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040037 return(row_spec_latex(kable_input, row, bold, italic, monospace,
38 color, background))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040039 }
40}
41
Hao Zhu669bcd22017-08-19 14:53:40 -040042row_spec_html <- function(kable_input, row, bold, italic, monospace,
43 color, background) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040044 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040045 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040046 kable_tbody <- xml_tpart(kable_xml, "tbody")
47
48 group_header_rows <- attr(kable_input, "group_header_rows")
49 if (!is.null(group_header_rows)) {
50 row <- positions_corrector(row, group_header_rows,
51 length(xml_children(kable_tbody)))
52 }
53
54 target_row <- xml_child(kable_tbody, row)
55
56 for (i in 1:length(xml_children(target_row))) {
57 target_cell <- xml_child(target_row, i)
58 if (bold) {
59 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
60 "font-weight: bold;")
61 }
62 if (italic) {
63 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
64 "font-style: italic;")
65 }
Hao Zhu3e53e602017-07-26 12:40:57 -040066 if (monospace) {
67 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
68 "font-family: monospace;")
69 }
Hao Zhu669bcd22017-08-19 14:53:40 -040070 if (!is.null(color)) {
71 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
72 "color: ", color, ";")
73 }
74 if (!is.null(background)) {
75 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
76 "background-color: ",
77 background, ";")
78 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040079 }
Hao Zhuf2dfd142017-07-24 14:43:28 -040080 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040081 attributes(out) <- kable_attrs
82 return(out)
83}
84
Hao Zhua73601b2017-08-19 15:31:51 -040085row_spec_latex <- function(kable_input, row, bold, italic, monospace,
86 color, background) {
Hao Zhu73604282017-06-11 22:08:48 -040087 table_info <- magic_mirror(kable_input)
88 target_row <- table_info$contents[row + 1]
89 new_row <- latex_row_cells(target_row)
90 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -040091 new_row <- lapply(new_row, function(x) {
92 paste0("\\\\bfseries{", x, "}")
93 })
Hao Zhu73604282017-06-11 22:08:48 -040094 }
95 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -040096 new_row <- lapply(new_row, function(x) {
97 paste0("\\\\em{", x, "}")
98 })
Hao Zhu73604282017-06-11 22:08:48 -040099 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400100 if (monospace) {
101 new_row <- lapply(new_row, function(x) {
102 paste0("\\\\ttfamily{", x, "}")
103 })
104 }
Hao Zhua73601b2017-08-19 15:31:51 -0400105
106 if (!is.null(color)) {
107 new_row <- lapply(new_row, function(x) {
108 paste0("\\\\textcolor{", color, "}{", x, "}")
109 })
110 }
Hao Zhu35ecd272017-06-11 22:50:42 -0400111 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400112
Hao Zhua73601b2017-08-19 15:31:51 -0400113 if (!is.null(background)) {
114 new_row <- paste0("\\\\rowcolor{", background, "} ", new_row)
115 }
116
Hao Zhud2c0f732017-08-26 10:40:14 -0400117 out <- sub(target_row, new_row, enc2utf8(as.character(kable_input)),
118 perl = T)
Hao Zhu73604282017-06-11 22:08:48 -0400119 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400120 attr(out, "kable_meta") <- table_info
Hao Zhu73604282017-06-11 22:08:48 -0400121 return(out)
122}