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 | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 149 | table_info$align_vector[column] <- latex_column_align_builder( |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 150 | table_info$align_vector_origin[column], width, bold, italic, monospace, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 151 | color, background, border_left, border_right) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 152 | |
| 153 | kable_align_new <- paste(table_info$align_vector, collapse = align_collapse) |
| 154 | |
Hao Zhu | d2c0f73 | 2017-08-26 10:40:14 -0400 | [diff] [blame] | 155 | out <- sub(kable_align_old, kable_align_new, |
| 156 | enc2utf8(as.character(kable_input)), |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 157 | perl = T) |
| 158 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 159 | if (!is.null(width)) { |
| 160 | if (is.null(table_info$column_width)) { |
| 161 | table_info$column_width <- list() |
| 162 | } |
| 163 | table_info$column_width[[paste0("column_", column)]] <- width |
| 164 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 165 | attr(out, "kable_meta") <- table_info |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 166 | return(out) |
| 167 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 168 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 169 | latex_column_align_builder <- function(x, width, bold, italic, monospace, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 170 | color, background, |
| 171 | border_left, border_right) { |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 172 | extra_align <- "" |
| 173 | if (!is.null(width)) { |
| 174 | extra_align <- switch(x, |
Hao Zhu | bf5bfe2 | 2017-06-21 14:37:41 -0400 | [diff] [blame] | 175 | "l" = "\\\\raggedright\\\\arraybackslash", |
| 176 | "c" = "\\\\centering\\\\arraybackslash", |
| 177 | "r" = "\\\\raggedleft\\\\arraybackslash") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 178 | x <- paste0("p\\{", width, "\\}") |
| 179 | } |
| 180 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 181 | if (!is.null(color)) { |
| 182 | color <- sprintf("\\\\color{%s}", color) |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 185 | if (!is.null(color)) { |
| 186 | background <- sprintf("\\\\columncolor{%s}", background) |
| 187 | } |
| 188 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 189 | latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[ |
| 190 | c(bold, italic, monospace)] |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 191 | latex_array_options <- c(latex_array_options, extra_align, |
| 192 | color, background) |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 193 | latex_array_options <- paste0( |
| 194 | "\\>\\{", paste(latex_array_options, collapse = ""), "\\}" |
| 195 | ) |
| 196 | x <- paste0(latex_array_options, x) |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 197 | if (border_left) { |
| 198 | x <- paste0("|", x) |
| 199 | } |
| 200 | if (border_right) { |
| 201 | x <- paste0(x, "|") |
| 202 | } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 203 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 204 | return(x) |
| 205 | } |