blob: 0c351a3d9146d07a1d301bfdd4292da7a6c0a443 [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
Hao Zhuf2100832018-01-11 16:20:29 -0500134 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhubff01912017-05-23 18:05:00 -0400135 return(out)
136}
137
Hao Zhu322de082017-09-11 19:25:29 -0400138column_spec_latex <- function(kable_input, column, width,
Hao Zhua73601b2017-08-19 15:31:51 -0400139 bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400140 color, background,
Hao Zhu4840bc92017-09-15 15:55:05 -0400141 border_left, border_right) {
Hao Zhubff01912017-05-23 18:05:00 -0400142 table_info <- magic_mirror(kable_input)
Hao Zhuf4b35292017-06-25 22:38:37 -1000143 if (!is.null(table_info$collapse_rows)) {
144 message("Usually it is recommended to use column_spec before collapse_rows,",
145 " especially in LaTeX, to get a desired result. ")
146 }
Hao Zhubff01912017-05-23 18:05:00 -0400147 align_collapse <- ifelse(table_info$booktabs, "", "\\|")
148 kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
149
Hao Zhu322de082017-09-11 19:25:29 -0400150 table_info$align_vector[column] <- unlist(lapply(
151 table_info$align_vector_origin[column],
Hao Zhu6ea2afd2017-09-11 18:30:49 -0400152 function(x) {
153 latex_column_align_builder(
154 x, width, bold, italic, monospace,
155 color, background, border_left, border_right)
156 }
157 ))
Hao Zhubff01912017-05-23 18:05:00 -0400158
159 kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
160
Hao Zhud2c0f732017-08-26 10:40:14 -0400161 out <- sub(kable_align_old, kable_align_new,
162 enc2utf8(as.character(kable_input)),
Hao Zhubff01912017-05-23 18:05:00 -0400163 perl = T)
164 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhuf4b35292017-06-25 22:38:37 -1000165 if (!is.null(width)) {
166 if (is.null(table_info$column_width)) {
167 table_info$column_width <- list()
168 }
Hao Zhu322de082017-09-11 19:25:29 -0400169 for (i in column) {
Hao Zhu9b43f622017-09-11 19:00:08 -0400170 table_info$column_width[[paste0("column_", i)]] <- width
171 }
Hao Zhuf4b35292017-06-25 22:38:37 -1000172 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400173 attr(out, "kable_meta") <- table_info
Hao Zhubff01912017-05-23 18:05:00 -0400174 return(out)
175}
Hao Zhu32f43f72017-06-20 18:24:54 -0400176
Hao Zhua73601b2017-08-19 15:31:51 -0400177latex_column_align_builder <- function(x, width, bold, italic, monospace,
Hao Zhuec7ab922017-08-19 22:56:44 -0400178 color, background,
179 border_left, border_right) {
Hao Zhu32f43f72017-06-20 18:24:54 -0400180 extra_align <- ""
181 if (!is.null(width)) {
182 extra_align <- switch(x,
Hao Zhubf5bfe22017-06-21 14:37:41 -0400183 "l" = "\\\\raggedright\\\\arraybackslash",
184 "c" = "\\\\centering\\\\arraybackslash",
185 "r" = "\\\\raggedleft\\\\arraybackslash")
Hao Zhu32f43f72017-06-20 18:24:54 -0400186 x <- paste0("p\\{", width, "\\}")
187 }
188
Hao Zhua73601b2017-08-19 15:31:51 -0400189 if (!is.null(color)) {
Stefanie LaZerte994fc562017-12-11 14:14:07 -0600190 color <- paste0("\\\\leavevmode\\\\color", latex_color(color))
Hao Zhu32f43f72017-06-20 18:24:54 -0400191 }
192
Hao Zhuf2ebf9e2017-09-14 11:40:49 -0400193 if (!is.null(background)) {
Hao Zhu915b1b22017-11-09 14:01:44 -0500194 background <- paste0("\\\\columncolor", latex_color(background))
Hao Zhuec7ab922017-08-19 22:56:44 -0400195 }
196
Hao Zhua73601b2017-08-19 15:31:51 -0400197 latex_array_options <- c("\\\\bfseries", "\\\\em", "\\\\ttfamily")[
198 c(bold, italic, monospace)]
Hao Zhuec7ab922017-08-19 22:56:44 -0400199 latex_array_options <- c(latex_array_options, extra_align,
200 color, background)
Hao Zhua73601b2017-08-19 15:31:51 -0400201 latex_array_options <- paste0(
202 "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
203 )
204 x <- paste0(latex_array_options, x)
Hao Zhuec7ab922017-08-19 22:56:44 -0400205 if (border_left) {
206 x <- paste0("|", x)
207 }
208 if (border_right) {
209 x <- paste0(x, "|")
210 }
Hao Zhua73601b2017-08-19 15:31:51 -0400211
Hao Zhu32f43f72017-06-20 18:24:54 -0400212 return(x)
213}