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 | b1caa27 | 2018-04-14 14:19:46 -0400 | [diff] [blame] | 30 | #' @param min_width Only for HTML table. Normal column width will automatically |
| 31 | #' collapse when the window cannot hold enough contents. With this `min_width`, |
| 32 | #' you can set up a column with a width that won't collapse even when the |
| 33 | #' window is not wide enough. |
| 34 | #' @param max_width Only for HTML table. `max_width` defines the maximum width |
| 35 | #' of table columns. |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 36 | #' @param extra_css Extra css text to be passed into the cells of the row. Note |
| 37 | #' that it's not for the whole column but to each individual cells |
Hao Zhu | 907ddfe | 2018-04-23 15:19:09 -0400 | [diff] [blame] | 38 | #' @param include_thead T/F. A HTML only feature to contoll whether the |
| 39 | #' header row will be manipulated. Default is `FALSE`. |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 40 | #' |
| 41 | #' @examples x <- knitr::kable(head(mtcars), "html") |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 42 | #' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE) |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 43 | #' |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 44 | #' @export |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 45 | column_spec <- function(kable_input, column, |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 46 | width = NULL, bold = FALSE, italic = FALSE, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 47 | monospace = FALSE, underline = FALSE, strikeout = FALSE, |
| 48 | color = NULL, background = NULL, |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 49 | border_left = FALSE, border_right = FALSE, |
Hao Zhu | b1caa27 | 2018-04-14 14:19:46 -0400 | [diff] [blame] | 50 | width_min = NULL, width_max = NULL, |
Hao Zhu | 907ddfe | 2018-04-23 15:19:09 -0400 | [diff] [blame] | 51 | extra_css = NULL, include_thead = FALSE) { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 52 | if (!is.numeric(column)) { |
| 53 | stop("column must be numeric. ") |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 54 | } |
| 55 | kable_format <- attr(kable_input, "format") |
| 56 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 57 | warning("Please specify format in kable. kableExtra can customize either ", |
| 58 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 59 | "for details.") |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 60 | return(kable_input) |
| 61 | } |
| 62 | if (kable_format == "html") { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 63 | return(column_spec_html(kable_input, column, width, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 64 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 65 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 66 | color, background, |
Hao Zhu | b1caa27 | 2018-04-14 14:19:46 -0400 | [diff] [blame] | 67 | border_left, border_right, |
| 68 | width_min, width_max, |
Hao Zhu | 907ddfe | 2018-04-23 15:19:09 -0400 | [diff] [blame] | 69 | extra_css, include_thead)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 70 | } |
| 71 | if (kable_format == "latex") { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 72 | return(column_spec_latex(kable_input, column, width, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 73 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 74 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 75 | color, background, |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 76 | border_left, border_right)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 80 | column_spec_html <- function(kable_input, column, width, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 81 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 82 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 83 | color, background, |
Hao Zhu | b1caa27 | 2018-04-14 14:19:46 -0400 | [diff] [blame] | 84 | border_left, border_right, |
| 85 | width_min, width_max, |
Hao Zhu | 907ddfe | 2018-04-23 15:19:09 -0400 | [diff] [blame] | 86 | extra_css, include_thead) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 87 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 88 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 89 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 90 | |
| 91 | group_header_rows <- attr(kable_input, "group_header_rows") |
Hao Zhu | 9b43f62 | 2017-09-11 19:00:08 -0400 | [diff] [blame] | 92 | all_contents_rows <- seq(1, length(xml_children(kable_tbody))) |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 93 | |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 94 | if (!is.null(group_header_rows)) { |
| 95 | all_contents_rows <- all_contents_rows[!all_contents_rows %in% |
| 96 | group_header_rows] |
| 97 | } |
| 98 | |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 99 | # Border css |
| 100 | border_l_css <- "1px solid" |
| 101 | border_r_css <- "1px solid" |
| 102 | if (is.character(border_left)) { |
| 103 | border_l_css <- border_left |
| 104 | border_left <- T |
| 105 | } |
| 106 | if (is.character(border_right)) { |
| 107 | border_r_css <- border_right |
| 108 | border_right <- T |
| 109 | } |
| 110 | |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 111 | for (i in all_contents_rows) { |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 112 | for (j in column) { |
Hao Zhu | 9b43f62 | 2017-09-11 19:00:08 -0400 | [diff] [blame] | 113 | target_cell <- xml_child(xml_child(kable_tbody, i), j) |
Hao Zhu | 907ddfe | 2018-04-23 15:19:09 -0400 | [diff] [blame] | 114 | column_spec_html_cell( |
| 115 | target_cell, width, width_min, width_max, |
| 116 | bold, italic, monospace, underline, strikeout, |
| 117 | color, background, border_left, border_right, |
| 118 | border_l_css, border_r_css, |
| 119 | extra_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 | |
Hao Zhu | 907ddfe | 2018-04-23 15:19:09 -0400 | [diff] [blame] | 124 | if (include_thead) { |
| 125 | kable_thead <- xml_tpart(kable_xml, "thead") |
| 126 | nrow_thead <- length(xml_children(kable_thead)) |
| 127 | for (j in column) { |
| 128 | target_cell <- xml_child(xml_child(kable_thead, nrow_thead), j) |
| 129 | column_spec_html_cell( |
| 130 | target_cell, width, width_min, width_max, |
| 131 | bold, italic, monospace, underline, strikeout, |
| 132 | color, background, border_left, border_right, |
| 133 | border_l_css, border_r_css, |
| 134 | extra_css |
| 135 | ) |
| 136 | } |
| 137 | } |
Hao Zhu | 6a14e88 | 2017-10-31 17:04:12 -0400 | [diff] [blame] | 138 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 139 | out <- as_kable_xml(kable_xml) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 140 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 141 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 142 | return(out) |
| 143 | } |
| 144 | |
Hao Zhu | 907ddfe | 2018-04-23 15:19:09 -0400 | [diff] [blame] | 145 | column_spec_html_cell <- function(target_cell, width, width_min, width_max, |
| 146 | bold, italic, monospace, underline, strikeout, |
| 147 | color, background, |
| 148 | border_left, border_right, |
| 149 | border_l_css, border_r_css, |
| 150 | extra_css) { |
| 151 | if (!is.null(width)) { |
| 152 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 153 | "width: ", width, "; ") |
| 154 | } |
| 155 | if (!is.null(width_min)) { |
| 156 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 157 | "min-width: ", width_min, "; ") |
| 158 | } |
| 159 | if (!is.null(width_max)) { |
| 160 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 161 | "max-width: ", width_max, "; ") |
| 162 | } |
| 163 | if (bold) { |
| 164 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 165 | "font-weight: bold;") |
| 166 | } |
| 167 | if (italic) { |
| 168 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 169 | "font-style: italic;") |
| 170 | } |
| 171 | if (monospace) { |
| 172 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 173 | "font-family: monospace;") |
| 174 | } |
| 175 | if (underline) { |
| 176 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 177 | "text-decoration: underline;") |
| 178 | } |
| 179 | if (strikeout) { |
| 180 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 181 | "text-decoration: line-through;") |
| 182 | } |
| 183 | if (!is.null(color)) { |
| 184 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 185 | "color: ", color, ";") |
| 186 | } |
| 187 | if (!is.null(background)) { |
| 188 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 189 | "background-color: ", |
| 190 | background, ";") |
| 191 | } |
| 192 | if (border_left) { |
| 193 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 194 | "border-left:", border_l_css, ";") |
| 195 | } |
| 196 | if (border_right) { |
| 197 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 198 | "border-right:", border_r_css, ";") |
| 199 | } |
| 200 | if (!is.null(extra_css)) { |
| 201 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 202 | extra_css) |
| 203 | } |
| 204 | } |
| 205 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 206 | column_spec_latex <- function(kable_input, column, width, |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 207 | bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 208 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 209 | color, background, |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 210 | border_left, border_right) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 211 | table_info <- magic_mirror(kable_input) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 212 | if (!is.null(table_info$collapse_rows)) { |
| 213 | message("Usually it is recommended to use column_spec before collapse_rows,", |
| 214 | " especially in LaTeX, to get a desired result. ") |
| 215 | } |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 216 | align_collapse <- ifelse(table_info$booktabs, "", "\\|") |
| 217 | kable_align_old <- paste(table_info$align_vector, collapse = align_collapse) |
| 218 | |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 219 | table_info$align_vector[column] <- unlist(lapply( |
| 220 | table_info$align_vector_origin[column], |
Hao Zhu | 6ea2afd | 2017-09-11 18:30:49 -0400 | [diff] [blame] | 221 | function(x) { |
| 222 | latex_column_align_builder( |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 223 | x, width, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 6ea2afd | 2017-09-11 18:30:49 -0400 | [diff] [blame] | 224 | color, background, border_left, border_right) |
| 225 | } |
| 226 | )) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 227 | |
| 228 | kable_align_new <- paste(table_info$align_vector, collapse = align_collapse) |
| 229 | |
Hao Zhu | d2c0f73 | 2017-08-26 10:40:14 -0400 | [diff] [blame] | 230 | out <- sub(kable_align_old, kable_align_new, |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 231 | solve_enc(kable_input), |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 232 | perl = T) |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 233 | |
| 234 | if (!is.null(width)) { |
| 235 | fix_newline <- replace_makecell_with_newline(out, table_info, column) |
| 236 | out <- fix_newline[[1]] |
| 237 | table_info <- fix_newline[[2]] |
| 238 | } |
| 239 | |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 240 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 241 | if (!is.null(width)) { |
| 242 | if (is.null(table_info$column_width)) { |
| 243 | table_info$column_width <- list() |
| 244 | } |
Hao Zhu | 322de08 | 2017-09-11 19:25:29 -0400 | [diff] [blame] | 245 | for (i in column) { |
Hao Zhu | 9b43f62 | 2017-09-11 19:00:08 -0400 | [diff] [blame] | 246 | table_info$column_width[[paste0("column_", i)]] <- width |
| 247 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 248 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 249 | attr(out, "kable_meta") <- table_info |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 250 | return(out) |
| 251 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 252 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 253 | latex_column_align_builder <- function(x, width, bold, italic, monospace, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 254 | underline, strikeout, |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 255 | color, background, |
| 256 | border_left, border_right) { |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 257 | extra_align <- "" |
| 258 | if (!is.null(width)) { |
| 259 | extra_align <- switch(x, |
Hao Zhu | bf5bfe2 | 2017-06-21 14:37:41 -0400 | [diff] [blame] | 260 | "l" = "\\\\raggedright\\\\arraybackslash", |
| 261 | "c" = "\\\\centering\\\\arraybackslash", |
| 262 | "r" = "\\\\raggedleft\\\\arraybackslash") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 263 | x <- paste0("p\\{", width, "\\}") |
| 264 | } |
| 265 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 266 | if (!is.null(color)) { |
Stefanie LaZerte | 994fc56 | 2017-12-11 14:14:07 -0600 | [diff] [blame] | 267 | color <- paste0("\\\\leavevmode\\\\color", latex_color(color)) |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Hao Zhu | f2ebf9e | 2017-09-14 11:40:49 -0400 | [diff] [blame] | 270 | if (!is.null(background)) { |
Hao Zhu | 915b1b2 | 2017-11-09 14:01:44 -0500 | [diff] [blame] | 271 | background <- paste0("\\\\columncolor", latex_color(background)) |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 274 | latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily", |
| 275 | "\\\\underline", "\\\\sout")[ |
Hao Zhu | b49bddf | 2018-01-12 15:25:23 -0500 | [diff] [blame] | 276 | c(bold, italic, monospace, underline, strikeout)] |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 277 | latex_array_options <- c(latex_array_options, extra_align, |
| 278 | color, background) |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 279 | latex_array_options <- paste0( |
| 280 | "\\>\\{", paste(latex_array_options, collapse = ""), "\\}" |
| 281 | ) |
| 282 | x <- paste0(latex_array_options, x) |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 283 | if (border_left) { |
Hao Zhu | b49bddf | 2018-01-12 15:25:23 -0500 | [diff] [blame] | 284 | x <- paste0("\\|", x) |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 285 | } |
| 286 | if (border_right) { |
Hao Zhu | b49bddf | 2018-01-12 15:25:23 -0500 | [diff] [blame] | 287 | x <- paste0(x, "\\|") |
Hao Zhu | ec7ab92 | 2017-08-19 22:56:44 -0400 | [diff] [blame] | 288 | } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame] | 289 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 290 | return(x) |
| 291 | } |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 292 | |
| 293 | replace_makecell_with_newline <- function(kable_input, table_info, column) { |
| 294 | if (!str_detect(kable_input, "makecell")) return(list(kable_input, table_info)) |
| 295 | contents_table <- data.frame(sapply(table_info$contents, |
| 296 | function(x) {str_split(x, " \\& ")[[1]]}), |
| 297 | stringsAsFactors = F) |
| 298 | names(contents_table) <- paste0("x", 1:table_info$nrow) |
| 299 | rows_check_makecell <- str_detect(contents_table[column, ], "makecell") |
| 300 | if (sum(rows_check_makecell) == 0) return(list(kable_input, table_info)) |
| 301 | rows_to_replace <- which(rows_check_makecell) |
| 302 | |
| 303 | for (i in column) { |
| 304 | target_column <- contents_table[i, ] |
| 305 | for (j in which(str_detect(target_column, "\\\\\\\\makecell"))) { |
| 306 | contents_table[i, j] <- str_replace( |
| 307 | contents_table[i, j], "\\\\\\\\makecell\\\\\\[.\\\\\\]\\\\\\{", "") |
| 308 | contents_table[i, j] <- str_replace( |
Hao Zhu | 9ac3e38 | 2018-04-12 18:56:32 -0400 | [diff] [blame] | 309 | contents_table[i, j], "\\\\\\}$", "") |
Hao Zhu | ae80df4 | 2018-04-12 15:45:11 -0400 | [diff] [blame] | 310 | contents_table[i, j] <- str_replace_all( |
| 311 | contents_table[i, j], "\\\\\\\\\\\\\\\\", "\\\\\\\\newline " |
| 312 | ) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | new_contents <- unlist(lapply(contents_table, paste, collapse = " & ")) |
| 317 | for (i in rows_to_replace) { |
| 318 | kable_input <- sub(table_info$contents[i], new_contents[i], kable_input, |
| 319 | perl = T) |
| 320 | table_info$contents[i] <- new_contents[i] |
| 321 | } |
| 322 | |
| 323 | return(list(kable_input, table_info)) |
| 324 | } |