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 |
Hao Zhu | e7c8f70 | 2017-10-10 13:22:59 -0400 | [diff] [blame] | 4 | #' its look. |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 5 | #' |
| 6 | #' @param kable_input Output of `knitr::kable()` with `format` specified |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 7 | #' @param column A numeric value or vector indicating which column(s) to be selected. |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 8 | #' @param width A character string telling HTML & LaTeX how wide the column |
| 9 | #' needs to be, e.g. "10cm", "3in" or "30em". |
| 10 | #' @param bold A T/F value to control whether the text of the selected column |
| 11 | #' need to be bolded. |
| 12 | #' @param italic A T/F value to control whether the text of the selected column |
| 13 | #' need to be emphasized. |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 14 | #' @param monospace A T/F value to control whether the text of the selected column |
| 15 | #' need to be monospaced (verbatim) |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 16 | #' @param underline A T/F value to control whether the text of the selected row |
| 17 | #' need to be underlined |
| 18 | #' @param strikeout A T/F value to control whether the text of the selected row |
| 19 | #' need to be stricked out. |
Hao Zhu | 53e240f | 2017-09-04 20:04:29 -0400 | [diff] [blame] | 20 | #' @param color A character string for column text color. Here please pay |
| 21 | #' attention to the differences in color codes between HTML and LaTeX. |
| 22 | #' @param background A character string for column background color. Here please |
| 23 | #' 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 Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 30 | #' @param extra_css Extra css text to be passed into the cells of the row. Note |
| 31 | #' that it's not for the whole column but to each individual cells |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 32 | #' |
| 33 | #' @examples x <- knitr::kable(head(mtcars), "html") |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 34 | #' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE) |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 35 | #' |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 36 | #' @export |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 37 | column_spec <- function(kable_input, column, |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 38 | width = NULL, bold = FALSE, italic = FALSE, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 39 | monospace = FALSE, underline = FALSE, strikeout = FALSE, |
| 40 | color = NULL, background = NULL, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 41 | border_left = FALSE, border_right = FALSE, |
| 42 | extra_css = NULL) { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 43 | if (!is.numeric(column)) { |
| 44 | stop("column must be numeric. ") |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 45 | } |
| 46 | kable_format <- attr(kable_input, "format") |
| 47 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 48 | warning("Please specify format in kable. kableExtra can customize either ", |
| 49 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 50 | "for details.") |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 51 | return(kable_input) |
| 52 | } |
| 53 | if (kable_format == "html") { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 54 | return(column_spec_html(kable_input, column, width, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 55 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 56 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 57 | color, background, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 58 | border_left, border_right, extra_css)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 59 | } |
| 60 | if (kable_format == "latex") { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 61 | return(column_spec_latex(kable_input, column, width, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 62 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 63 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 64 | color, background, |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 65 | border_left, border_right)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 69 | column_spec_html <- function(kable_input, column, width, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 70 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 71 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 72 | color, background, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 73 | border_left, border_right, extra_css) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 74 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 75 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 76 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 77 | |
| 78 | group_header_rows <- attr(kable_input, "group_header_rows") |
Hao Zhu | 9b43f62 | 2017-09-11 19:00:08 -0400 | [diff] [blame] | 79 | all_contents_rows <- seq(1, length(xml_children(kable_tbody))) |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 80 | |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 81 | if (!is.null(group_header_rows)) { |
| 82 | all_contents_rows <- all_contents_rows[!all_contents_rows %in% |
| 83 | group_header_rows] |
| 84 | } |
| 85 | |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 86 | # Border css |
| 87 | border_l_css <- "1px solid" |
| 88 | border_r_css <- "1px solid" |
| 89 | if (is.character(border_left)) { |
| 90 | border_l_css <- border_left |
| 91 | border_left <- T |
| 92 | } |
| 93 | if (is.character(border_right)) { |
| 94 | border_r_css <- border_right |
| 95 | border_right <- T |
| 96 | } |
| 97 | |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 98 | for (i in all_contents_rows) { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 99 | for (j in column) { |
Hao Zhu | 9b43f62 | 2017-09-11 19:00:08 -0400 | [diff] [blame] | 100 | target_cell <- xml_child(xml_child(kable_tbody, i), j) |
| 101 | if (!is.null(width)) { |
| 102 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
Hao Zhu | 6a14e88 | 2017-10-31 17:04:12 -0400 | [diff] [blame] | 103 | "width: ", width, "; ") |
Hao Zhu | 9b43f62 | 2017-09-11 19:00:08 -0400 | [diff] [blame] | 104 | } |
| 105 | if (bold) { |
| 106 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 107 | "font-weight: bold;") |
| 108 | } |
| 109 | if (italic) { |
| 110 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 111 | "font-style: italic;") |
| 112 | } |
| 113 | if (monospace) { |
| 114 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 115 | "font-family: monospace;") |
| 116 | } |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 117 | if (underline) { |
| 118 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
Hao Zhu | b49bddf | 2018-01-12 15:25:23 -0500 | [diff] [blame] | 119 | "text-decoration: underline;") |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 120 | } |
| 121 | if (strikeout) { |
| 122 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
Hao Zhu | b49bddf | 2018-01-12 15:25:23 -0500 | [diff] [blame] | 123 | "text-decoration: line-through;") |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 124 | } |
Hao Zhu | 9b43f62 | 2017-09-11 19:00:08 -0400 | [diff] [blame] | 125 | if (!is.null(color)) { |
| 126 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 127 | "color: ", color, ";") |
| 128 | } |
| 129 | if (!is.null(background)) { |
| 130 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 131 | "background-color: ", |
| 132 | background, ";") |
| 133 | } |
| 134 | if (border_left) { |
| 135 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 136 | "border-left:", border_l_css, ";") |
| 137 | } |
| 138 | if (border_right) { |
| 139 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 140 | "border-right:", border_r_css, ";") |
| 141 | } |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 142 | if (!is.null(extra_css)) { |
| 143 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 144 | extra_css) |
| 145 | } |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 146 | } |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 147 | } |
Hao Zhu | 6a14e88 | 2017-10-31 17:04:12 -0400 | [diff] [blame] | 148 | |
| 149 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 150 | out <- as_kable_xml(kable_xml) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 151 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 152 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 153 | return(out) |
| 154 | } |
| 155 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 156 | column_spec_latex <- function(kable_input, column, width, |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 157 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 158 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 159 | color, background, |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 160 | border_left, border_right) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 161 | table_info <- magic_mirror(kable_input) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 162 | if (!is.null(table_info$collapse_rows)) { |
| 163 | message("Usually it is recommended to use column_spec before collapse_rows,", |
| 164 | " especially in LaTeX, to get a desired result. ") |
| 165 | } |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 166 | align_collapse <- ifelse(table_info$booktabs, "", "\\|") |
| 167 | kable_align_old <- paste(table_info$align_vector, collapse = align_collapse) |
| 168 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 169 | table_info$align_vector[column] <- unlist(lapply( |
| 170 | table_info$align_vector_origin[column], |
Hao Zhu | 6ea2afd | 2017-09-11 18:30:49 -0400 | [diff] [blame] | 171 | function(x) { |
| 172 | latex_column_align_builder( |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 173 | x, width, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 6ea2afd | 2017-09-11 18:30:49 -0400 | [diff] [blame] | 174 | color, background, border_left, border_right) |
| 175 | } |
| 176 | )) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 177 | |
| 178 | kable_align_new <- paste(table_info$align_vector, collapse = align_collapse) |
| 179 | |
Hao Zhu | d2c0f73 | 2017-08-26 10:40:14 -0400 | [diff] [blame] | 180 | out <- sub(kable_align_old, kable_align_new, |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 181 | solve_enc(kable_input), |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 182 | perl = T) |
| 183 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 184 | if (!is.null(width)) { |
| 185 | if (is.null(table_info$column_width)) { |
| 186 | table_info$column_width <- list() |
| 187 | } |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 188 | for (i in column) { |
Hao Zhu | 9b43f62 | 2017-09-11 19:00:08 -0400 | [diff] [blame] | 189 | table_info$column_width[[paste0("column_", i)]] <- width |
| 190 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 191 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 192 | attr(out, "kable_meta") <- table_info |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 193 | return(out) |
| 194 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 195 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 196 | latex_column_align_builder <- function(x, width, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 197 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 198 | color, background, |
| 199 | border_left, border_right) { |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 200 | extra_align <- "" |
| 201 | if (!is.null(width)) { |
| 202 | extra_align <- switch(x, |
Hao Zhu | bf5bfe2 | 2017-06-21 14:37:41 -0400 | [diff] [blame] | 203 | "l" = "\\\\raggedright\\\\arraybackslash", |
| 204 | "c" = "\\\\centering\\\\arraybackslash", |
| 205 | "r" = "\\\\raggedleft\\\\arraybackslash") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 206 | x <- paste0("p\\{", width, "\\}") |
| 207 | } |
| 208 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 209 | if (!is.null(color)) { |
Stefanie LaZerte | 994fc56 | 2017-12-11 14:14:07 -0600 | [diff] [blame] | 210 | color <- paste0("\\\\leavevmode\\\\color", latex_color(color)) |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Hao Zhu | f2ebf9e | 2017-09-14 11:40:49 -0400 | [diff] [blame] | 213 | if (!is.null(background)) { |
Hao Zhu | 915b1b2 | 2017-11-09 14:01:44 -0500 | [diff] [blame] | 214 | background <- paste0("\\\\columncolor", latex_color(background)) |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 217 | latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily", |
| 218 | "\\\\underline", "\\\\sout")[ |
Hao Zhu | b49bddf | 2018-01-12 15:25:23 -0500 | [diff] [blame] | 219 | c(bold, italic, monospace, underline, strikeout)] |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 220 | latex_array_options <- c(latex_array_options, extra_align, |
| 221 | color, background) |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 222 | latex_array_options <- paste0( |
| 223 | "\\>\\{", paste(latex_array_options, collapse = ""), "\\}" |
| 224 | ) |
| 225 | x <- paste0(latex_array_options, x) |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 226 | if (border_left) { |
Hao Zhu | b49bddf | 2018-01-12 15:25:23 -0500 | [diff] [blame] | 227 | x <- paste0("\\|", x) |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 228 | } |
| 229 | if (border_right) { |
Hao Zhu | b49bddf | 2018-01-12 15:25:23 -0500 | [diff] [blame] | 230 | x <- paste0(x, "\\|") |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 231 | } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 232 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 233 | return(x) |
| 234 | } |