blob: 19c5d00ff72c0c76c040d45d399526a81f3387f9 [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
Hao Zhu8d347c42017-10-10 13:11:19 -04004#' its look. It can also specify the format of the header row when `row` = 0.
Hao Zhu79f1e2a2017-06-11 20:55:30 -04005#'
6#' @param kable_input Output of `knitr::kable()` with `format` specified
Hao Zhu322de082017-09-11 19:25:29 -04007#' @param row A numeric value or vector indicating which row(s) to be selected. You don't
Hao Zhu909ea382017-06-12 15:43:47 -04008#' need to count in header rows or group labeling rows.
Hao Zhu79f1e2a2017-06-11 20:55:30 -04009#' @param bold A T/F value to control whether the text of the selected row
10#' need to be bolded.
11#' @param italic A T/F value to control whether the text of the selected row
12#' need to be emphasized.
Hao Zhu3e53e602017-07-26 12:40:57 -040013#' @param monospace A T/F value to control whether the text of the selected column
14#' need to be monospaced (verbatim)
Hao Zhu8d347c42017-10-10 13:11:19 -040015#' @param color A character string for row text color. Here please pay
Hao Zhu53e240f2017-09-04 20:04:29 -040016#' attention to the differences in color codes between HTML and LaTeX.
Hao Zhu8d347c42017-10-10 13:11:19 -040017#' @param background A character string for row background color. Here please
Hao Zhu53e240f2017-09-04 20:04:29 -040018#' pay attention to the differences in color codes between HTML and LaTeX.
Hao Zhu8d347c42017-10-10 13:11:19 -040019#' @param align A character string for cell alignment. For HTML, possible values could
20#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
21#' while for LaTeX, you can only choose from `l`, `c` & `r`.
Hao Zhue7c8f702017-10-10 13:22:59 -040022#' @param font_size Only if you want to specify font size locally in HTML. This feature
23#' is not available in LaTeX
Hao Zhu79f1e2a2017-06-11 20:55:30 -040024#'
25#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040026#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040027#'
28#' @export
29row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040030 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhue7c8f702017-10-10 13:22:59 -040031 color = NULL, background = NULL, align = NULL, font_size = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040032 if (!is.numeric(row)) {
Hao Zhu322de082017-09-11 19:25:29 -040033 stop("row must be numeric. ")
Hao Zhu79f1e2a2017-06-11 20:55:30 -040034 }
35 kable_format <- attr(kable_input, "format")
36 if (!kable_format %in% c("html", "latex")) {
37 message("Currently generic markdown table using pandoc is not supported.")
38 return(kable_input)
39 }
40 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040041 return(row_spec_html(kable_input, row, bold, italic, monospace,
Hao Zhue7c8f702017-10-10 13:22:59 -040042 color, background, align, font_size))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040043 }
44 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040045 return(row_spec_latex(kable_input, row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -040046 color, background, align))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040047 }
48}
49
Hao Zhu669bcd22017-08-19 14:53:40 -040050row_spec_html <- function(kable_input, row, bold, italic, monospace,
Hao Zhue7c8f702017-10-10 13:22:59 -040051 color, background, align, font_size) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040052 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040053 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040054
Hao Zhu8d347c42017-10-10 13:11:19 -040055 if (!is.null(align)) {
56 if (align %in% c("l", "c", "r")) {
57 align <- switch(align, r = "right", c = "center", l = "left")
58 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040059 }
60
Hao Zhu8d347c42017-10-10 13:11:19 -040061 if (0 %in% row) {
62 kable_thead <- xml_tpart(kable_xml, "thead")
63 original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
64 for (theader_i in 1:length(xml_children(original_header_row))) {
65 target_header_cell <- xml_child(original_header_row, theader_i)
Hao Zhue7c8f702017-10-10 13:22:59 -040066 xml_cell_style(target_header_cell, bold, italic, monospace, color, background,
67 align, font_size)
Hao Zhu8d347c42017-10-10 13:11:19 -040068 }
69 row <- row[row != 0]
70 }
71
72 if (length(row) != 0) {
73 kable_tbody <- xml_tpart(kable_xml, "tbody")
74
75 group_header_rows <- attr(kable_input, "group_header_rows")
76 if (!is.null(group_header_rows)) {
77 row <- positions_corrector(row, group_header_rows,
78 length(xml_children(kable_tbody)))
79 }
80
81 for (j in row) {
82 target_row <- xml_child(kable_tbody, j)
83 for (i in 1:length(xml_children(target_row))) {
84 target_cell <- xml_child(target_row, i)
Hao Zhue7c8f702017-10-10 13:22:59 -040085 xml_cell_style(target_cell, bold, italic, monospace, color, background,
86 align, font_size)
Hao Zhu322de082017-09-11 19:25:29 -040087 }
Hao Zhu669bcd22017-08-19 14:53:40 -040088 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040089 }
Hao Zhu322de082017-09-11 19:25:29 -040090
Hao Zhuf2dfd142017-07-24 14:43:28 -040091 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040092 attributes(out) <- kable_attrs
93 return(out)
94}
95
Hao Zhue7c8f702017-10-10 13:22:59 -040096xml_cell_style <- function(x, bold, italic, monospace, color, background, align, font_size) {
Hao Zhu8d347c42017-10-10 13:11:19 -040097 if (bold) {
98 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
99 "font-weight: bold;")
100 }
101 if (italic) {
102 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
103 "font-style: italic;")
104 }
105 if (monospace) {
106 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
107 "font-family: monospace;")
108 }
109 if (!is.null(color)) {
110 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
111 "color: ", color, ";")
112 }
113 if (!is.null(background)) {
114 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
115 "background-color: ",
116 background, ";")
117 }
118 if (!is.null(align)) {
119 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
120 "text-align: ", align, ";")
121 }
Hao Zhue7c8f702017-10-10 13:22:59 -0400122 if (!is.null(font_size)) {
123 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
124 "font-size: ", font_size, "px;")
125 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400126 return(x)
127}
128
Hao Zhua73601b2017-08-19 15:31:51 -0400129row_spec_latex <- function(kable_input, row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -0400130 color, background, align) {
Hao Zhu73604282017-06-11 22:08:48 -0400131 table_info <- magic_mirror(kable_input)
Hao Zhu322de082017-09-11 19:25:29 -0400132 out <- enc2utf8(as.character(kable_input))
133 row <- row + 1
134 for (i in row) {
135 target_row <- table_info$contents[i]
136 new_row <- latex_new_row_builder(target_row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -0400137 color, background, align)
Hao Zhu322de082017-09-11 19:25:29 -0400138 out <- sub(target_row, new_row, out, perl = T)
139 }
140
141 out <- structure(out, format = "latex", class = "knitr_kable")
142 attr(out, "kable_meta") <- table_info
143 return(out)
144}
145
146latex_new_row_builder <- function(target_row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -0400147 color, background, align) {
Hao Zhu73604282017-06-11 22:08:48 -0400148 new_row <- latex_row_cells(target_row)
149 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400150 new_row <- lapply(new_row, function(x) {
151 paste0("\\\\bfseries{", x, "}")
152 })
Hao Zhu73604282017-06-11 22:08:48 -0400153 }
154 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400155 new_row <- lapply(new_row, function(x) {
156 paste0("\\\\em{", x, "}")
157 })
Hao Zhu73604282017-06-11 22:08:48 -0400158 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400159 if (monospace) {
160 new_row <- lapply(new_row, function(x) {
161 paste0("\\\\ttfamily{", x, "}")
162 })
163 }
Hao Zhua73601b2017-08-19 15:31:51 -0400164
165 if (!is.null(color)) {
166 new_row <- lapply(new_row, function(x) {
167 paste0("\\\\textcolor{", color, "}{", x, "}")
168 })
169 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400170
171 if (!is.null(align)) {
172 new_row <- lapply(new_row, function(x) {
173 paste0("\\\\multicolumn{1}{", align, "}{", x, "}")
174 })
175 }
Hao Zhu35ecd272017-06-11 22:50:42 -0400176 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400177
Hao Zhua73601b2017-08-19 15:31:51 -0400178 if (!is.null(background)) {
179 new_row <- paste0("\\\\rowcolor{", background, "} ", new_row)
180 }
181
Hao Zhu8d347c42017-10-10 13:11:19 -0400182
183
Hao Zhu322de082017-09-11 19:25:29 -0400184 return(new_row)
Hao Zhu73604282017-06-11 22:08:48 -0400185}