blob: 77f0c69ceccdb3e2c9bca54fcad3f519450bff79 [file] [log] [blame]
Hao Zhubff01912017-05-23 18:05:00 -04001#' Specify the look of the selected column
2#'
3#' @description This function allows users to select a column and then specify
Hao Zhue7c8f702017-10-10 13:22:59 -04004#' its look.
Hao Zhubff01912017-05-23 18:05:00 -04005#'
6#' @param kable_input Output of `knitr::kable()` with `format` specified
Hao Zhu322de082017-09-11 19:25:29 -04007#' @param column A numeric value or vector indicating which column(s) to be selected.
Hao Zhubff01912017-05-23 18:05:00 -04008#' @param width A character string telling HTML & LaTeX how wide the column
9#' needs to be, e.g. "10cm", "3in" or "30em".
Hao Zhu33b865f2020-08-18 02:10:43 -040010#' @param bold T/F value or vector to control whether the text of the selected
11#' column need to be bolded.
12#' @param italic T/F value or vector to control whether the text of the
13#' selected column need to be emphasized.
14#' @param monospace T/F value or vector to control whether the text of the
15#' selected column need to be monospaced (verbatim)
16#' @param underline T/F value or vector to control whether the text of the
17#' selected row need to be underlined
18#' @param strikeout T/F value or vector to control whether the text of the
19#' selected row need to be striked out.
20#' @param color A character string or vector for column text color. Here please
21#' pay attention to the differences in color codes between HTML and LaTeX.
22#' @param background A character string or vector for column background color. Here please
Hao Zhu53e240f2017-09-04 20:04:29 -040023#' pay attention to the differences in color codes between HTML and LaTeX.
24#' @param border_left A logical variable indicating whether there should be a
25#' border line on the left of the selected column. In HTML, you can also pass
26#' in a character string for the CSS of the border line
27#' @param border_right A logical variable indicating whether there should be a
28#' border line on the right of the selected column. In HTML, you can also pass
29#' in a character string for the CSS of the border line
Hao Zhu6107f372018-05-21 00:23:26 -040030#' @param width_min Only for HTML table. Normal column width will automatically
31#' collapse when the window cannot hold enough contents. With this `width_min`,
Hao Zhub1caa272018-04-14 14:19:46 -040032#' you can set up a column with a width that won't collapse even when the
33#' window is not wide enough.
Hao Zhu6107f372018-05-21 00:23:26 -040034#' @param width_max Only for HTML table. `width_max` defines the maximum width
Hao Zhub1caa272018-04-14 14:19:46 -040035#' of table columns.
Hao Zhu33b865f2020-08-18 02:10:43 -040036#' @param extra_css A vector of extra css text to be passed into the cells of
37#' the column.
Hao Zhu907ddfe2018-04-23 15:19:09 -040038#' @param include_thead T/F. A HTML only feature to contoll whether the
39#' header row will be manipulated. Default is `FALSE`.
Duncan Murdoch8bc96222019-04-29 12:46:39 -040040#' @param latex_column_spec Only for LaTeX tables. Code to replace the column
41#' specification. If not `NULL`, will override all other arguments.
Hao Zhu2b739ac2020-08-15 01:38:51 -040042#' @param latex_valign vertical alignment. Only works when you specified column
43#' width. Choose among `p`, `m`, `b`.
Hao Zhu33b865f2020-08-18 02:10:43 -040044#' @param link A vector of strings for url links.
45#' @param new_tab T/F for whether to open up the new link in new tab
46#' @param tooltip A vector of strings to be displayed as tooltip.
47#' Obviously, this feature is only available in HTML. Read the package
48#' vignette to see how to use bootstrap tooltip css to improve the loading
49#' speed and look.
50#' @param popover Similar with tooltip but can hold more contents. The best way
51#' to build a popover is through `spec_popover()`. If you only provide a text
52#' string, it will be used as content. Note that You have to enable this
53#' bootstrap module manually. Read the package vignette to see how.
Hao Zhu5fe235c2020-08-26 00:26:49 -040054#' @param image Vector of image paths.
Duncan Murdoch8bc96222019-04-29 12:46:39 -040055#'
56#' @details Use `latex_column_spec` in a LaTeX table to change or
57#' customize the column specification. Because of the way it is handled
58#' internally, any backslashes must be escaped.
Hao Zhu78e61222017-05-24 20:53:35 -040059#'
Hao Zhu9399dcc2020-08-26 17:27:38 -040060#' @examples
61#' \dontrun{
62#' x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040063#' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
Duncan Murdoch8bc96222019-04-29 12:46:39 -040064#' x <- knitr::kable(head(mtcars), "latex", booktabs = TRUE)
65#' column_spec(x, 1, latex_column_spec = ">{\\\\color{red}}c")
Hao Zhu9399dcc2020-08-26 17:27:38 -040066#' }
Hao Zhubff01912017-05-23 18:05:00 -040067#' @export
Hao Zhu322de082017-09-11 19:25:29 -040068column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040069 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050070 monospace = FALSE, underline = FALSE, strikeout = FALSE,
71 color = NULL, background = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050072 border_left = FALSE, border_right = FALSE,
Hao Zhub1caa272018-04-14 14:19:46 -040073 width_min = NULL, width_max = NULL,
Duncan Murdoch8bc96222019-04-29 12:46:39 -040074 extra_css = NULL, include_thead = FALSE,
Hao Zhu33b865f2020-08-18 02:10:43 -040075 latex_column_spec = NULL, latex_valign = 'p',
76 link = NULL, new_tab = TRUE,
Hao Zhu5fe235c2020-08-26 00:26:49 -040077 tooltip = NULL, popover = NULL, image = NULL) {
Hao Zhu322de082017-09-11 19:25:29 -040078 if (!is.numeric(column)) {
79 stop("column must be numeric. ")
Hao Zhubff01912017-05-23 18:05:00 -040080 }
81 kable_format <- attr(kable_input, "format")
82 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050083 warning("Please specify format in kable. kableExtra can customize either ",
84 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
85 "for details.")
Hao Zhubff01912017-05-23 18:05:00 -040086 return(kable_input)
87 }
88 if (kable_format == "html") {
Hao Zhu322de082017-09-11 19:25:29 -040089 return(column_spec_html(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040090 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -050091 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -040092 color, background,
Hao Zhub1caa272018-04-14 14:19:46 -040093 border_left, border_right,
94 width_min, width_max,
Hao Zhu33b865f2020-08-18 02:10:43 -040095 extra_css, include_thead,
Hao Zhu5fe235c2020-08-26 00:26:49 -040096 link, new_tab, tooltip, popover, image))
Hao Zhubff01912017-05-23 18:05:00 -040097 }
98 if (kable_format == "latex") {
Hao Zhu322de082017-09-11 19:25:29 -040099 return(column_spec_latex(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -0400100 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500101 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400102 color, background,
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400103 border_left, border_right,
Hao Zhu33b865f2020-08-18 02:10:43 -0400104 latex_column_spec, latex_valign, include_thead,
Hao Zhu5fe235c2020-08-26 00:26:49 -0400105 link, image))
Hao Zhubff01912017-05-23 18:05:00 -0400106 }
107}
108
Hao Zhu322de082017-09-11 19:25:29 -0400109column_spec_html <- function(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -0400110 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500111 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400112 color, background,
Hao Zhub1caa272018-04-14 14:19:46 -0400113 border_left, border_right,
114 width_min, width_max,
Hao Zhu33b865f2020-08-18 02:10:43 -0400115 extra_css, include_thead,
Hao Zhu5fe235c2020-08-26 00:26:49 -0400116 link, new_tab, tooltip, popover, image) {
Hao Zhubff01912017-05-23 18:05:00 -0400117 kable_attrs <- attributes(kable_input)
Hao Zhud57df632020-09-04 14:37:36 -0400118 kable_xml <- kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -0400119 kable_tbody <- xml_tpart(kable_xml, "tbody")
120
121 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -0400122 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -0400123
Hao Zhubff01912017-05-23 18:05:00 -0400124 if (!is.null(group_header_rows)) {
125 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
126 group_header_rows]
127 }
128
Hao Zhuec7ab922017-08-19 22:56:44 -0400129 # Border css
130 border_l_css <- "1px solid"
131 border_r_css <- "1px solid"
132 if (is.character(border_left)) {
133 border_l_css <- border_left
134 border_left <- T
135 }
136 if (is.character(border_right)) {
137 border_r_css <- border_right
138 border_right <- T
139 }
140
Hao Zhu907ddfe2018-04-23 15:19:09 -0400141 if (include_thead) {
142 kable_thead <- xml_tpart(kable_xml, "thead")
143 nrow_thead <- length(xml_children(kable_thead))
144 for (j in column) {
145 target_cell <- xml_child(xml_child(kable_thead, nrow_thead), j)
146 column_spec_html_cell(
147 target_cell, width, width_min, width_max,
Hao Zhu33b865f2020-08-18 02:10:43 -0400148 bold[1], italic[1], monospace[1], underline[1], strikeout[1],
149 color[1], background[1], border_left, border_right,
Hao Zhu907ddfe2018-04-23 15:19:09 -0400150 border_l_css, border_r_css,
Hao Zhu33b865f2020-08-18 02:10:43 -0400151 extra_css[1], link[1], new_tab[1], tooltip[1], popover[1]
152 )
153 }
154 }
155
156 nrows <- length(all_contents_rows)
157 off <- 0
158
159 bold <- ensure_len_html(bold, nrows, "bold")
160 italic <- ensure_len_html(italic, nrows, "italic")
161 monospace <- ensure_len_html(monospace, nrows, "monospace")
162 underline <- ensure_len_html(underline, nrows, "underline")
163 strikeout <- ensure_len_html(strikeout, nrows, "strikeout")
164 color <- ensure_len_html(color, nrows, "color")
165 background <- ensure_len_html(background, nrows,"background")
166 link <- ensure_len_html(link, nrows, "link")
167 new_tab <- ensure_len_html(new_tab, nrows, "new_tab")
168 tooltip <- ensure_len_html(tooltip, nrows, "tooltip")
169 popover <- ensure_len_html(popover, nrows, "popover")
Hao Zhu5fe235c2020-08-26 00:26:49 -0400170 image <- ensure_len_html(image, nrows, "image")
Hao Zhu33b865f2020-08-18 02:10:43 -0400171
Hao Zhud57df632020-09-04 14:37:36 -0400172 for (i in seq(length(all_contents_rows))) {
Hao Zhu33b865f2020-08-18 02:10:43 -0400173 for (j in column) {
Hao Zhud57df632020-09-04 14:37:36 -0400174 target_cell <- xml_child(xml_child(kable_tbody, all_contents_rows[i]), j)
Hao Zhu33b865f2020-08-18 02:10:43 -0400175 column_spec_html_cell(
176 target_cell, width, width_min, width_max,
177 bold[i], italic[i], monospace[i], underline[i], strikeout[i],
178 color[i], background[i], border_left, border_right,
179 border_l_css, border_r_css,
180 extra_css,
Hao Zhu5fe235c2020-08-26 00:26:49 -0400181 link[i], new_tab[i], tooltip[i], popover[i], image[i]
Hao Zhu907ddfe2018-04-23 15:19:09 -0400182 )
183 }
184 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400185
Hao Zhuf2dfd142017-07-24 14:43:28 -0400186 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400187 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500188 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhubff01912017-05-23 18:05:00 -0400189 return(out)
190}
191
Hao Zhu33b865f2020-08-18 02:10:43 -0400192ensure_len_html <- function(x, l, name) {
193 if (is.null(x)) return(NULL)
194 if (length(x) == 1) return(rep(x, l))
195 if (length(x) == l) return(x)
196 warning("The number of provided values in ", name, " does not equal to the ",
197 "number of rows. ")
198 return(rep(x, ceiling(l / length(x)))[seq(1, l)])
199}
200
Hao Zhu907ddfe2018-04-23 15:19:09 -0400201column_spec_html_cell <- function(target_cell, width, width_min, width_max,
202 bold, italic, monospace, underline, strikeout,
203 color, background,
204 border_left, border_right,
205 border_l_css, border_r_css,
Hao Zhu33b865f2020-08-18 02:10:43 -0400206 extra_css,
Hao Zhu5fe235c2020-08-26 00:26:49 -0400207 link, new_tab, tooltip, popover, image) {
Hao Zhu517453c2018-05-13 13:07:18 -0400208 if (is.na(xml_attr(target_cell, "style"))) {
209 xml_attr(target_cell, "style") <- ""
210 }
Hao Zhu907ddfe2018-04-23 15:19:09 -0400211 if (!is.null(width)) {
212 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
213 "width: ", width, "; ")
214 }
215 if (!is.null(width_min)) {
216 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
217 "min-width: ", width_min, "; ")
218 }
219 if (!is.null(width_max)) {
220 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
221 "max-width: ", width_max, "; ")
222 }
223 if (bold) {
224 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
225 "font-weight: bold;")
226 }
227 if (italic) {
228 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
229 "font-style: italic;")
230 }
231 if (monospace) {
232 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
233 "font-family: monospace;")
234 }
235 if (underline) {
236 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
237 "text-decoration: underline;")
238 }
239 if (strikeout) {
240 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
241 "text-decoration: line-through;")
242 }
243 if (!is.null(color)) {
244 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhu72917f92019-03-15 18:41:42 -0400245 "color: ", html_color(color),
246 " !important;")
Hao Zhu907ddfe2018-04-23 15:19:09 -0400247 }
248 if (!is.null(background)) {
249 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
250 "background-color: ",
Hao Zhu72917f92019-03-15 18:41:42 -0400251 html_color(background),
252 " !important;")
Hao Zhu907ddfe2018-04-23 15:19:09 -0400253 }
254 if (border_left) {
255 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
256 "border-left:", border_l_css, ";")
257 }
258 if (border_right) {
259 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
260 "border-right:", border_r_css, ";")
261 }
262 if (!is.null(extra_css)) {
263 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
264 extra_css)
265 }
Hao Zhu33b865f2020-08-18 02:10:43 -0400266
Hao Zhu5fe235c2020-08-26 00:26:49 -0400267 if (!is.null(image)) {
268 image <- image[[1]]
269 if (class(image) == "kableExtraInlinePlots") {
270 if (!is.null(image$svg_text)) {
271 xml_add_child(target_cell, xml2::read_xml(image$svg_text))
272 } else {
273 img_text <- paste0('<img src="', image$path, '" width="',
274 image$width / image$res * 96, '" height="',
275 image$height / image$res * 96,
276 '"></img>')
277 xml_add_child(target_cell, xml2::read_html(img_text))
278 }
279 } else {
280 img_text <- paste0('<img src="', image, '"></img>')
281 xml_add_child(target_cell, xml2::read_html(img_text))
282 }
283 }
284
Hao Zhu33b865f2020-08-18 02:10:43 -0400285 # favor popover over tooltip
286 if (!is.null(popover)) {
287 if (class(popover) != "ke_popover") popover <- spec_popover(popover)
288 popover_list <- attr(popover, 'list')
289 for (p in names(popover_list)) {
290 xml_attr(target_cell, p) <- popover_list[p]
291 }
292 } else if (!is.null(tooltip)) {
293 if (class(tooltip) != "ke_tooltip") tooltip <- spec_tooltip(tooltip)
294 tooltip_list <- attr(tooltip, 'list')
295 for (t in names(tooltip_list)) {
296 xml_attr(target_cell, t) <- tooltip_list[t]
297 }
298 }
Hao Zhu33b865f2020-08-18 02:10:43 -0400299
300 if (!is.null(link)) {
301 href_node <- xml2::read_xml(paste0(
302 '<a href="', link, '">', xml_text(target_cell), '</a>'
303 ))
304 if (!is.null(color)) {
305 xml_attr(href_node, "style") <- paste0("color: ", html_color(color),
306 " !important;")
307 }
308 xml_add_child(target_cell, href_node)
309 xml_text(target_cell) <- ""
310 }
Hao Zhu907ddfe2018-04-23 15:19:09 -0400311}
312
Hao Zhu322de082017-09-11 19:25:29 -0400313column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400314 bold, italic, monospace,
Hao Zhuef0c8302018-01-12 13:30:20 -0500315 underline, strikeout,
Hao Zhuec7ab922017-08-19 22:56:44 -0400316 color, background,
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400317 border_left, border_right,
Hao Zhu33b865f2020-08-18 02:10:43 -0400318 latex_column_spec, latex_valign, include_thead,
Hao Zhu5fe235c2020-08-26 00:26:49 -0400319 link, image) {
Hao Zhubff01912017-05-23 18:05:00 -0400320 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000321 if (!is.null(table_info$collapse_rows)) {
322 message("Usually it is recommended to use column_spec before collapse_rows,",
323 " especially in LaTeX, to get a desired result. ")
324 }
Hao Zhu67764aa2019-03-15 11:57:50 -0400325 align_collapse <- ifelse(table_info$booktabs | !is.null(table_info$xtable),
326 "", "\\|")
Hao Zhubff01912017-05-23 18:05:00 -0400327 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
328
Hao Zhu322de082017-09-11 19:25:29 -0400329 table_info$align_vector[column] <- unlist(lapply(
330 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400331 function(x) {
332 latex_column_align_builder(
Hao Zhu33b865f2020-08-18 02:10:43 -0400333 x, width, border_left, border_right, latex_column_spec, latex_valign)
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400334 }
335 ))
Hao Zhubff01912017-05-23 18:05:00 -0400336
337 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
338
Duncan Murdoch6eb29502018-12-16 20:21:00 -0500339 out <- sub(paste0("\\{", kable_align_old, "\\}"),
340 paste0("\\{", kable_align_new, "\\}"),
Hao Zhu3fc0e882018-04-03 16:06:41 -0400341 solve_enc(kable_input),
Hao Zhubff01912017-05-23 18:05:00 -0400342 perl = T)
Hao Zhuae80df42018-04-12 15:45:11 -0400343
344 if (!is.null(width)) {
345 fix_newline <- replace_makecell_with_newline(out, table_info, column)
346 out <- fix_newline[[1]]
347 table_info <- fix_newline[[2]]
348 }
349
Hao Zhu33b865f2020-08-18 02:10:43 -0400350 if (table_info$duplicated_rows) {
351 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
352 out <- dup_fx_out[[1]]
353 table_info <- dup_fx_out[[2]]
354 }
355
356 nrows <- length(table_info$contents)
357 off <- table_info$position_offset
358
359 bold <- ensure_len_latex(bold, nrows, off, include_thead, FALSE, "bold")
360 italic <- ensure_len_latex(italic, nrows, off, include_thead, FALSE, "italic")
361 monospace <- ensure_len_latex(monospace, nrows, off, include_thead, FALSE,
362 "monospace")
363 underline <- ensure_len_latex(underline, nrows, off, include_thead, FALSE,
364 "underline")
365 strikeout <- ensure_len_latex(strikeout, nrows, off, include_thead, FALSE,
366 "strikeout")
367 color <- ensure_len_latex(color, nrows, off, include_thead, "black", "color")
368 background <- ensure_len_latex(background, nrows, off, include_thead, "white",
369 "background")
370 link <- ensure_len_latex(link, nrows, off, include_thead, "#", "link")
Hao Zhu5fe235c2020-08-26 00:26:49 -0400371 image <- ensure_len_latex(image, nrows, off, include_thead, "", "image")
Hao Zhu33b865f2020-08-18 02:10:43 -0400372
373 if (include_thead) {
374 rows <- seq(1, nrows)
375 } else {
376 rows <- seq(1 + off, nrows)
377 }
378
379 for (i in rows) {
380 target_row <- table_info$contents[i]
381 new_row <- latex_cell_builder(
382 target_row, column, table_info,
383 bold[i], italic[i], monospace[i], underline[i],
Hao Zhu5fe235c2020-08-26 00:26:49 -0400384 strikeout[i], color[i], background[i], link[i], image[i]
Hao Zhu33b865f2020-08-18 02:10:43 -0400385 # font_size, angle
386 )
387 temp_sub <- ifelse(i == 1 & (table_info$tabular == "longtable" |
388 !is.null(table_info$repeat_header_latex)),
389 gsub, sub)
390 out <- temp_sub(target_row, new_row, out, perl = T)
391 table_info$contents[i] <- new_row
392 }
393
Hao Zhubff01912017-05-23 18:05:00 -0400394 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000395 if (!is.null(width)) {
396 if (is.null(table_info$column_width)) {
397 table_info$column_width <- list()
398 }
Hao Zhu322de082017-09-11 19:25:29 -0400399 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400400 table_info$column_width[[paste0("column_", i)]] <- width
401 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000402 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400403 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400404 return(out)
405}
Hao Zhu32f43f72017-06-20 18:24:54 -0400406
Hao Zhu33b865f2020-08-18 02:10:43 -0400407ensure_len_latex <- function(x, l, off, include_thead, def, name) {
408 if (is.null(x)) return(NULL)
409 if (length(x) == 1) return(rep(x, l))
410 if (include_thead) {
411 if (length(x) == l) return(x)
412 warning("The number of provided values in ", name, " does not equal to the ",
413 "number of rows. ")
414 return(rep(x, ceiling(l / length(x)))[seq(1, l)])
415 } else {
416 l_ = l - off
417 if (length(x) == l_) return(c(def, x))
418 warning("The number of provided values in ", name, " does not equal to the ",
419 "number of rows. ")
420 return(c(def, rep(x, ceiling(l_ / length(x)))[seq(1, l_)]))
421 }
422}
423
424latex_column_align_builder <- function(x, width,
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400425 border_left, border_right,
Hao Zhu2b739ac2020-08-15 01:38:51 -0400426 latex_column_spec, latex_valign) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400427 extra_align <- ""
428 if (!is.null(width)) {
429 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400430 "l" = "\\\\raggedright\\\\arraybackslash",
431 "c" = "\\\\centering\\\\arraybackslash",
432 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu2b739ac2020-08-15 01:38:51 -0400433 x <- paste0(latex_valign, "\\{", width, "\\}")
Hao Zhu32f43f72017-06-20 18:24:54 -0400434 }
Hao Zhu33b865f2020-08-18 02:10:43 -0400435 # if (!is.null(color)) {
436 # color <- paste0("\\\\leavevmode\\\\color", latex_color(color))
437 # }
438 #
439 # if (!is.null(background)) {
440 # background <- paste0("\\\\columncolor", latex_color(background))
441 # }
442 #
443 # latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily",
444 # "\\\\underline", "\\\\sout")[
445 # c(bold, italic, monospace, underline, strikeout)]
446 # latex_array_options <- c(latex_array_options, extra_align,
447 # color, background)
Hao Zhud0f7c8a2020-08-20 01:17:23 -0400448 latex_array_options <- paste0("\\>\\{", extra_align, "\\}")
449 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400450 if (border_left) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500451 x <- paste0("\\|", x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400452 }
453 if (border_right) {
Hao Zhub49bddf2018-01-12 15:25:23 -0500454 x <- paste0(x, "\\|")
Hao Zhuec7ab922017-08-19 22:56:44 -0400455 }
Duncan Murdoch8bc96222019-04-29 12:46:39 -0400456 if (!is.null(latex_column_spec))
Hao Zhu33b865f2020-08-18 02:10:43 -0400457 x <- latex_column_spec
Hao Zhua73601b2017-08-19 15:31:51 -0400458
Hao Zhu32f43f72017-06-20 18:24:54 -0400459 return(x)
460}
Hao Zhuae80df42018-04-12 15:45:11 -0400461
462replace_makecell_with_newline <- function(kable_input, table_info, column) {
463 if (!str_detect(kable_input, "makecell")) return(list(kable_input, table_info))
464 contents_table <- data.frame(sapply(table_info$contents,
Hao Zhu33b865f2020-08-18 02:10:43 -0400465 function(x) {str_split(x, " \\& ")[[1]]}),
466 stringsAsFactors = F)
Hao Zhuae80df42018-04-12 15:45:11 -0400467 names(contents_table) <- paste0("x", 1:table_info$nrow)
468 rows_check_makecell <- str_detect(contents_table[column, ], "makecell")
469 if (sum(rows_check_makecell) == 0) return(list(kable_input, table_info))
470 rows_to_replace <- which(rows_check_makecell)
471
472 for (i in column) {
473 target_column <- contents_table[i, ]
474 for (j in which(str_detect(target_column, "\\\\\\\\makecell"))) {
475 contents_table[i, j] <- str_replace(
476 contents_table[i, j], "\\\\\\\\makecell\\\\\\[.\\\\\\]\\\\\\{", "")
477 contents_table[i, j] <- str_replace(
Hao Zhu9ac3e382018-04-12 18:56:32 -0400478 contents_table[i, j], "\\\\\\}$", "")
Hao Zhuae80df42018-04-12 15:45:11 -0400479 contents_table[i, j] <- str_replace_all(
480 contents_table[i, j], "\\\\\\\\\\\\\\\\", "\\\\\\\\newline "
481 )
482 }
483 }
484
485 new_contents <- unlist(lapply(contents_table, paste, collapse = " & "))
486 for (i in rows_to_replace) {
487 kable_input <- sub(table_info$contents[i], new_contents[i], kable_input,
488 perl = T)
489 table_info$contents[i] <- new_contents[i]
490 }
491
492 return(list(kable_input, table_info))
493}
Hao Zhu33b865f2020-08-18 02:10:43 -0400494
495latex_cell_builder <- function(target_row, column, table_info,
496 bold, italic, monospace,
497 underline, strikeout,
Hao Zhu5fe235c2020-08-26 00:26:49 -0400498 color, background, link, image
Hao Zhu33b865f2020-08-18 02:10:43 -0400499 # font_size, angle
500 ) {
501 new_row <- latex_row_cells(target_row)[[1]]
502 if (bold) {
503 new_row[column] <- paste0("\\\\textbf\\{", new_row[column], "\\}")
504 }
505 if (italic) {
506 new_row[column] <- paste0("\\\\em\\{", new_row[column], "\\}")
507 }
508 if (monospace) {
509 new_row[column] <- paste0("\\\\ttfamily\\{", new_row[column], "\\}")
510 }
511 if (underline) {
512 new_row[column] <- paste0("\\\\underline\\{", new_row[column], "\\}")
513 }
514 if (strikeout) {
515 new_row[column] <- paste0("\\\\sout\\{", new_row[column], "\\}")
516 }
517 if (!is.null(color)) {
Hao Zhuc79d8542020-08-18 03:19:21 -0400518 clean_columns <- unlist(lapply(new_row[column], clear_color_latex))
Hao Zhu33b865f2020-08-18 02:10:43 -0400519 new_row[column] <- paste0("\\\\textcolor", latex_color(color), "\\{",
Hao Zhuc79d8542020-08-18 03:19:21 -0400520 clean_columns, "\\}")
Hao Zhu33b865f2020-08-18 02:10:43 -0400521 }
522 # if (!is.null(font_size)) {
523 # new_row[column] <- paste0("\\\\begingroup\\\\fontsize\\{", font_size, "\\}\\{",
524 # as.numeric(font_size) + 2,
525 # "\\}\\\\selectfont ", new_row[column], "\\\\endgroup")
526 # }
527 # if (!is.null(angle)) {
528 # new_row[column] <- paste0("\\\\rotatebox\\{", angle, "\\}\\{",
529 # new_row[column], "\\}")
530 # }
531 if (!is.null(background)) {
Hao Zhuc79d8542020-08-18 03:19:21 -0400532 clean_columns <- unlist(lapply(new_row[column], clear_color_latex, TRUE))
Hao Zhu33b865f2020-08-18 02:10:43 -0400533 new_row[column] <- paste0("\\\\cellcolor", latex_color(background), "\\{",
Hao Zhuc79d8542020-08-18 03:19:21 -0400534 clean_columns, "\\}")
Hao Zhu33b865f2020-08-18 02:10:43 -0400535 }
536
537 if (!is.null(link)) {
538 new_row[column] <- paste0("\\\\href\\{", escape_latex(link), "\\}\\{",
539 new_row[column], "\\}")
540 }
541
Hao Zhu5fe235c2020-08-26 00:26:49 -0400542 if (!is.null(image)) {
543 image <- image[[1]]
544 if (class(image) == "kableExtraInlinePlots") {
545 new_row[column] <- paste0(
546 new_row[column],
547 '\\\\includegraphics\\[width=',
548 # '\\\\raisebox\\{-\\\\totalheight\\}\\{\\\\includegraphics\\[width=',
549 round(image$width / image$res, 2), 'in, height=',
550 round(image$height / image$res, 2), 'in\\]\\{',
551 image$path,
552 '\\}'
553 # '\\}\\}'
554 )
555 } else {
556 if (!is.null(image) && !is.na(image) && image != "") {
557 new_row[column] <- paste0(
558 new_row[column],
559 '\\\\includegraphics\\{',
560 image, '\\}'
561 )
562 }
563 }
564 }
565
Hao Zhu33b865f2020-08-18 02:10:43 -0400566 new_row <- paste(new_row, collapse = " & ")
567
568 return(new_row)
569}