blob: 88bf499b122bf8a99cb7d2925703c2e6c8ff4940 [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 Zhu8b32b192017-10-24 14:51:48 -040022#' @param font_size A numeric input for font size. For HTML, you can also use
23#' options including `xx-small`, `x-small`, `small`, `medium`, `large`,
24#' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`.
Hao Zhubacd2f32017-10-11 14:06:36 -040025#' @param angle 0-360, degree that the text will rotate.
Hao Zhub1de9672018-01-08 16:29:24 -050026#' @param extra_css Extra css text to be passed into the cells of the row. Note
27#' that it's not for the whole row.
Hao Zhu79f1e2a2017-06-11 20:55:30 -040028#'
29#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040030#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040031#'
32#' @export
33row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040034 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhubacd2f32017-10-11 14:06:36 -040035 color = NULL, background = NULL, align = NULL,
Hao Zhu593f57e2018-01-09 13:30:01 -050036 font_size = NULL, angle = NULL, extra_css = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040037 if (!is.numeric(row)) {
Hao Zhu322de082017-09-11 19:25:29 -040038 stop("row must be numeric. ")
Hao Zhu79f1e2a2017-06-11 20:55:30 -040039 }
40 kable_format <- attr(kable_input, "format")
41 if (!kable_format %in% c("html", "latex")) {
42 message("Currently generic markdown table using pandoc is not supported.")
43 return(kable_input)
44 }
45 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040046 return(row_spec_html(kable_input, row, bold, italic, monospace,
Hao Zhub1de9672018-01-08 16:29:24 -050047 color, background, align, font_size, angle,
48 extra_css))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040049 }
50 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040051 return(row_spec_latex(kable_input, row, bold, italic, monospace,
Hao Zhu8b32b192017-10-24 14:51:48 -040052 color, background, align, font_size, angle))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040053 }
54}
55
Hao Zhu669bcd22017-08-19 14:53:40 -040056row_spec_html <- function(kable_input, row, bold, italic, monospace,
Hao Zhub1de9672018-01-08 16:29:24 -050057 color, background, align, font_size, angle,
58 extra_css) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040059 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040060 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040061
Hao Zhu8d347c42017-10-10 13:11:19 -040062 if (!is.null(align)) {
63 if (align %in% c("l", "c", "r")) {
64 align <- switch(align, r = "right", c = "center", l = "left")
65 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040066 }
67
Hao Zhu8d347c42017-10-10 13:11:19 -040068 if (0 %in% row) {
69 kable_thead <- xml_tpart(kable_xml, "thead")
70 original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
71 for (theader_i in 1:length(xml_children(original_header_row))) {
72 target_header_cell <- xml_child(original_header_row, theader_i)
Hao Zhue7c8f702017-10-10 13:22:59 -040073 xml_cell_style(target_header_cell, bold, italic, monospace, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050074 align, font_size, angle, extra_css)
Hao Zhu8d347c42017-10-10 13:11:19 -040075 }
76 row <- row[row != 0]
77 }
78
79 if (length(row) != 0) {
80 kable_tbody <- xml_tpart(kable_xml, "tbody")
81
82 group_header_rows <- attr(kable_input, "group_header_rows")
83 if (!is.null(group_header_rows)) {
84 row <- positions_corrector(row, group_header_rows,
85 length(xml_children(kable_tbody)))
86 }
87
88 for (j in row) {
89 target_row <- xml_child(kable_tbody, j)
90 for (i in 1:length(xml_children(target_row))) {
91 target_cell <- xml_child(target_row, i)
Hao Zhue7c8f702017-10-10 13:22:59 -040092 xml_cell_style(target_cell, bold, italic, monospace, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050093 align, font_size, angle, extra_css)
Hao Zhu322de082017-09-11 19:25:29 -040094 }
Hao Zhu669bcd22017-08-19 14:53:40 -040095 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040096 }
Hao Zhu322de082017-09-11 19:25:29 -040097
Hao Zhuf2dfd142017-07-24 14:43:28 -040098 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040099 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500100 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400101 return(out)
102}
103
Hao Zhubacd2f32017-10-11 14:06:36 -0400104xml_cell_style <- function(x, bold, italic, monospace, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -0500105 align, font_size, angle, extra_css) {
Hao Zhu8d347c42017-10-10 13:11:19 -0400106 if (bold) {
107 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400108 "font-weight: bold;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400109 }
110 if (italic) {
111 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400112 "font-style: italic;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400113 }
114 if (monospace) {
115 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400116 "font-family: monospace;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400117 }
118 if (!is.null(color)) {
119 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400120 "color: ", color, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400121 }
122 if (!is.null(background)) {
123 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400124 "background-color: ",
125 background, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400126 }
127 if (!is.null(align)) {
128 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
129 "text-align: ", align, ";")
130 }
Hao Zhue7c8f702017-10-10 13:22:59 -0400131 if (!is.null(font_size)) {
132 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
133 "font-size: ", font_size, "px;")
134 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400135 if (!is.null(angle)) {
136 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
137 "-webkit-transform: rotate(", angle,
138 "deg); -moz-transform: rotate(", angle,
139 "deg); -ms-transform: rotate(", angle,
140 "deg); -o-transform: rotate(", angle,
Hao Zhu457acb42017-10-14 17:37:02 -0400141 "deg); transform: rotate(", angle,
Hao Zhubacd2f32017-10-11 14:06:36 -0400142 "deg);")
143 }
Hao Zhub1de9672018-01-08 16:29:24 -0500144 if (!is.null(extra_css)) {
145 xml_attr(x, "style") <- paste0(xml_attr(x, "style"), extra_css)
146 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400147 return(x)
148}
149
Hao Zhua73601b2017-08-19 15:31:51 -0400150row_spec_latex <- function(kable_input, row, bold, italic, monospace,
Hao Zhu8b32b192017-10-24 14:51:48 -0400151 color, background, align, font_size, angle) {
Hao Zhu73604282017-06-11 22:08:48 -0400152 table_info <- magic_mirror(kable_input)
Hao Zhu322de082017-09-11 19:25:29 -0400153 out <- enc2utf8(as.character(kable_input))
Hao Zhu064990d2017-10-17 18:08:42 -0400154
155 if (table_info$duplicated_rows) {
156 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
157 out <- dup_fx_out[[1]]
158 table_info <- dup_fx_out[[2]]
159 }
160
Hao Zhu322de082017-09-11 19:25:29 -0400161 row <- row + 1
162 for (i in row) {
163 target_row <- table_info$contents[i]
164 new_row <- latex_new_row_builder(target_row, bold, italic, monospace,
Hao Zhu8b32b192017-10-24 14:51:48 -0400165 color, background, align, font_size, angle)
Hao Zhu322de082017-09-11 19:25:29 -0400166 out <- sub(target_row, new_row, out, perl = T)
167 }
168
169 out <- structure(out, format = "latex", class = "knitr_kable")
170 attr(out, "kable_meta") <- table_info
171 return(out)
172}
173
174latex_new_row_builder <- function(target_row, bold, italic, monospace,
Hao Zhu8b32b192017-10-24 14:51:48 -0400175 color, background, align, font_size, angle) {
Hao Zhu73604282017-06-11 22:08:48 -0400176 new_row <- latex_row_cells(target_row)
177 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400178 new_row <- lapply(new_row, function(x) {
179 paste0("\\\\bfseries{", x, "}")
180 })
Hao Zhu73604282017-06-11 22:08:48 -0400181 }
182 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400183 new_row <- lapply(new_row, function(x) {
184 paste0("\\\\em{", x, "}")
185 })
Hao Zhu73604282017-06-11 22:08:48 -0400186 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400187 if (monospace) {
188 new_row <- lapply(new_row, function(x) {
189 paste0("\\\\ttfamily{", x, "}")
190 })
191 }
Hao Zhua73601b2017-08-19 15:31:51 -0400192
193 if (!is.null(color)) {
194 new_row <- lapply(new_row, function(x) {
Hao Zhu457acb42017-10-14 17:37:02 -0400195 paste0("\\\\textcolor", latex_color(color), "{", x, "}")
Hao Zhua73601b2017-08-19 15:31:51 -0400196 })
197 }
Hao Zhu8b32b192017-10-24 14:51:48 -0400198 if (!is.null(font_size)) {
199 new_row <- lapply(new_row, function(x) {
200 paste0("\\\\begingroup\\\\fontsize{", font_size, "}{",
201 as.numeric(font_size) + 2,
202 "}\\\\selectfont ", x, "\\\\endgroup")})
203 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400204 if (!is.null(align)) {
205 new_row <- lapply(new_row, function(x) {
206 paste0("\\\\multicolumn{1}{", align, "}{", x, "}")
207 })
208 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400209
210 if (!is.null(angle)) {
211 new_row <- lapply(new_row, function(x) {
212 paste0("\\\\rotatebox{", angle, "}{", x, "}")
213 })
214 }
215
Hao Zhu35ecd272017-06-11 22:50:42 -0400216 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400217
Hao Zhua73601b2017-08-19 15:31:51 -0400218 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500219 new_row <- paste0("\\\\rowcolor", latex_color(background), " ", new_row)
Hao Zhua73601b2017-08-19 15:31:51 -0400220 }
221
Hao Zhu322de082017-09-11 19:25:29 -0400222 return(new_row)
Hao Zhu73604282017-06-11 22:08:48 -0400223}