blob: aacac760ce4d7a12df5c5ebfd3b8fb8c3f2732ba [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.
24#' @param hover_message A vector of strings to be displayed as hover message.
25#' Of course, this feature is nly available in HTML.
Hao Zhubacd2f32017-10-11 14:06:36 -040026#'
27#' @export
28cell_spec <- function(x, format,
29 bold = F, italic = F, monospace = F,
30 color = NULL, background = NULL,
Hao Zhu9ce317e2017-10-12 18:19:55 -040031 align = NULL, font_size = NULL, angle = NULL,
32 hover_message = NULL) {
Hao Zhubacd2f32017-10-11 14:06:36 -040033
34 if (missing(format) || is.null(format)) format = getOption('knitr.table.format')
35 if (is.null(format)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040036 message("Setting cell_spec format as html")
37 format <- "html"
Hao Zhubacd2f32017-10-11 14:06:36 -040038 }
39
40 if (tolower(format) == "html") {
41 return(cell_spec_html(x, bold, italic, monospace,
Hao Zhu9ce317e2017-10-12 18:19:55 -040042 color, background, align, font_size, angle,
43 hover_message))
Hao Zhubacd2f32017-10-11 14:06:36 -040044 }
45 if (tolower(format) == "latex") {
46 return(cell_spec_latex(x, bold, italic, monospace,
47 color, background, align, angle))
48 }
49}
50
51cell_spec_html <- function(x, bold, italic, monospace,
Hao Zhu9ce317e2017-10-12 18:19:55 -040052 color, background, align, font_size, angle,
53 hover_message) {
Hao Zhubacd2f32017-10-11 14:06:36 -040054 cell_style <- NULL
55 if (bold) cell_style <- paste(cell_style,"font-weight: bold;")
56 if (italic) cell_style <- paste(cell_style, "font-style: italic;")
57 if (monospace) cell_style <- paste(cell_style, "font-family: monospace;")
Hao Zhu9ce317e2017-10-12 18:19:55 -040058 if (!is.null(color)) cell_style <- paste0(cell_style, "color: ", color, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -040059 if (!is.null(background)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040060 cell_style <- paste0(cell_style, "border-radius: 4px; padding-right: 4px",
61 ";padding-left: 4px; background-color: ", background, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -040062 }
63 if (!is.null(align)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040064 cell_style <- paste0(cell_style, "text-align: ", align, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -040065 }
66 if (!is.null(font_size)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040067 cell_style <- paste0(cell_style, "font-size: ", font_size, "px;")
Hao Zhubacd2f32017-10-11 14:06:36 -040068 }
69 if (!is.null(angle)) {
70 cell_style <- paste0(cell_style,
Hao Zhu9ce317e2017-10-12 18:19:55 -040071 "-webkit-transform: rotate(", angle,
Hao Zhubacd2f32017-10-11 14:06:36 -040072 "deg); -moz-transform: rotate(", angle,
73 "deg); -ms-transform: rotate(", angle,
74 "deg); -o-transform: rotate(", angle, "deg);")
75 }
Hao Zhu9ce317e2017-10-12 18:19:55 -040076
77 if (!is.null(hover_message)) {
78 hover_message <- gsub("\n", "&#013;", hover_message)
79 hover_message <- paste0("data-toggle='tooltip' title='", hover_message, "'")
80 }
Hao Zhubacd2f32017-10-11 14:06:36 -040081 out <- paste0(
Hao Zhu9ce317e2017-10-12 18:19:55 -040082 '<div style="', cell_style, '"', hover_message, '>', x, '</div>'
Hao Zhubacd2f32017-10-11 14:06:36 -040083 )
84 return(out)
85}
86
87cell_spec_latex <- function(x, bold, italic, monospace,
88 color, background, align, angle) {
89 if (bold) x <- paste0("\\bfseries{", x, "}")
90 if (italic) x <-paste0("\\em{", x, "}")
91 if (monospace) x <- paste0("\\ttfamily{", x, "}")
Hao Zhu9ce317e2017-10-12 18:19:55 -040092 if (!is.null(color)) {
93 color <- latex_color(color)
94 x <- paste0("\\textcolor", color, "{", x, "}")
95 }
96 if (!is.null(background)) {
97 background <- latex_color(background)
98 x <- paste0("\\cellcolor", background, "{", x, "}")
99 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400100 if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}")
101 if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}")
102 return(x)
103}