Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 1 | #' 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 |
Hao Zhu | bf5bfe2 | 2017-06-21 14:37:41 -0400 | [diff] [blame] | 8 | #' @param column A numeric value indicating which column to be selected. When |
| 9 | #' you do the counting, ignore the extra header columns you added through |
| 10 | #' add_header_left. |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 11 | #' @param width A character string telling HTML & LaTeX how wide the column |
| 12 | #' needs to be, e.g. "10cm", "3in" or "30em". |
| 13 | #' @param bold A T/F value to control whether the text of the selected column |
| 14 | #' need to be bolded. |
| 15 | #' @param italic A T/F value to control whether the text of the selected column |
| 16 | #' need to be emphasized. |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 17 | #' @param monospace A T/F value to control whether the text of the selected column |
| 18 | #' need to be monospaced (verbatim) |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 19 | #' @param color A character string for column text color. |
| 20 | #' @param background A character string for column background color. |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 21 | #' |
| 22 | #' @examples x <- knitr::kable(head(mtcars), "html") |
| 23 | #' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE) |
| 24 | #' |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 25 | #' @export |
Hao Zhu | f13a35e | 2017-05-24 00:55:43 -0400 | [diff] [blame] | 26 | column_spec <- function(kable_input, column, |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 27 | width = NULL, bold = FALSE, italic = FALSE, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 28 | monospace = FALSE, color = NULL, background = NULL, |
| 29 | border_left = FALSE, border_right = FALSE) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 30 | if (!is.numeric(column)) { |
| 31 | stop("column must be a numeric value") |
| 32 | } |
| 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 Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 39 | return(column_spec_html(kable_input, column, width, |
| 40 | bold, italic, monospace, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 41 | color, background, |
| 42 | border_left, border_right)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 43 | } |
| 44 | if (kable_format == "latex") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 45 | return(column_spec_latex(kable_input, column, width, |
| 46 | bold, italic, monospace, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 47 | color, background, |
| 48 | border_left, border_right)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 52 | column_spec_html <- function(kable_input, column, width, |
| 53 | bold, italic, monospace, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 54 | color, background, |
| 55 | border_left, border_right) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 56 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 57 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 58 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 59 | |
| 60 | group_header_rows <- attr(kable_input, "group_header_rows") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 61 | if (is.null(kable_attrs$column_adjust)) { |
| 62 | all_contents_rows <- seq(1, length(xml_children(kable_tbody))) |
| 63 | all_contents_array <- rep(column, length(all_contents_rows)) |
| 64 | } else { |
| 65 | column <- column + kable_attrs$column_adjust$count |
| 66 | all_contents_array <- colSums(kable_attrs$column_adjust$matrix[1:column, ]) |
| 67 | all_contents_rows <- which(all_contents_array != 0 & |
| 68 | kable_attrs$column_adjust$matrix[column, ]) |
| 69 | } |
| 70 | |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 71 | if (!is.null(group_header_rows)) { |
| 72 | all_contents_rows <- all_contents_rows[!all_contents_rows %in% |
| 73 | group_header_rows] |
| 74 | } |
| 75 | |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 76 | # Border css |
| 77 | border_l_css <- "1px solid" |
| 78 | border_r_css <- "1px solid" |
| 79 | if (is.character(border_left)) { |
| 80 | border_l_css <- border_left |
| 81 | border_left <- T |
| 82 | } |
| 83 | if (is.character(border_right)) { |
| 84 | border_r_css <- border_right |
| 85 | border_right <- T |
| 86 | } |
| 87 | |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 88 | for (i in all_contents_rows) { |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 89 | target_cell <- xml_child(xml_child(kable_tbody, i), all_contents_array[i]) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 90 | if (!is.null(width)) { |
| 91 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 92 | "width: ", width, "; ") |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 93 | } |
| 94 | if (bold) { |
| 95 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 96 | "font-weight: bold;") |
| 97 | } |
| 98 | if (italic) { |
| 99 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 100 | "font-style: italic;") |
| 101 | } |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 102 | if (monospace) { |
| 103 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 104 | "font-family: monospace;") |
| 105 | } |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 106 | if (!is.null(color)) { |
| 107 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 108 | "color: ", color, ";") |
| 109 | } |
| 110 | if (!is.null(background)) { |
| 111 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 112 | "background-color: ", |
| 113 | background, ";") |
| 114 | } |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 115 | if (border_left) { |
| 116 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 117 | "border-left:", border_l_css, ";") |
| 118 | } |
| 119 | if (border_right) { |
| 120 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 121 | "border-right:", border_r_css, ";") |
| 122 | } |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 123 | } |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 124 | out <- as_kable_xml(kable_xml) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 125 | attributes(out) <- kable_attrs |
| 126 | return(out) |
| 127 | } |
| 128 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 129 | column_spec_latex <- function(kable_input, column, width, |
| 130 | bold, italic, monospace, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 131 | color, background, |
| 132 | border_left, border_right) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 133 | table_info <- magic_mirror(kable_input) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 134 | if (!is.null(table_info$collapse_rows)) { |
| 135 | message("Usually it is recommended to use column_spec before collapse_rows,", |
| 136 | " especially in LaTeX, to get a desired result. ") |
| 137 | } |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 138 | align_collapse <- ifelse(table_info$booktabs, "", "\\|") |
| 139 | kable_align_old <- paste(table_info$align_vector, collapse = align_collapse) |
| 140 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 141 | table_info$align_vector[column] <- latex_column_align_builder( |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 142 | table_info$align_vector_origin[column], width, bold, italic, monospace, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 143 | color, background, border_left, border_right) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 144 | |
| 145 | kable_align_new <- paste(table_info$align_vector, collapse = align_collapse) |
| 146 | |
Hao Zhu | d2c0f73 | 2017-08-26 10:40:14 -0400 | [diff] [blame] | 147 | out <- sub(kable_align_old, kable_align_new, |
| 148 | enc2utf8(as.character(kable_input)), |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 149 | perl = T) |
| 150 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 151 | if (!is.null(width)) { |
| 152 | if (is.null(table_info$column_width)) { |
| 153 | table_info$column_width <- list() |
| 154 | } |
| 155 | table_info$column_width[[paste0("column_", column)]] <- width |
| 156 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 157 | attr(out, "kable_meta") <- table_info |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 158 | return(out) |
| 159 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 160 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 161 | latex_column_align_builder <- function(x, width, bold, italic, monospace, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 162 | color, background, |
| 163 | border_left, border_right) { |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 164 | extra_align <- "" |
| 165 | if (!is.null(width)) { |
| 166 | extra_align <- switch(x, |
Hao Zhu | bf5bfe2 | 2017-06-21 14:37:41 -0400 | [diff] [blame] | 167 | "l" = "\\\\raggedright\\\\arraybackslash", |
| 168 | "c" = "\\\\centering\\\\arraybackslash", |
| 169 | "r" = "\\\\raggedleft\\\\arraybackslash") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 170 | x <- paste0("p\\{", width, "\\}") |
| 171 | } |
| 172 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 173 | if (!is.null(color)) { |
| 174 | color <- sprintf("\\\\color{%s}", color) |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 177 | if (!is.null(color)) { |
| 178 | background <- sprintf("\\\\columncolor{%s}", background) |
| 179 | } |
| 180 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 181 | latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[ |
| 182 | c(bold, italic, monospace)] |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 183 | latex_array_options <- c(latex_array_options, extra_align, |
| 184 | color, background) |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 185 | latex_array_options <- paste0( |
| 186 | "\\>\\{", paste(latex_array_options, collapse = ""), "\\}" |
| 187 | ) |
| 188 | x <- paste0(latex_array_options, x) |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 189 | if (border_left) { |
| 190 | x <- paste0("|", x) |
| 191 | } |
| 192 | if (border_right) { |
| 193 | x <- paste0(x, "|") |
| 194 | } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 195 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 196 | return(x) |
| 197 | } |