blob: a640a4d4e6334fd17c390a51d450a4abcaef4c95 [file] [log] [blame]
Hao Zhubff01912017-05-23 18:05:00 -04001#' Specify the look of the selected column
2#'
3#' @description This function allows users to select a column and then specify
Hao Zhue7c8f702017-10-10 13:22:59 -04004#' its look.
Hao Zhubff01912017-05-23 18:05:00 -04005#'
6#' @param kable_input Output of `knitr::kable()` with `format` specified
Hao Zhu322de082017-09-11 19:25:29 -04007#' @param column A numeric value or vector indicating which column(s) to be selected.
Hao Zhubff01912017-05-23 18:05:00 -04008#' @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 Zhu8f202992017-07-15 02:20:18 -040014#' @param monospace A T/F value to control whether the text of the selected column
15#' need to be monospaced (verbatim)
Hao Zhu53e240f2017-09-04 20:04:29 -040016#' @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 Zhub1de9672018-01-08 16:29:24 -050026#' @param extra_css Extra css text to be passed into the cells of the row. Note
27#' that it's not for the whole column but to each individual cells
Hao Zhu78e61222017-05-24 20:53:35 -040028#'
29#' @examples x <- knitr::kable(head(mtcars), "html")
Hao Zhu4840bc92017-09-15 15:55:05 -040030#' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
Hao Zhu78e61222017-05-24 20:53:35 -040031#'
Hao Zhubff01912017-05-23 18:05:00 -040032#' @export
Hao Zhu322de082017-09-11 19:25:29 -040033column_spec <- function(kable_input, column,
Hao Zhu8f202992017-07-15 02:20:18 -040034 width = NULL, bold = FALSE, italic = FALSE,
Hao Zhuec7ab922017-08-19 22:56:44 -040035 monospace = FALSE, color = NULL, background = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050036 border_left = FALSE, border_right = FALSE,
37 extra_css = NULL) {
Hao Zhu322de082017-09-11 19:25:29 -040038 if (!is.numeric(column)) {
39 stop("column must be numeric. ")
Hao Zhubff01912017-05-23 18:05:00 -040040 }
41 kable_format <- attr(kable_input, "format")
42 if (!kable_format %in% c("html", "latex")) {
43 message("Currently generic markdown table using pandoc is not supported.")
44 return(kable_input)
45 }
46 if (kable_format == "html") {
Hao Zhu322de082017-09-11 19:25:29 -040047 return(column_spec_html(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040048 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040049 color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050050 border_left, border_right, extra_css))
Hao Zhubff01912017-05-23 18:05:00 -040051 }
52 if (kable_format == "latex") {
Hao Zhu322de082017-09-11 19:25:29 -040053 return(column_spec_latex(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040054 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040055 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -040056 border_left, border_right))
Hao Zhubff01912017-05-23 18:05:00 -040057 }
58}
59
Hao Zhu322de082017-09-11 19:25:29 -040060column_spec_html <- function(kable_input, column, width,
Hao Zhu669bcd22017-08-19 14:53:40 -040061 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -040062 color, background,
Hao Zhub1de9672018-01-08 16:29:24 -050063 border_left, border_right, extra_css) {
Hao Zhubff01912017-05-23 18:05:00 -040064 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040065 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhubff01912017-05-23 18:05:00 -040066 kable_tbody <- xml_tpart(kable_xml, "tbody")
67
68 group_header_rows <- attr(kable_input, "group_header_rows")
Hao Zhu9b43f622017-09-11 19:00:08 -040069 all_contents_rows <- seq(1, length(xml_children(kable_tbody)))
Hao Zhu32f43f72017-06-20 18:24:54 -040070
Hao Zhubff01912017-05-23 18:05:00 -040071 if (!is.null(group_header_rows)) {
72 all_contents_rows <- all_contents_rows[!all_contents_rows %in%
73 group_header_rows]
74 }
75
Hao Zhuec7ab922017-08-19 22:56:44 -040076 # Border css
77 border_l_css <- "1px solid"
78 border_r_css <- "1px solid"
79 if (is.character(border_left)) {
80 border_l_css <- border_left
81 border_left <- T
82 }
83 if (is.character(border_right)) {
84 border_r_css <- border_right
85 border_right <- T
86 }
87
Hao Zhubff01912017-05-23 18:05:00 -040088 for (i in all_contents_rows) {
Hao Zhu322de082017-09-11 19:25:29 -040089 for (j in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -040090 target_cell <- xml_child(xml_child(kable_tbody, i), j)
91 if (!is.null(width)) {
92 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
Hao Zhu6a14e882017-10-31 17:04:12 -040093 "width: ", width, "; ")
Hao Zhu9b43f622017-09-11 19:00:08 -040094 }
95 if (bold) {
96 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
97 "font-weight: bold;")
98 }
99 if (italic) {
100 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
101 "font-style: italic;")
102 }
103 if (monospace) {
104 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
105 "font-family: monospace;")
106 }
107 if (!is.null(color)) {
108 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
109 "color: ", color, ";")
110 }
111 if (!is.null(background)) {
112 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
113 "background-color: ",
114 background, ";")
115 }
116 if (border_left) {
117 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
118 "border-left:", border_l_css, ";")
119 }
120 if (border_right) {
121 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
122 "border-right:", border_r_css, ";")
123 }
Hao Zhub1de9672018-01-08 16:29:24 -0500124 if (!is.null(extra_css)) {
125 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
126 extra_css)
127 }
Hao Zhuec7ab922017-08-19 22:56:44 -0400128 }
Hao Zhubff01912017-05-23 18:05:00 -0400129 }
Hao Zhu6a14e882017-10-31 17:04:12 -0400130
131
Hao Zhuf2dfd142017-07-24 14:43:28 -0400132 out <- as_kable_xml(kable_xml)
Hao Zhubff01912017-05-23 18:05:00 -0400133 attributes(out) <- kable_attrs
134 return(out)
135}
136
Hao Zhu322de082017-09-11 19:25:29 -0400137column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400138 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400139 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -0400140 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -0400141 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000142 if (!is.null(table_info$collapse_rows)) {
143 message("Usually it is recommended to use column_spec before collapse_rows,",
144 " especially in LaTeX, to get a desired result. ")
145 }
Hao Zhubff01912017-05-23 18:05:00 -0400146 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
147 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
148
Hao Zhu322de082017-09-11 19:25:29 -0400149 table_info$align_vector[column] <- unlist(lapply(
150 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400151 function(x) {
152 latex_column_align_builder(
153 x, width, bold, italic, monospace,
154 color, background, border_left, border_right)
155 }
156 ))
Hao Zhubff01912017-05-23 18:05:00 -0400157
158 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
159
Hao Zhud2c0f732017-08-26 10:40:14 -0400160 out <- sub(kable_align_old, kable_align_new,
161 enc2utf8(as.character(kable_input)),
Hao Zhubff01912017-05-23 18:05:00 -0400162 perl = T)
163 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000164 if (!is.null(width)) {
165 if (is.null(table_info$column_width)) {
166 table_info$column_width <- list()
167 }
Hao Zhu322de082017-09-11 19:25:29 -0400168 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400169 table_info$column_width[[paste0("column_", i)]] <- width
170 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000171 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400172 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400173 return(out)
174}
Hao Zhu32f43f72017-06-20 18:24:54 -0400175
Hao Zhua73601b2017-08-19 15:31:51 -0400176latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400177 color, background,
178 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400179 extra_align <- ""
180 if (!is.null(width)) {
181 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400182 "l" = "\\\\raggedright\\\\arraybackslash",
183 "c" = "\\\\centering\\\\arraybackslash",
184 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400185 x <- paste0("p\\{", width, "\\}")
186 }
187
Hao Zhua73601b2017-08-19 15:31:51 -0400188 if (!is.null(color)) {
Stefanie LaZerte994fc562017-12-11 14:14:07 -0600189 color <- paste0("\\\\leavevmode\\\\color", latex_color(color))
Hao Zhu32f43f72017-06-20 18:24:54 -0400190 }
191
Hao Zhuf2ebf9e2017-09-14 11:40:49 -0400192 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500193 background <- paste0("\\\\columncolor", latex_color(background))
Hao Zhuec7ab922017-08-19 22:56:44 -0400194 }
195
Hao Zhua73601b2017-08-19 15:31:51 -0400196 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
197 c(bold, italic, monospace)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400198 latex_array_options <- c(latex_array_options, extra_align,
199 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400200 latex_array_options <- paste0(
201 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
202 )
203 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400204 if (border_left) {
205 x <- paste0("|", x)
206 }
207 if (border_right) {
208 x <- paste0(x, "|")
209 }
Hao Zhua73601b2017-08-19 15:31:51 -0400210
Hao Zhu32f43f72017-06-20 18:24:54 -0400211 return(x)
212}