blob: f45cb3df2759a0c861f244f2c622018893c00b60 [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 Zhu79f1e2a2017-06-11 20:55:30 -040022#'
23#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040024#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040025#'
26#' @export
27row_spec <- function(kable_input, row,
Hao Zhu669bcd22017-08-19 14:53:40 -040028 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhu8d347c42017-10-10 13:11:19 -040029 color = NULL, background = NULL, align = NULL) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040030 if (!is.numeric(row)) {
Hao Zhu322de082017-09-11 19:25:29 -040031 stop("row must be numeric. ")
Hao Zhu79f1e2a2017-06-11 20:55:30 -040032 }
33 kable_format <- attr(kable_input, "format")
34 if (!kable_format %in% c("html", "latex")) {
35 message("Currently generic markdown table using pandoc is not supported.")
36 return(kable_input)
37 }
38 if (kable_format == "html") {
Hao Zhu669bcd22017-08-19 14:53:40 -040039 return(row_spec_html(kable_input, row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -040040 color, background, align))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040041 }
42 if (kable_format == "latex") {
Hao Zhu669bcd22017-08-19 14:53:40 -040043 return(row_spec_latex(kable_input, row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -040044 color, background, align))
Hao Zhu79f1e2a2017-06-11 20:55:30 -040045 }
46}
47
Hao Zhu669bcd22017-08-19 14:53:40 -040048row_spec_html <- function(kable_input, row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -040049 color, background, align) {
Hao Zhu79f1e2a2017-06-11 20:55:30 -040050 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040051 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040052
Hao Zhu8d347c42017-10-10 13:11:19 -040053 if (!is.null(align)) {
54 if (align %in% c("l", "c", "r")) {
55 align <- switch(align, r = "right", c = "center", l = "left")
56 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040057 }
58
Hao Zhu8d347c42017-10-10 13:11:19 -040059 if (0 %in% row) {
60 kable_thead <- xml_tpart(kable_xml, "thead")
61 original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
62 for (theader_i in 1:length(xml_children(original_header_row))) {
63 target_header_cell <- xml_child(original_header_row, theader_i)
64 xml_cell_style(target_header_cell, bold, italic, monospace, color, background, align)
65 }
66 row <- row[row != 0]
67 }
68
69 if (length(row) != 0) {
70 kable_tbody <- xml_tpart(kable_xml, "tbody")
71
72 group_header_rows <- attr(kable_input, "group_header_rows")
73 if (!is.null(group_header_rows)) {
74 row <- positions_corrector(row, group_header_rows,
75 length(xml_children(kable_tbody)))
76 }
77
78 for (j in row) {
79 target_row <- xml_child(kable_tbody, j)
80 for (i in 1:length(xml_children(target_row))) {
81 target_cell <- xml_child(target_row, i)
82 xml_cell_style(target_cell, bold, italic, monospace, color, background, align)
Hao Zhu322de082017-09-11 19:25:29 -040083 }
Hao Zhu669bcd22017-08-19 14:53:40 -040084 }
Hao Zhu79f1e2a2017-06-11 20:55:30 -040085 }
Hao Zhu322de082017-09-11 19:25:29 -040086
Hao Zhuf2dfd142017-07-24 14:43:28 -040087 out <- as_kable_xml(kable_xml)
Hao Zhu79f1e2a2017-06-11 20:55:30 -040088 attributes(out) <- kable_attrs
89 return(out)
90}
91
Hao Zhu8d347c42017-10-10 13:11:19 -040092xml_cell_style <- function(x, bold, italic, monospace, color, background, align) {
93 if (bold) {
94 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
95 "font-weight: bold;")
96 }
97 if (italic) {
98 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
99 "font-style: italic;")
100 }
101 if (monospace) {
102 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
103 "font-family: monospace;")
104 }
105 if (!is.null(color)) {
106 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
107 "color: ", color, ";")
108 }
109 if (!is.null(background)) {
110 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
111 "background-color: ",
112 background, ";")
113 }
114 if (!is.null(align)) {
115 xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
116 "text-align: ", align, ";")
117 }
118 return(x)
119}
120
Hao Zhua73601b2017-08-19 15:31:51 -0400121row_spec_latex <- function(kable_input, row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -0400122 color, background, align) {
Hao Zhu73604282017-06-11 22:08:48 -0400123 table_info <- magic_mirror(kable_input)
Hao Zhu322de082017-09-11 19:25:29 -0400124 out <- enc2utf8(as.character(kable_input))
125 row <- row + 1
126 for (i in row) {
127 target_row <- table_info$contents[i]
128 new_row <- latex_new_row_builder(target_row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -0400129 color, background, align)
Hao Zhu322de082017-09-11 19:25:29 -0400130 out <- sub(target_row, new_row, out, perl = T)
131 }
132
133 out <- structure(out, format = "latex", class = "knitr_kable")
134 attr(out, "kable_meta") <- table_info
135 return(out)
136}
137
138latex_new_row_builder <- function(target_row, bold, italic, monospace,
Hao Zhu8d347c42017-10-10 13:11:19 -0400139 color, background, align) {
Hao Zhu73604282017-06-11 22:08:48 -0400140 new_row <- latex_row_cells(target_row)
141 if (bold) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400142 new_row <- lapply(new_row, function(x) {
143 paste0("\\\\bfseries{", x, "}")
144 })
Hao Zhu73604282017-06-11 22:08:48 -0400145 }
146 if (italic) {
Hao Zhu35ecd272017-06-11 22:50:42 -0400147 new_row <- lapply(new_row, function(x) {
148 paste0("\\\\em{", x, "}")
149 })
Hao Zhu73604282017-06-11 22:08:48 -0400150 }
Hao Zhu3e53e602017-07-26 12:40:57 -0400151 if (monospace) {
152 new_row <- lapply(new_row, function(x) {
153 paste0("\\\\ttfamily{", x, "}")
154 })
155 }
Hao Zhua73601b2017-08-19 15:31:51 -0400156
157 if (!is.null(color)) {
158 new_row <- lapply(new_row, function(x) {
159 paste0("\\\\textcolor{", color, "}{", x, "}")
160 })
161 }
Hao Zhu8d347c42017-10-10 13:11:19 -0400162
163 if (!is.null(align)) {
164 new_row <- lapply(new_row, function(x) {
165 paste0("\\\\multicolumn{1}{", align, "}{", x, "}")
166 })
167 }
Hao Zhu35ecd272017-06-11 22:50:42 -0400168 new_row <- paste(unlist(new_row), collapse = " & ")
Hao Zhu73604282017-06-11 22:08:48 -0400169
Hao Zhua73601b2017-08-19 15:31:51 -0400170 if (!is.null(background)) {
171 new_row <- paste0("\\\\rowcolor{", background, "} ", new_row)
172 }
173
Hao Zhu8d347c42017-10-10 13:11:19 -0400174
175
Hao Zhu322de082017-09-11 19:25:29 -0400176 return(new_row)
Hao Zhu73604282017-06-11 22:08:48 -0400177}