blob: 55a8bed4a21395fdd9035b0ac510e8c737716505 [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 Zhu3e53e602017-07-26 12:40:57 -040022 bold = FALSE, italic = FALSE, monospace = FALSE) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040023 if (!is.numeric(row)) {
24 stop("row must be a numeric value")
25 }
26 kable_format <- attr(kable_input, "format")
27 if (!kable_format %in% c("html", "latex")) {
28 message("Currently generic markdown table using pandoc is not supported.")
29 return(kable_input)
30 }
31 if (kable_format == "html") {
Hao Zhu3e53e602017-07-26 12:40:57 -040032 return(row_spec_html(kable_input, row, bold, italic, monospace))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040033 }
34 if (kable_format == "latex") {
Hao Zhu3e53e602017-07-26 12:40:57 -040035 return(row_spec_latex(kable_input, row, bold, italic, monospace))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040036 }
37}
38
Hao Zhu3e53e602017-07-26 12:40:57 -040039row_spec_html <- function(kable_input, row, bold, italic, monospace) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040040 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040041 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040042 kable_tbody <- xml_tpart(kable_xml, "tbody")
43
44 group_header_rows <- attr(kable_input, "group_header_rows")
45 if (!is.null(group_header_rows)) {
46 row <- positions_corrector(row, group_header_rows,
47 length(xml_children(kable_tbody)))
48 }
49
50 target_row <- xml_child(kable_tbody, row)
51
52 for (i in 1:length(xml_children(target_row))) {
53 target_cell <- xml_child(target_row, i)
54 if (bold) {
55 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
56 "font-weight: bold;")
57 }
58 if (italic) {
59 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
60 "font-style: italic;")
61 }
Hao Zhu3e53e602017-07-26 12:40:57 -040062 if (monospace) {
63 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
64 "font-family: monospace;")
65 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040066 }
Hao Zhuf2dfd142017-07-24 14:43:28 -040067 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040068 attributes(out) <- kable_attrs
69 return(out)
70}
71
Hao Zhu3e53e602017-07-26 12:40:57 -040072row_spec_latex <- function(kable_input, row, bold, italic, monospace) {
Hao Zhu73604282017-06-11 22:08:48 -040073 table_info <- magic_mirror(kable_input)
74 target_row <- table_info$contents[row + 1]
75 new_row <- latex_row_cells(target_row)
76 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -040077 new_row <- lapply(new_row, function(x) {
78 paste0("\\\\bfseries{", x, "}")
79 })
Hao Zhu73604282017-06-11 22:08:48 -040080 }
81 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -040082 new_row <- lapply(new_row, function(x) {
83 paste0("\\\\em{", x, "}")
84 })
Hao Zhu73604282017-06-11 22:08:48 -040085 }
Hao Zhu3e53e602017-07-26 12:40:57 -040086 if (monospace) {
87 new_row <- lapply(new_row, function(x) {
88 paste0("\\\\ttfamily{", x, "}")
89 })
90 }
Hao Zhu35ecd272017-06-11 22:50:42 -040091 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -040092
93 out <- sub(target_row, new_row, as.character(kable_input), perl = T)
94 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -040095 attr(out, "kable_meta") <- table_info
Hao Zhu73604282017-06-11 22:08:48 -040096 return(out)
97}