blob: 5a3c617e6d6c204564ce4478b229c815fbfe2ea9 [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 Zhuef0c8302018-01-12 13:30:20 -050013#' @param monospace A T/F value to control whether the text of the selected row
Hao Zhu3e53e602017-07-26 12:40:57 -040014#' need to be monospaced (verbatim)
Hao Zhuef0c8302018-01-12 13:30:20 -050015#' @param underline A T/F value to control whether the text of the selected row
16#' need to be underlined
17#' @param strikeout A T/F value to control whether the text of the selected row
18#' need to be stricked out.
Hao Zhu457acb42017-10-14 17:37:02 -040019#' @param color A character string for row text color. For example, "red" or
20#' "#BBBBBB".
Hao Zhu8d347c42017-10-10 13:11:19 -040021#' @param background A character string for row background color. Here please
Hao Zhu53e240f2017-09-04 20:04:29 -040022#' pay attention to the differences in color codes between HTML and LaTeX.
Hao Zhu8d347c42017-10-10 13:11:19 -040023#' @param align A character string for cell alignment. For HTML, possible values could
24#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
25#' while for LaTeX, you can only choose from `l`, `c` & `r`.
Hao Zhu8b32b192017-10-24 14:51:48 -040026#' @param font_size A numeric input for font size. For HTML, you can also use
27#' options including `xx-small`, `x-small`, `small`, `medium`, `large`,
28#' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`.
Hao Zhubacd2f32017-10-11 14:06:36 -040029#' @param angle 0-360, degree that the text will rotate.
Hao Zhub1de9672018-01-08 16:29:24 -050030#' @param extra_css Extra css text to be passed into the cells of the row. Note
31#' that it's not for the whole row.
Hao Zhu79f1e2a2017-06-11 20:55:30 -040032#'
33#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040034#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040035#'
36#' @export
37row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040038 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050039 underline = FALSE, strikeout = FALSE,
Hao Zhubacd2f32017-10-11 14:06:36 -040040 color = NULL, background = NULL, align = NULL,
Hao Zhu593f57e2018-01-09 13:30:01 -050041 font_size = NULL, angle = NULL, extra_css = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040042 if (!is.numeric(row)) {
Hao Zhu322de082017-09-11 19:25:29 -040043 stop("row must be numeric. ")
Hao Zhu79f1e2a2017-06-11 20:55:30 -040044 }
45 kable_format <- attr(kable_input, "format")
46 if (!kable_format %in% c("html", "latex")) {
47 message("Currently generic markdown table using pandoc is not supported.")
48 return(kable_input)
49 }
50 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040051 return(row_spec_html(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050052 underline, strikeout,
Hao Zhub1de9672018-01-08 16:29:24 -050053 color, background, align, font_size, angle,
54 extra_css))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040055 }
56 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040057 return(row_spec_latex(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050058 underline, strikeout,
Hao Zhu8b32b192017-10-24 14:51:48 -040059 color, background, align, font_size, angle))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040060 }
61}
62
Hao Zhu669bcd22017-08-19 14:53:40 -040063row_spec_html <- function(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050064 underline, strikeout,
Hao Zhub1de9672018-01-08 16:29:24 -050065 color, background, align, font_size, angle,
66 extra_css) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040067 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040068 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040069
Hao Zhu8d347c42017-10-10 13:11:19 -040070 if (!is.null(align)) {
71 if (align %in% c("l", "c", "r")) {
72 align <- switch(align, r = "right", c = "center", l = "left")
73 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040074 }
75
Hao Zhu8d347c42017-10-10 13:11:19 -040076 if (0 %in% row) {
77 kable_thead <- xml_tpart(kable_xml, "thead")
78 original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
79 for (theader_i in 1:length(xml_children(original_header_row))) {
80 target_header_cell <- xml_child(original_header_row, theader_i)
Hao Zhuef0c8302018-01-12 13:30:20 -050081 xml_cell_style(target_header_cell, bold, italic, monospace,
82 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050083 align, font_size, angle, extra_css)
Hao Zhu8d347c42017-10-10 13:11:19 -040084 }
85 row <- row[row != 0]
86 }
87
88 if (length(row) != 0) {
89 kable_tbody <- xml_tpart(kable_xml, "tbody")
90
91 group_header_rows <- attr(kable_input, "group_header_rows")
92 if (!is.null(group_header_rows)) {
93 row <- positions_corrector(row, group_header_rows,
94 length(xml_children(kable_tbody)))
95 }
96
97 for (j in row) {
98 target_row <- xml_child(kable_tbody, j)
99 for (i in 1:length(xml_children(target_row))) {
100 target_cell <- xml_child(target_row, i)
Hao Zhuef0c8302018-01-12 13:30:20 -0500101 xml_cell_style(target_cell, bold, italic, monospace,
102 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -0500103 align, font_size, angle, extra_css)
Hao Zhu322de082017-09-11 19:25:29 -0400104 }
Hao Zhu669bcd22017-08-19 14:53:40 -0400105 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400106 }
Hao Zhu322de082017-09-11 19:25:29 -0400107
Hao Zhuf2dfd142017-07-24 14:43:28 -0400108 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400109 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500110 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400111 return(out)
112}
113
Hao Zhuef0c8302018-01-12 13:30:20 -0500114xml_cell_style <- function(x, bold, italic, monospace,
115 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -0500116 align, font_size, angle, extra_css) {
Hao Zhu8d347c42017-10-10 13:11:19 -0400117 if (bold) {
118 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400119 "font-weight: bold;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400120 }
121 if (italic) {
122 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400123 "font-style: italic;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400124 }
125 if (monospace) {
126 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400127 "font-family: monospace;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400128 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500129 if (underline) {
130 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
131 "text-decoration: underline;")
132 }
133 if (strikeout) {
134 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
135 "text-decoration: line-through;")
136 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400137 if (!is.null(color)) {
138 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400139 "color: ", color, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400140 }
141 if (!is.null(background)) {
142 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400143 "background-color: ",
144 background, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400145 }
146 if (!is.null(align)) {
147 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
148 "text-align: ", align, ";")
149 }
Hao Zhue7c8f702017-10-10 13:22:59 -0400150 if (!is.null(font_size)) {
151 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
152 "font-size: ", font_size, "px;")
153 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400154 if (!is.null(angle)) {
155 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
156 "-webkit-transform: rotate(", angle,
157 "deg); -moz-transform: rotate(", angle,
158 "deg); -ms-transform: rotate(", angle,
159 "deg); -o-transform: rotate(", angle,
Hao Zhu457acb42017-10-14 17:37:02 -0400160 "deg); transform: rotate(", angle,
Hao Zhubacd2f32017-10-11 14:06:36 -0400161 "deg);")
162 }
Hao Zhub1de9672018-01-08 16:29:24 -0500163 if (!is.null(extra_css)) {
164 xml_attr(x, "style") <- paste0(xml_attr(x, "style"), extra_css)
165 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400166 return(x)
167}
168
Hao Zhua73601b2017-08-19 15:31:51 -0400169row_spec_latex <- function(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500170 underline, strikeout,
Hao Zhu8b32b192017-10-24 14:51:48 -0400171 color, background, align, font_size, angle) {
Hao Zhu73604282017-06-11 22:08:48 -0400172 table_info <- magic_mirror(kable_input)
Hao Zhu322de082017-09-11 19:25:29 -0400173 out <- enc2utf8(as.character(kable_input))
Hao Zhu064990d2017-10-17 18:08:42 -0400174
175 if (table_info$duplicated_rows) {
176 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
177 out <- dup_fx_out[[1]]
178 table_info <- dup_fx_out[[2]]
179 }
180
Hao Zhu322de082017-09-11 19:25:29 -0400181 row <- row + 1
182 for (i in row) {
183 target_row <- table_info$contents[i]
184 new_row <- latex_new_row_builder(target_row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500185 underline, strikeout,
Hao Zhu8b32b192017-10-24 14:51:48 -0400186 color, background, align, font_size, angle)
Hao Zhu322de082017-09-11 19:25:29 -0400187 out <- sub(target_row, new_row, out, perl = T)
188 }
189
190 out <- structure(out, format = "latex", class = "knitr_kable")
191 attr(out, "kable_meta") <- table_info
192 return(out)
193}
194
195latex_new_row_builder <- function(target_row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500196 underline, strikeout,
Hao Zhu8b32b192017-10-24 14:51:48 -0400197 color, background, align, font_size, angle) {
Hao Zhu73604282017-06-11 22:08:48 -0400198 new_row <- latex_row_cells(target_row)
199 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400200 new_row <- lapply(new_row, function(x) {
Hao Zhuef0c8302018-01-12 13:30:20 -0500201 paste0("\\\\textbf{", x, "}")
Hao Zhu35ecd272017-06-11 22:50:42 -0400202 })
Hao Zhu73604282017-06-11 22:08:48 -0400203 }
204 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400205 new_row <- lapply(new_row, function(x) {
206 paste0("\\\\em{", x, "}")
207 })
Hao Zhu73604282017-06-11 22:08:48 -0400208 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400209 if (monospace) {
210 new_row <- lapply(new_row, function(x) {
211 paste0("\\\\ttfamily{", x, "}")
212 })
213 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500214 if (underline) {
215 new_row <- lapply(new_row, function(x) {
216 paste0("\\\\underline{", x, "}")
217 })
218 }
219 if (strikeout) {
220 new_row <- lapply(new_row, function(x) {
221 paste0("\\\\sout{", x, "}")
222 })
223 }
Hao Zhua73601b2017-08-19 15:31:51 -0400224 if (!is.null(color)) {
225 new_row <- lapply(new_row, function(x) {
Hao Zhu457acb42017-10-14 17:37:02 -0400226 paste0("\\\\textcolor", latex_color(color), "{", x, "}")
Hao Zhua73601b2017-08-19 15:31:51 -0400227 })
228 }
Hao Zhu8b32b192017-10-24 14:51:48 -0400229 if (!is.null(font_size)) {
230 new_row <- lapply(new_row, function(x) {
231 paste0("\\\\begingroup\\\\fontsize{", font_size, "}{",
232 as.numeric(font_size) + 2,
233 "}\\\\selectfont ", x, "\\\\endgroup")})
234 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400235 if (!is.null(align)) {
236 new_row <- lapply(new_row, function(x) {
237 paste0("\\\\multicolumn{1}{", align, "}{", x, "}")
238 })
239 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400240
241 if (!is.null(angle)) {
242 new_row <- lapply(new_row, function(x) {
243 paste0("\\\\rotatebox{", angle, "}{", x, "}")
244 })
245 }
246
Hao Zhu35ecd272017-06-11 22:50:42 -0400247 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400248
Hao Zhua73601b2017-08-19 15:31:51 -0400249 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500250 new_row <- paste0("\\\\rowcolor", latex_color(background), " ", new_row)
Hao Zhua73601b2017-08-19 15:31:51 -0400251 }
252
Hao Zhu322de082017-09-11 19:25:29 -0400253 return(new_row)
Hao Zhu73604282017-06-11 22:08:48 -0400254}