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