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 | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 19 | #' |
| 20 | #' @examples x <- knitr::kable(head(mtcars), "html") |
| 21 | #' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE) |
| 22 | #' |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 23 | #' @export |
Hao Zhu | f13a35e | 2017-05-24 00:55:43 -0400 | [diff] [blame] | 24 | column_spec <- function(kable_input, column, |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 25 | width = NULL, bold = FALSE, italic = FALSE, |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 26 | monospace = FALSE, color = NULL, background = NULL) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 27 | if (!is.numeric(column)) { |
| 28 | stop("column must be a numeric value") |
| 29 | } |
| 30 | kable_format <- attr(kable_input, "format") |
| 31 | if (!kable_format %in% c("html", "latex")) { |
| 32 | message("Currently generic markdown table using pandoc is not supported.") |
| 33 | return(kable_input) |
| 34 | } |
| 35 | if (kable_format == "html") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 36 | return(column_spec_html(kable_input, column, width, |
| 37 | bold, italic, monospace, |
| 38 | color, background)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 39 | } |
| 40 | if (kable_format == "latex") { |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 41 | return(column_spec_latex(kable_input, column, width, |
| 42 | bold, italic, monospace, |
| 43 | color, background)) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 47 | column_spec_html <- function(kable_input, column, width, |
| 48 | bold, italic, monospace, |
| 49 | color, background) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 50 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 51 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 52 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 53 | |
| 54 | group_header_rows <- attr(kable_input, "group_header_rows") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 55 | if (is.null(kable_attrs$column_adjust)) { |
| 56 | all_contents_rows <- seq(1, length(xml_children(kable_tbody))) |
| 57 | all_contents_array <- rep(column, length(all_contents_rows)) |
| 58 | } else { |
| 59 | column <- column + kable_attrs$column_adjust$count |
| 60 | all_contents_array <- colSums(kable_attrs$column_adjust$matrix[1:column, ]) |
| 61 | all_contents_rows <- which(all_contents_array != 0 & |
| 62 | kable_attrs$column_adjust$matrix[column, ]) |
| 63 | } |
| 64 | |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 65 | if (!is.null(group_header_rows)) { |
| 66 | all_contents_rows <- all_contents_rows[!all_contents_rows %in% |
| 67 | group_header_rows] |
| 68 | } |
| 69 | |
| 70 | for (i in all_contents_rows) { |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 71 | 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] | 72 | if (!is.null(width)) { |
| 73 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame^] | 74 | "width: ", width, "; ") |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 75 | } |
| 76 | if (bold) { |
| 77 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 78 | "font-weight: bold;") |
| 79 | } |
| 80 | if (italic) { |
| 81 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 82 | "font-style: italic;") |
| 83 | } |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 84 | if (monospace) { |
| 85 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 86 | "font-family: monospace;") |
| 87 | } |
Hao Zhu | 669bcd2 | 2017-08-19 14:53:40 -0400 | [diff] [blame] | 88 | if (!is.null(color)) { |
| 89 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 90 | "color: ", color, ";") |
| 91 | } |
| 92 | if (!is.null(background)) { |
| 93 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 94 | "background-color: ", |
| 95 | background, ";") |
| 96 | } |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 97 | } |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 98 | out <- as_kable_xml(kable_xml) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 99 | attributes(out) <- kable_attrs |
| 100 | return(out) |
| 101 | } |
| 102 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame^] | 103 | column_spec_latex <- function(kable_input, column, width, |
| 104 | bold, italic, monospace, |
| 105 | color, background) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 106 | table_info <- magic_mirror(kable_input) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 107 | if (!is.null(table_info$collapse_rows)) { |
| 108 | message("Usually it is recommended to use column_spec before collapse_rows,", |
| 109 | " especially in LaTeX, to get a desired result. ") |
| 110 | } |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame^] | 111 | if (!is.null(background)) { |
| 112 | warning("Column background color for LaTeX has not yet been implemented.") |
| 113 | } |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 114 | align_collapse <- ifelse(table_info$booktabs, "", "\\|") |
| 115 | kable_align_old <- paste(table_info$align_vector, collapse = align_collapse) |
| 116 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 117 | table_info$align_vector[column] <- latex_column_align_builder( |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame^] | 118 | table_info$align_vector[column], width, bold, italic, monospace, |
| 119 | color, background) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 120 | |
| 121 | kable_align_new <- paste(table_info$align_vector, collapse = align_collapse) |
| 122 | |
| 123 | out <- sub(kable_align_old, kable_align_new, as.character(kable_input), |
| 124 | perl = T) |
| 125 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 126 | if (!is.null(width)) { |
| 127 | if (is.null(table_info$column_width)) { |
| 128 | table_info$column_width <- list() |
| 129 | } |
| 130 | table_info$column_width[[paste0("column_", column)]] <- width |
| 131 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 132 | attr(out, "kable_meta") <- table_info |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 133 | return(out) |
| 134 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 135 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame^] | 136 | latex_column_align_builder <- function(x, width, bold, italic, monospace, |
| 137 | color, background) { |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 138 | extra_align <- "" |
| 139 | if (!is.null(width)) { |
| 140 | extra_align <- switch(x, |
Hao Zhu | bf5bfe2 | 2017-06-21 14:37:41 -0400 | [diff] [blame] | 141 | "l" = "\\\\raggedright\\\\arraybackslash", |
| 142 | "c" = "\\\\centering\\\\arraybackslash", |
| 143 | "r" = "\\\\raggedleft\\\\arraybackslash") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 144 | x <- paste0("p\\{", width, "\\}") |
| 145 | } |
| 146 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame^] | 147 | if (!is.null(color)) { |
| 148 | color <- sprintf("\\\\color{%s}", color) |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Hao Zhu | a73601b | 2017-08-19 15:31:51 -0400 | [diff] [blame^] | 151 | latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[ |
| 152 | c(bold, italic, monospace)] |
| 153 | latex_array_options <- c(latex_array_options, extra_align, color) |
| 154 | latex_array_options <- paste0( |
| 155 | "\\>\\{", paste(latex_array_options, collapse = ""), "\\}" |
| 156 | ) |
| 157 | x <- paste0(latex_array_options, x) |
| 158 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 159 | return(x) |
| 160 | } |