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