blob: ee27be782c353f8352d0d5dd9aaa12d2787d09ea [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 Zhu53454f02018-01-14 16:29:10 -050032#' @param hline_after T/F. A replicate of `hline.after` in xtable. It
33#' addes a hline after ther row
34#' @param extra_latex_after Extra LaTeX text to be added after the row. Similar
35#' with `add.to.row` in xtable
Hao Zhu79f1e2a2017-06-11 20:55:30 -040036#'
37#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040038#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040039#'
40#' @export
41row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040042 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050043 underline = FALSE, strikeout = FALSE,
Hao Zhubacd2f32017-10-11 14:06:36 -040044 color = NULL, background = NULL, align = NULL,
Hao Zhu53454f02018-01-14 16:29:10 -050045 font_size = NULL, angle = NULL, extra_css = NULL,
46 hline_after = FALSE, extra_latex_after = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040047 if (!is.numeric(row)) {
Hao Zhu322de082017-09-11 19:25:29 -040048 stop("row must be numeric. ")
Hao Zhu79f1e2a2017-06-11 20:55:30 -040049 }
50 kable_format <- attr(kable_input, "format")
51 if (!kable_format %in% c("html", "latex")) {
52 message("Currently generic markdown table using pandoc is not supported.")
53 return(kable_input)
54 }
55 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040056 return(row_spec_html(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050057 underline, strikeout,
Hao Zhub1de9672018-01-08 16:29:24 -050058 color, background, align, font_size, angle,
59 extra_css))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040060 }
61 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040062 return(row_spec_latex(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050063 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -050064 color, background, align, font_size, angle,
65 hline_after, extra_latex_after))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040066 }
67}
68
Hao Zhu669bcd22017-08-19 14:53:40 -040069row_spec_html <- function(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050070 underline, strikeout,
Hao Zhub1de9672018-01-08 16:29:24 -050071 color, background, align, font_size, angle,
72 extra_css) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040073 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040074 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040075
Hao Zhu8d347c42017-10-10 13:11:19 -040076 if (!is.null(align)) {
77 if (align %in% c("l", "c", "r")) {
78 align <- switch(align, r = "right", c = "center", l = "left")
79 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040080 }
81
Hao Zhu8d347c42017-10-10 13:11:19 -040082 if (0 %in% row) {
83 kable_thead <- xml_tpart(kable_xml, "thead")
84 original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
85 for (theader_i in 1:length(xml_children(original_header_row))) {
86 target_header_cell <- xml_child(original_header_row, theader_i)
Hao Zhuef0c8302018-01-12 13:30:20 -050087 xml_cell_style(target_header_cell, bold, italic, monospace,
88 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050089 align, font_size, angle, extra_css)
Hao Zhu8d347c42017-10-10 13:11:19 -040090 }
91 row <- row[row != 0]
92 }
93
94 if (length(row) != 0) {
95 kable_tbody <- xml_tpart(kable_xml, "tbody")
96
97 group_header_rows <- attr(kable_input, "group_header_rows")
98 if (!is.null(group_header_rows)) {
99 row <- positions_corrector(row, group_header_rows,
100 length(xml_children(kable_tbody)))
101 }
102
103 for (j in row) {
104 target_row <- xml_child(kable_tbody, j)
105 for (i in 1:length(xml_children(target_row))) {
106 target_cell <- xml_child(target_row, i)
Hao Zhuef0c8302018-01-12 13:30:20 -0500107 xml_cell_style(target_cell, bold, italic, monospace,
108 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -0500109 align, font_size, angle, extra_css)
Hao Zhu322de082017-09-11 19:25:29 -0400110 }
Hao Zhu669bcd22017-08-19 14:53:40 -0400111 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400112 }
Hao Zhu322de082017-09-11 19:25:29 -0400113
Hao Zhuf2dfd142017-07-24 14:43:28 -0400114 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400115 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500116 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu79f1e2a2017-06-11 20:55:30 -0400117 return(out)
118}
119
Hao Zhuef0c8302018-01-12 13:30:20 -0500120xml_cell_style <- function(x, bold, italic, monospace,
121 underline, strikeout, color, background,
Hao Zhub1de9672018-01-08 16:29:24 -0500122 align, font_size, angle, extra_css) {
Hao Zhu8d347c42017-10-10 13:11:19 -0400123 if (bold) {
124 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400125 "font-weight: bold;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400126 }
127 if (italic) {
128 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400129 "font-style: italic;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400130 }
131 if (monospace) {
132 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400133 "font-family: monospace;")
Hao Zhu8d347c42017-10-10 13:11:19 -0400134 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500135 if (underline) {
136 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
137 "text-decoration: underline;")
138 }
139 if (strikeout) {
140 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
141 "text-decoration: line-through;")
142 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400143 if (!is.null(color)) {
144 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400145 "color: ", color, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400146 }
147 if (!is.null(background)) {
148 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Hao Zhubacd2f32017-10-11 14:06:36 -0400149 "background-color: ",
150 background, ";")
Hao Zhu8d347c42017-10-10 13:11:19 -0400151 }
152 if (!is.null(align)) {
153 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
154 "text-align: ", align, ";")
155 }
Hao Zhue7c8f702017-10-10 13:22:59 -0400156 if (!is.null(font_size)) {
157 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
158 "font-size: ", font_size, "px;")
159 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400160 if (!is.null(angle)) {
161 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
162 "-webkit-transform: rotate(", angle,
163 "deg); -moz-transform: rotate(", angle,
164 "deg); -ms-transform: rotate(", angle,
165 "deg); -o-transform: rotate(", angle,
Hao Zhu457acb42017-10-14 17:37:02 -0400166 "deg); transform: rotate(", angle,
Hao Zhubacd2f32017-10-11 14:06:36 -0400167 "deg);")
168 }
Hao Zhub1de9672018-01-08 16:29:24 -0500169 if (!is.null(extra_css)) {
170 xml_attr(x, "style") <- paste0(xml_attr(x, "style"), extra_css)
171 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400172 return(x)
173}
174
Hao Zhua73601b2017-08-19 15:31:51 -0400175row_spec_latex <- function(kable_input, row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500176 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -0500177 color, background, align, font_size, angle,
178 hline_after, extra_latex_after) {
Hao Zhu73604282017-06-11 22:08:48 -0400179 table_info <- magic_mirror(kable_input)
Hao Zhu322de082017-09-11 19:25:29 -0400180 out <- enc2utf8(as.character(kable_input))
Hao Zhu064990d2017-10-17 18:08:42 -0400181
182 if (table_info$duplicated_rows) {
183 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
184 out <- dup_fx_out[[1]]
185 table_info <- dup_fx_out[[2]]
186 }
187
Hao Zhu322de082017-09-11 19:25:29 -0400188 row <- row + 1
189 for (i in row) {
190 target_row <- table_info$contents[i]
191 new_row <- latex_new_row_builder(target_row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500192 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -0500193 color, background, align, font_size, angle,
194 hline_after, extra_latex_after)
195 out <- str_replace(out,
196 paste0(target_row, "\\\\\\\\"),
197 new_row)
Hao Zhu322de082017-09-11 19:25:29 -0400198 }
199
200 out <- structure(out, format = "latex", class = "knitr_kable")
201 attr(out, "kable_meta") <- table_info
202 return(out)
203}
204
205latex_new_row_builder <- function(target_row, bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500206 underline, strikeout,
Hao Zhu53454f02018-01-14 16:29:10 -0500207 color, background, align, font_size, angle,
208 hline_after, extra_latex_after) {
Hao Zhu73604282017-06-11 22:08:48 -0400209 new_row <- latex_row_cells(target_row)
210 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400211 new_row <- lapply(new_row, function(x) {
Hao Zhuef0c8302018-01-12 13:30:20 -0500212 paste0("\\\\textbf{", x, "}")
Hao Zhu35ecd272017-06-11 22:50:42 -0400213 })
Hao Zhu73604282017-06-11 22:08:48 -0400214 }
215 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400216 new_row <- lapply(new_row, function(x) {
217 paste0("\\\\em{", x, "}")
218 })
Hao Zhu73604282017-06-11 22:08:48 -0400219 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400220 if (monospace) {
221 new_row <- lapply(new_row, function(x) {
222 paste0("\\\\ttfamily{", x, "}")
223 })
224 }
Hao Zhuef0c8302018-01-12 13:30:20 -0500225 if (underline) {
226 new_row <- lapply(new_row, function(x) {
227 paste0("\\\\underline{", x, "}")
228 })
229 }
230 if (strikeout) {
231 new_row <- lapply(new_row, function(x) {
232 paste0("\\\\sout{", x, "}")
233 })
234 }
Hao Zhua73601b2017-08-19 15:31:51 -0400235 if (!is.null(color)) {
236 new_row <- lapply(new_row, function(x) {
Hao Zhu457acb42017-10-14 17:37:02 -0400237 paste0("\\\\textcolor", latex_color(color), "{", x, "}")
Hao Zhua73601b2017-08-19 15:31:51 -0400238 })
239 }
Hao Zhu8b32b192017-10-24 14:51:48 -0400240 if (!is.null(font_size)) {
241 new_row <- lapply(new_row, function(x) {
242 paste0("\\\\begingroup\\\\fontsize{", font_size, "}{",
243 as.numeric(font_size) + 2,
244 "}\\\\selectfont ", x, "\\\\endgroup")})
245 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400246 if (!is.null(align)) {
247 new_row <- lapply(new_row, function(x) {
248 paste0("\\\\multicolumn{1}{", align, "}{", x, "}")
249 })
250 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400251
252 if (!is.null(angle)) {
253 new_row <- lapply(new_row, function(x) {
254 paste0("\\\\rotatebox{", angle, "}{", x, "}")
255 })
256 }
257
Hao Zhu35ecd272017-06-11 22:50:42 -0400258 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400259
Hao Zhua73601b2017-08-19 15:31:51 -0400260 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500261 new_row <- paste0("\\\\rowcolor", latex_color(background), " ", new_row)
Hao Zhua73601b2017-08-19 15:31:51 -0400262 }
263
Hao Zhu53454f02018-01-14 16:29:10 -0500264 new_row <- paste0(new_row, "\\\\\\\\")
265
266 if (hline_after) {
267 new_row <- paste0(new_row, "\n\\\\hline")
268 }
269 if (!is.null(extra_latex_after)) {
270 new_row <- paste0(new_row, "\n",
271 regex_escape(extra_latex_after, double_backslash = TRUE))
272 }
Hao Zhu322de082017-09-11 19:25:29 -0400273 return(new_row)
Hao Zhu73604282017-06-11 22:08:48 -0400274}