blob: 0f55d311e0314dacc3ed4554a9b5d3768bf75d22 [file] [log] [blame]
Hao Zhubacd2f32017-10-11 14:06:36 -04001#' Specify Cell format
2#'
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()`.
8#' @param bold A T/F value to control whether the text of the selected column
9#' need to be bolded.
10#' @param italic A T/F value to control whether the text of the selected column
11#' need to be emphasized.
12#' @param monospace A T/F value to control whether the text of the selected column
13#' need to be monospaced (verbatim)
14#' @param color A character string for column text color. Here please pay
15#' attention to the differences in color codes between HTML and LaTeX.
16#' @param background A character string for column background color. Here please
17#' pay attention to the differences in color codes between HTML and LaTeX.
18#' @param align A character string for cell alignment. For HTML, possible values could
19#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
20#' while for LaTeX, you can only choose from `l`, `c` & `r`.
21#' @param font_size Only if you want to specify font size locally in HTML.
22#' This feature is not available in LaTeX
Hao Zhu9ce317e2017-10-12 18:19:55 -040023#' @param angle 0-360, degree that the text will rotate. Can be a vector.
Hao Zhu064990d2017-10-17 18:08:42 -040024#' @param tooltip A vector of strings to be displayed as tooltip.
25#' Of course, this feature is only available in HTML.
Hao Zhu457acb42017-10-14 17:37:02 -040026#' @param background_as_tile T/F value indicating if you want to have round
27#' cornered tile as background.
Hao Zhubacd2f32017-10-11 14:06:36 -040028#'
29#' @export
30cell_spec <- function(x, format,
31 bold = F, italic = F, monospace = F,
32 color = NULL, background = NULL,
Hao Zhu9ce317e2017-10-12 18:19:55 -040033 align = NULL, font_size = NULL, angle = NULL,
Hao Zhu064990d2017-10-17 18:08:42 -040034 tooltip = NULL, background_as_tile = TRUE) {
Hao Zhubacd2f32017-10-11 14:06:36 -040035
36 if (missing(format) || is.null(format)) format = getOption('knitr.table.format')
37 if (is.null(format)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040038 message("Setting cell_spec format as html")
39 format <- "html"
Hao Zhubacd2f32017-10-11 14:06:36 -040040 }
41
42 if (tolower(format) == "html") {
43 return(cell_spec_html(x, bold, italic, monospace,
Hao Zhu9ce317e2017-10-12 18:19:55 -040044 color, background, align, font_size, angle,
Hao Zhu064990d2017-10-17 18:08:42 -040045 tooltip, background_as_tile))
Hao Zhubacd2f32017-10-11 14:06:36 -040046 }
47 if (tolower(format) == "latex") {
48 return(cell_spec_latex(x, bold, italic, monospace,
49 color, background, align, angle))
50 }
51}
52
53cell_spec_html <- function(x, bold, italic, monospace,
Hao Zhu9ce317e2017-10-12 18:19:55 -040054 color, background, align, font_size, angle,
Hao Zhu064990d2017-10-17 18:08:42 -040055 tooltip, background_as_tile) {
Hao Zhubacd2f32017-10-11 14:06:36 -040056 cell_style <- NULL
57 if (bold) cell_style <- paste(cell_style,"font-weight: bold;")
58 if (italic) cell_style <- paste(cell_style, "font-style: italic;")
59 if (monospace) cell_style <- paste(cell_style, "font-family: monospace;")
Hao Zhu457acb42017-10-14 17:37:02 -040060 if (!is.null(color)) {
61 cell_style <- paste0(cell_style, "color: ", html_color(color), ";")
62 }
Hao Zhubacd2f32017-10-11 14:06:36 -040063 if (!is.null(background)) {
Hao Zhu457acb42017-10-14 17:37:02 -040064 cell_style <- paste0(
65 cell_style,
66 ifelse(background_as_tile, "border-radius: 4px; ", ""),
67 "padding-right: 4px; padding-left: 4px; ",
68 "background-color: ", html_color(background), ";"
69 )
Hao Zhubacd2f32017-10-11 14:06:36 -040070 }
71 if (!is.null(align)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040072 cell_style <- paste0(cell_style, "text-align: ", align, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -040073 }
74 if (!is.null(font_size)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040075 cell_style <- paste0(cell_style, "font-size: ", font_size, "px;")
Hao Zhubacd2f32017-10-11 14:06:36 -040076 }
77 if (!is.null(angle)) {
78 cell_style <- paste0(cell_style,
Hao Zhu9ce317e2017-10-12 18:19:55 -040079 "-webkit-transform: rotate(", angle,
Hao Zhubacd2f32017-10-11 14:06:36 -040080 "deg); -moz-transform: rotate(", angle,
81 "deg); -ms-transform: rotate(", angle,
Hao Zhu457acb42017-10-14 17:37:02 -040082 "deg); -o-transform: rotate(", angle,
83 "deg); transform: rotate(", angle,
84 "deg);")
Hao Zhubacd2f32017-10-11 14:06:36 -040085 }
Hao Zhu9ce317e2017-10-12 18:19:55 -040086
Hao Zhu064990d2017-10-17 18:08:42 -040087 if (!is.null(tooltip)) {
88 tooltip <- gsub("\n", "&#013;", tooltip)
89 tooltip <- paste0("data-toggle='tooltip' title='", tooltip, "'")
Hao Zhu9ce317e2017-10-12 18:19:55 -040090 }
Hao Zhubacd2f32017-10-11 14:06:36 -040091 out <- paste0(
Hao Zhu064990d2017-10-17 18:08:42 -040092 '<div style="', cell_style, '"', tooltip, '>', x, '</div>'
Hao Zhubacd2f32017-10-11 14:06:36 -040093 )
94 return(out)
95}
96
97cell_spec_latex <- function(x, bold, italic, monospace,
98 color, background, align, angle) {
99 if (bold) x <- paste0("\\bfseries{", x, "}")
100 if (italic) x <-paste0("\\em{", x, "}")
101 if (monospace) x <- paste0("\\ttfamily{", x, "}")
Hao Zhu9ce317e2017-10-12 18:19:55 -0400102 if (!is.null(color)) {
103 color <- latex_color(color)
104 x <- paste0("\\textcolor", color, "{", x, "}")
105 }
106 if (!is.null(background)) {
107 background <- latex_color(background)
108 x <- paste0("\\cellcolor", background, "{", x, "}")
Hao Zhu457acb42017-10-14 17:37:02 -0400109 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400110 if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}")
111 if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}")
112 return(x)
113}