blob: 4fa58958ba1d0d3a7c1e187db0424ba08e13c406 [file] [log] [blame]
Hao Zhuce5ee412017-10-23 01:14:38 -04001#' Specify Cell/Text format
Hao Zhubacd2f32017-10-11 14:06:36 -04002#'
3#' @description Specify Cell format before it gets into kable
4#'
5#' @param x Things to be formated. It could be a vector of numbers or strings.
6#' @param format Either "html" or "latex". It can also be set through
7#' `option(knitr.table.format)`, same as `knitr::kable()`.
Hao Zhuce5ee412017-10-23 01:14:38 -04008#' @param bold T/F for font bold.
9#' @param italic T/F for font italic.
10#' @param monospace T/F for font monospaced (verbatim)
11#' @param color A character string for text color. Here please pay
Hao Zhubacd2f32017-10-11 14:06:36 -040012#' attention to the differences in color codes between HTML and LaTeX.
Hao Zhuce5ee412017-10-23 01:14:38 -040013#' @param background A character string for background color. Here please
14#' pay attention to the differences in color codes between HTML and LaTeX. Also
15#' note that in HTML, background defined in cell_spec won't cover the whole
16#' cell.
17#' @param align A character string for cell alignment. For HTML, possible
18#' values could be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`,
19#' `initial` and `inherit` while for LaTeX, you can only choose
20#' from `l`, `c` & `r`.
21#' @param font_size j
Hao Zhu9ce317e2017-10-12 18:19:55 -040022#' @param angle 0-360, degree that the text will rotate. Can be a vector.
Hao Zhu064990d2017-10-17 18:08:42 -040023#' @param tooltip A vector of strings to be displayed as tooltip.
Hao Zhuce5ee412017-10-23 01:14:38 -040024#' Obviously, this feature is only available in HTML.
Hao Zhu457acb42017-10-14 17:37:02 -040025#' @param background_as_tile T/F value indicating if you want to have round
Hao Zhuce5ee412017-10-23 01:14:38 -040026#' cornered tile as background in HTML.
27#' @param latex_background_in_cell T/F value. It only takes effect in LaTeX
28#' when `background` provided, Default value is `TRUE`. If it's `TRUE`, the
29#' background only works in a table cell. If it's `FALSE`, it works outside of a
30#' table environment.
Hao Zhubacd2f32017-10-11 14:06:36 -040031#'
32#' @export
33cell_spec <- function(x, format,
34 bold = F, italic = F, monospace = F,
35 color = NULL, background = NULL,
Hao Zhu9ce317e2017-10-12 18:19:55 -040036 align = NULL, font_size = NULL, angle = NULL,
Hao Zhuce5ee412017-10-23 01:14:38 -040037 tooltip = NULL, background_as_tile = TRUE,
38 latex_background_in_cell = TRUE) {
Hao Zhubacd2f32017-10-11 14:06:36 -040039
40 if (missing(format) || is.null(format)) format = getOption('knitr.table.format')
41 if (is.null(format)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040042 message("Setting cell_spec format as html")
43 format <- "html"
Hao Zhubacd2f32017-10-11 14:06:36 -040044 }
45
46 if (tolower(format) == "html") {
47 return(cell_spec_html(x, bold, italic, monospace,
Hao Zhu9ce317e2017-10-12 18:19:55 -040048 color, background, align, font_size, angle,
Hao Zhu064990d2017-10-17 18:08:42 -040049 tooltip, background_as_tile))
Hao Zhubacd2f32017-10-11 14:06:36 -040050 }
51 if (tolower(format) == "latex") {
52 return(cell_spec_latex(x, bold, italic, monospace,
Hao Zhuce5ee412017-10-23 01:14:38 -040053 color, background, align, font_size, angle,
54 latex_background_in_cell))
Hao Zhubacd2f32017-10-11 14:06:36 -040055 }
56}
57
58cell_spec_html <- function(x, bold, italic, monospace,
Hao Zhu9ce317e2017-10-12 18:19:55 -040059 color, background, align, font_size, angle,
Hao Zhu064990d2017-10-17 18:08:42 -040060 tooltip, background_as_tile) {
Hao Zhubacd2f32017-10-11 14:06:36 -040061 cell_style <- NULL
62 if (bold) cell_style <- paste(cell_style,"font-weight: bold;")
63 if (italic) cell_style <- paste(cell_style, "font-style: italic;")
64 if (monospace) cell_style <- paste(cell_style, "font-family: monospace;")
Hao Zhu457acb42017-10-14 17:37:02 -040065 if (!is.null(color)) {
66 cell_style <- paste0(cell_style, "color: ", html_color(color), ";")
67 }
Hao Zhubacd2f32017-10-11 14:06:36 -040068 if (!is.null(background)) {
Hao Zhu457acb42017-10-14 17:37:02 -040069 cell_style <- paste0(
70 cell_style,
71 ifelse(background_as_tile, "border-radius: 4px; ", ""),
72 "padding-right: 4px; padding-left: 4px; ",
73 "background-color: ", html_color(background), ";"
74 )
Hao Zhubacd2f32017-10-11 14:06:36 -040075 }
76 if (!is.null(align)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040077 cell_style <- paste0(cell_style, "text-align: ", align, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -040078 }
79 if (!is.null(font_size)) {
Hao Zhuce5ee412017-10-23 01:14:38 -040080 if (is.numeric) font_size <- paste0(font_size, "px")
81 cell_style <- paste0(cell_style, "font-size: ", font_size, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -040082 }
Hao Zhu064990d2017-10-17 18:08:42 -040083 if (!is.null(tooltip)) {
84 tooltip <- gsub("\n", "&#013;", tooltip)
85 tooltip <- paste0("data-toggle='tooltip' title='", tooltip, "'")
Hao Zhu9ce317e2017-10-12 18:19:55 -040086 }
Hao Zhubacd2f32017-10-11 14:06:36 -040087 out <- paste0(
Hao Zhuce5ee412017-10-23 01:14:38 -040088 '<span style="', cell_style, '"', tooltip, '>', x, '</span>'
Hao Zhubacd2f32017-10-11 14:06:36 -040089 )
Hao Zhuce5ee412017-10-23 01:14:38 -040090
91 if (!is.null(angle)) {
92 rotate_css <- paste0("-webkit-transform: rotate(", angle,
93 "deg); -moz-transform: rotate(", angle,
94 "deg); -ms-transform: rotate(", angle,
95 "deg); -o-transform: rotate(", angle,
96 "deg); transform: rotate(", angle,
97 "deg);")
98 out <- paste0('<div style="', rotate_css, '">', out, '</div>')
99 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400100 return(out)
101}
102
103cell_spec_latex <- function(x, bold, italic, monospace,
Hao Zhuce5ee412017-10-23 01:14:38 -0400104 color, background, align, font_size, angle,
105 latex_background_in_cell) {
Hao Zhubacd2f32017-10-11 14:06:36 -0400106 if (bold) x <- paste0("\\bfseries{", x, "}")
Hao Zhuce5ee412017-10-23 01:14:38 -0400107 if (italic) x <- paste0("\\em{", x, "}")
Hao Zhubacd2f32017-10-11 14:06:36 -0400108 if (monospace) x <- paste0("\\ttfamily{", x, "}")
Hao Zhu9ce317e2017-10-12 18:19:55 -0400109 if (!is.null(color)) {
110 color <- latex_color(color)
111 x <- paste0("\\textcolor", color, "{", x, "}")
112 }
113 if (!is.null(background)) {
114 background <- latex_color(background)
Hao Zhuce5ee412017-10-23 01:14:38 -0400115 background_env <- ifelse(latex_background_in_cell, "cellcolor", "colorbox")
116 x <- paste0("\\", background_env, background, "{", x, "}")
Hao Zhu457acb42017-10-14 17:37:02 -0400117 }
Hao Zhuce5ee412017-10-23 01:14:38 -0400118 if (!is.null(font_size)) {
119 x <- paste0("\\begingroup\\fontsize{", font_size, "}{", as.numeric(font_size) + 2,
120 "}\\selectfont ", x, "\\endgroup")
121 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400122 if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}")
Hao Zhuce5ee412017-10-23 01:14:38 -0400123 if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}")
Hao Zhubacd2f32017-10-11 14:06:36 -0400124 return(x)
125}
Hao Zhuce5ee412017-10-23 01:14:38 -0400126
127#' @rdname cell_spec
128#' @export
129text_spec <- function(x, format,
130 bold = F, italic = F, monospace = F,
131 color = NULL, background = NULL,
132 align = NULL, font_size = NULL, angle = NULL,
133 tooltip = NULL, background_as_tile = TRUE,
134 latex_background_in_cell = FALSE) {
135 cell_spec(x, format, bold, italic, monospace, color, background, align,
136 font_size, angle, tooltip, background_as_tile,
137 latex_background_in_cell)
138}