blob: ec8a959bc3ce12fe5c6779236d492c537d59f6d0 [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 Zhu457acb42017-10-14 17:37:02 -040015#' @param color A character string for row text color. For example, "red" or
16#' "#BBBBBB".
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 Zhubacd2f32017-10-11 14:06:36 -040022#' @param font_size Only if you want to specify font size locally in HTML.
23#' This feature is not available in LaTeX
24#' @param angle 0-360, degree that the text will rotate.
Hao Zhu79f1e2a2017-06-11 20:55:30 -040025#'
26#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040027#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040028#'
29#' @export
30row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040031 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhubacd2f32017-10-11 14:06:36 -040032 color = NULL, background = NULL, align = NULL,
33 font_size = NULL, angle = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040034 if (!is.numeric(row)) {
Hao Zhu322de082017-09-11 19:25:29 -040035 stop("row must be numeric. ")
Hao Zhu79f1e2a2017-06-11 20:55:30 -040036 }
37 kable_format <- attr(kable_input, "format")
38 if (!kable_format %in% c("html", "latex")) {
39 message("Currently generic markdown table using pandoc is not supported.")
40 return(kable_input)
41 }
42 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040043 return(row_spec_html(kable_input, row, bold, italic, monospace,
Hao Zhubacd2f32017-10-11 14:06:36 -040044 color, background, align, font_size, angle))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040045 }
46 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040047 return(row_spec_latex(kable_input, row, bold, italic, monospace,
Hao Zhubacd2f32017-10-11 14:06:36 -040048 color, background, align, angle))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040049 }
50}
51
Hao Zhu669bcd22017-08-19 14:53:40 -040052row_spec_html <- function(kable_input, row, bold, italic, monospace,
Hao Zhubacd2f32017-10-11 14:06:36 -040053 color, background, align, font_size, angle) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040054 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040055 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040056
Hao Zhu8d347c42017-10-10 13:11:19 -040057 if (!is.null(align)) {
58 if (align %in% c("l", "c", "r")) {
59 align <- switch(align, r = "right", c = "center", l = "left")
60 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040061 }
62
Hao Zhu8d347c42017-10-10 13:11:19 -040063 if (0 %in% row) {
64 kable_thead <- xml_tpart(kable_xml, "thead")
65 original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
66 for (theader_i in 1:length(xml_children(original_header_row))) {
67 target_header_cell <- xml_child(original_header_row, theader_i)
Hao Zhue7c8f702017-10-10 13:22:59 -040068 xml_cell_style(target_header_cell, bold, italic, monospace, color, background,
Hao Zhubacd2f32017-10-11 14:06:36 -040069 align, font_size, angle)
Hao Zhu8d347c42017-10-10 13:11:19 -040070 }
71 row <- row[row != 0]
72 }
73
74 if (length(row) != 0) {
75 kable_tbody <- xml_tpart(kable_xml, "tbody")
76
77 group_header_rows <- attr(kable_input, "group_header_rows")
78 if (!is.null(group_header_rows)) {
79 row <- positions_corrector(row, group_header_rows,
80 length(xml_children(kable_tbody)))
81 }
82
83 for (j in row) {
84 target_row <- xml_child(kable_tbody, j)
85 for (i in 1:length(xml_children(target_row))) {
86 target_cell <- xml_child(target_row, i)
Hao Zhue7c8f702017-10-10 13:22:59 -040087 xml_cell_style(target_cell, bold, italic, monospace, color, background,
Hao Zhubacd2f32017-10-11 14:06:36 -040088 align, font_size, angle)
Hao Zhu322de082017-09-11 19:25:29 -040089 }
Hao Zhu669bcd22017-08-19 14:53:40 -040090 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040091 }
Hao Zhu322de082017-09-11 19:25:29 -040092
Hao Zhuf2dfd142017-07-24 14:43:28 -040093 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040094 attributes(out) <- kable_attrs
95 return(out)
96}
97
Hao Zhubacd2f32017-10-11 14:06:36 -040098xml_cell_style <- function(x, bold, italic, monospace, color, background,
99 align, font_size, angle) {
Hao Zhu8d347c42017-10-10 13:11:19 -0400100 if (bold) {
101 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400102 "font-weight: bold;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400103 }
104 if (italic) {
105 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400106 "font-style: italic;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400107 }
108 if (monospace) {
109 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400110 "font-family: monospace;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400111 }
112 if (!is.null(color)) {
113 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400114 "color: ", color, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400115 }
116 if (!is.null(background)) {
117 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400118 "background-color: ",
119 background, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400120 }
121 if (!is.null(align)) {
122 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
123 "text-align: ", align, ";")
124 }
Hao Zhue7c8f702017-10-10 13:22:59 -0400125 if (!is.null(font_size)) {
126 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
127 "font-size: ", font_size, "px;")
128 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400129 if (!is.null(angle)) {
130 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
131 "-webkit-transform: rotate(", angle,
132 "deg); -moz-transform: rotate(", angle,
133 "deg); -ms-transform: rotate(", angle,
134 "deg); -o-transform: rotate(", angle,
Hao Zhu457acb42017-10-14 17:37:02 -0400135 "deg); transform: rotate(", angle,
Hao Zhubacd2f32017-10-11 14:06:36 -0400136 "deg);")
137 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400138 return(x)
139}
140
Hao Zhua73601b2017-08-19 15:31:51 -0400141row_spec_latex <- function(kable_input, row, bold, italic, monospace,
Hao Zhubacd2f32017-10-11 14:06:36 -0400142 color, background, align, angle) {
Hao Zhu73604282017-06-11 22:08:48 -0400143 table_info <- magic_mirror(kable_input)
Hao Zhu322de082017-09-11 19:25:29 -0400144 out <- enc2utf8(as.character(kable_input))
145 row <- row + 1
146 for (i in row) {
147 target_row <- table_info$contents[i]
148 new_row <- latex_new_row_builder(target_row, bold, italic, monospace,
Hao Zhubacd2f32017-10-11 14:06:36 -0400149 color, background, align, angle)
Hao Zhu322de082017-09-11 19:25:29 -0400150 out <- sub(target_row, new_row, out, perl = T)
151 }
152
153 out <- structure(out, format = "latex", class = "knitr_kable")
154 attr(out, "kable_meta") <- table_info
155 return(out)
156}
157
158latex_new_row_builder <- function(target_row, bold, italic, monospace,
Hao Zhubacd2f32017-10-11 14:06:36 -0400159 color, background, align, angle) {
Hao Zhu73604282017-06-11 22:08:48 -0400160 new_row <- latex_row_cells(target_row)
161 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400162 new_row <- lapply(new_row, function(x) {
163 paste0("\\\\bfseries{", x, "}")
164 })
Hao Zhu73604282017-06-11 22:08:48 -0400165 }
166 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400167 new_row <- lapply(new_row, function(x) {
168 paste0("\\\\em{", x, "}")
169 })
Hao Zhu73604282017-06-11 22:08:48 -0400170 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400171 if (monospace) {
172 new_row <- lapply(new_row, function(x) {
173 paste0("\\\\ttfamily{", x, "}")
174 })
175 }
Hao Zhua73601b2017-08-19 15:31:51 -0400176
177 if (!is.null(color)) {
178 new_row <- lapply(new_row, function(x) {
Hao Zhu457acb42017-10-14 17:37:02 -0400179 paste0("\\\\textcolor", latex_color(color), "{", x, "}")
Hao Zhua73601b2017-08-19 15:31:51 -0400180 })
181 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400182
183 if (!is.null(align)) {
184 new_row <- lapply(new_row, function(x) {
185 paste0("\\\\multicolumn{1}{", align, "}{", x, "}")
186 })
187 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400188
189 if (!is.null(angle)) {
190 new_row <- lapply(new_row, function(x) {
191 paste0("\\\\rotatebox{", angle, "}{", x, "}")
192 })
193 }
194
Hao Zhu35ecd272017-06-11 22:50:42 -0400195 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400196
Hao Zhua73601b2017-08-19 15:31:51 -0400197 if (!is.null(background)) {
198 new_row <- paste0("\\\\rowcolor{", background, "} ", new_row)
199 }
200
Hao Zhu322de082017-09-11 19:25:29 -0400201 return(new_row)
Hao Zhu73604282017-06-11 22:08:48 -0400202}