blob: bc8ef7e64cb03febb34ec913fcc47203ec3e41e2 [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
4#' its look. Right now it supports the following three properties: column width,
5#' bold text and italic text.
6#'
7#' @param kable_input Output of `knitr::kable()` with `format` specified
8#' @param column A numeric value indicating which column to be selected
9#' @param width A character string telling HTML & LaTeX how wide the column
10#' needs to be, e.g. "10cm", "3in" or "30em".
11#' @param bold A T/F value to control whether the text of the selected column
12#' need to be bolded.
13#' @param italic A T/F value to control whether the text of the selected column
14#' need to be emphasized.
15#' @export
16column_spec <- function(kable_input, column, width, bold = F, italic = F) {
17 if (!is.numeric(column)) {
18 stop("column must be a numeric value")
19 }
20 kable_format <- attr(kable_input, "format")
21 if (!kable_format %in% c("html", "latex")) {
22 message("Currently generic markdown table using pandoc is not supported.")
23 return(kable_input)
24 }
25 if (kable_format == "html") {
26 return(column_spec_html(kable_input, column, width, bold, italic))
27 }
28 if (kable_format == "latex") {
29 return(column_spec_latex(kable_input, column, width, bold, italic))
30 }
31}
32
33column_spec_html <- function(kable_input, column, width, bold, italic) {
34 kable_attrs <- attributes(kable_input)
35 kable_xml <- read_xml(as.character(kable_input), options = "COMPACT")
36 kable_tbody <- xml_tpart(kable_xml, "tbody")
37
38 group_header_rows <- attr(kable_input, "group_header_rows")
39 all_contents_rows <- seq(1, length(kable_tbody))
40 if (!is.null(group_header_rows)) {
41 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
42 group_header_rows]
43 }
44
45 for (i in all_contents_rows) {
46 target_cell <- xml_child(xml_child(kable_tbody, i), column)
47 if (!is.null(width)) {
48 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
49 "width: ", width, "; ")
50 }
51 if (bold) {
52 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
53 "font-weight: bold;")
54 }
55 if (italic) {
56 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
57 "font-style: italic;")
58 }
59 }
60 out <- structure(as.character(kable_xml), format = "html",
61 class = "knitr_kable")
62 attributes(out) <- kable_attrs
63 return(out)
64}
65
66column_spec_latex <- function(kable_input, column, width, bold, italic) {
67 table_info <- magic_mirror(kable_input)
68 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
69 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
70
71 if (!is.null(width)) {
72 table_info$align_vector[column] <- paste0("p\\{", width, "\\}")
73 }
74
75 if (bold | italic) {
76 usepackage_latex("array")
77 latex_array_options <- c("\\\\bfseries", "\\\\em")[c(bold, italic)]
78 latex_array_options <- paste0(
79 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
80 )
81 table_info$align_vector[column] <- paste0(latex_array_options,
82 table_info$align_vector[column])
83 }
84
85 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
86
87 out <- sub(kable_align_old, kable_align_new, as.character(kable_input),
88 perl = T)
89 out <- structure(out, format = "latex", class = "knitr_kable")
90 attr(out, "original_kable_meta") <- table_info
91 return(out)
92}