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