blob: bc7af1ed85f5996de8314a1d6cf78f1f8ba6b297 [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`.
Hao Zhu6f362bb2017-10-23 23:21:38 -040021#' @param font_size A numeric input for font size. For HTML, you can also use
Hao Zhu8b32b192017-10-24 14:51:48 -040022#' options including `xx-small`, `x-small`, `small`, `medium`, `large`,
23#' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`.
Hao Zhu9ce317e2017-10-12 18:19:55 -040024#' @param angle 0-360, degree that the text will rotate. Can be a vector.
Hao Zhu064990d2017-10-17 18:08:42 -040025#' @param tooltip A vector of strings to be displayed as tooltip.
Hao Zhu6f362bb2017-10-23 23:21:38 -040026#' Obviously, this feature is only available in HTML. Read the package
27#' vignette to see how to use bootstrap tooltip css to improve the loading
28#' speed and look.
29#' @param popover Similar with tooltip but can hold more contents. The best way
30#' to build a popover is through `spec_popover()`. If you only provide a text
31#' string, it will be used as content. Note that You have to enable this
32#' bootstrap module manually. Read the package vignette to see how.
33#' @param link A vector of strings for url links. Can be used together with
34#' tooltip and popover.
Hao Zhub1de9672018-01-08 16:29:24 -050035#' @param extra_css Extra css text to be passed into the cell
Hao Zhu6f362bb2017-10-23 23:21:38 -040036#' @param escape T/F value showing whether special characters should be escaped.
Hao Zhu457acb42017-10-14 17:37:02 -040037#' @param background_as_tile T/F value indicating if you want to have round
Hao Zhuce5ee412017-10-23 01:14:38 -040038#' cornered tile as background in HTML.
39#' @param latex_background_in_cell T/F value. It only takes effect in LaTeX
40#' when `background` provided, Default value is `TRUE`. If it's `TRUE`, the
41#' background only works in a table cell. If it's `FALSE`, it works outside of a
42#' table environment.
Hao Zhubacd2f32017-10-11 14:06:36 -040043#'
44#' @export
45cell_spec <- function(x, format,
Hao Zhu6f362bb2017-10-23 23:21:38 -040046 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhubacd2f32017-10-11 14:06:36 -040047 color = NULL, background = NULL,
Hao Zhu9ce317e2017-10-12 18:19:55 -040048 align = NULL, font_size = NULL, angle = NULL,
Hao Zhu6f362bb2017-10-23 23:21:38 -040049 tooltip = NULL, popover = NULL, link = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050050 extra_css = NULL,
Hao Zhu6f362bb2017-10-23 23:21:38 -040051 escape = TRUE,
52 background_as_tile = TRUE,
Hao Zhuce5ee412017-10-23 01:14:38 -040053 latex_background_in_cell = TRUE) {
Hao Zhubacd2f32017-10-11 14:06:36 -040054
Hao Zhu1ac13ad2018-01-08 16:12:24 -050055 if (missing(format) || is.null(format)) {
56 format <- getOption('knitr.table.format')
57 }
Hao Zhubacd2f32017-10-11 14:06:36 -040058 if (is.null(format)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040059 message("Setting cell_spec format as html")
60 format <- "html"
Hao Zhubacd2f32017-10-11 14:06:36 -040061 }
62
63 if (tolower(format) == "html") {
64 return(cell_spec_html(x, bold, italic, monospace,
Hao Zhu9ce317e2017-10-12 18:19:55 -040065 color, background, align, font_size, angle,
Hao Zhub1de9672018-01-08 16:29:24 -050066 tooltip, popover, link, extra_css,
Hao Zhu6f362bb2017-10-23 23:21:38 -040067 escape, background_as_tile))
Hao Zhubacd2f32017-10-11 14:06:36 -040068 }
69 if (tolower(format) == "latex") {
70 return(cell_spec_latex(x, bold, italic, monospace,
Hao Zhu6f362bb2017-10-23 23:21:38 -040071 color, background, align, font_size, angle, escape,
Hao Zhuce5ee412017-10-23 01:14:38 -040072 latex_background_in_cell))
Hao Zhubacd2f32017-10-11 14:06:36 -040073 }
74}
75
76cell_spec_html <- function(x, bold, italic, monospace,
Hao Zhu9ce317e2017-10-12 18:19:55 -040077 color, background, align, font_size, angle,
Hao Zhub1de9672018-01-08 16:29:24 -050078 tooltip, popover, link, extra_css,
Hao Zhu6f362bb2017-10-23 23:21:38 -040079 escape, background_as_tile) {
80 if (escape) x <- escape_html(x)
Hao Zhubacd2f32017-10-11 14:06:36 -040081 cell_style <- NULL
Hao Zhu1ac13ad2018-01-08 16:12:24 -050082 cell_style <- paste(cell_style, ifelse(bold, "font-weight: bold;", ""))
83 cell_style <- paste(cell_style, ifelse(italic, "font-style: italic;", ""))
84 cell_style <- paste(cell_style,
85 ifelse(monospace, "font-family: monospace;", ""))
Hao Zhu457acb42017-10-14 17:37:02 -040086 if (!is.null(color)) {
87 cell_style <- paste0(cell_style, "color: ", html_color(color), ";")
88 }
Hao Zhubacd2f32017-10-11 14:06:36 -040089 if (!is.null(background)) {
Hao Zhu457acb42017-10-14 17:37:02 -040090 cell_style <- paste0(
91 cell_style,
92 ifelse(background_as_tile, "border-radius: 4px; ", ""),
93 "padding-right: 4px; padding-left: 4px; ",
94 "background-color: ", html_color(background), ";"
95 )
Hao Zhubacd2f32017-10-11 14:06:36 -040096 }
Hao Zhub1de9672018-01-08 16:29:24 -050097 if (!is.null(extra_css)) {
98 cell_style <- paste0(cell_style, extra_css)
99 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400100 if (!is.null(align)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -0400101 cell_style <- paste0(cell_style, "text-align: ", align, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -0400102 }
103 if (!is.null(font_size)) {
Hao Zhu6f362bb2017-10-23 23:21:38 -0400104 if (is.numeric(font_size)) font_size <- paste0(font_size, "px")
Hao Zhuce5ee412017-10-23 01:14:38 -0400105 cell_style <- paste0(cell_style, "font-size: ", font_size, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -0400106 }
Hao Zhu6f362bb2017-10-23 23:21:38 -0400107
108 # favor popover over tooltip
109 if (!is.null(popover)) {
110 if (class(popover) != "ke_popover") popover <- spec_popover(popover)
111 tooltip_n_popover <- popover
112 } else if (!is.null(tooltip)) {
113 if (class(tooltip) != "ke_tooltip") tooltip <- spec_tooltip(tooltip)
114 tooltip_n_popover <- tooltip
115 } else {
116 tooltip_n_popover <- NULL
Hao Zhu9ce317e2017-10-12 18:19:55 -0400117 }
Hao Zhu6f362bb2017-10-23 23:21:38 -0400118
119 if (!is.null(link)) {
120 x <- paste0('<a href="', link, '" style="', cell_style, '" ',
121 tooltip_n_popover, '>', x, '</a>')
122 } else {
123 x <- paste0('<span style="', cell_style, '" ',
124 tooltip_n_popover, '>', x, '</span>')
125 }
126
127 # if (!is.null(link)) {
128 # x <- paste0('<a href="', link, '" style="', cell_style, '" ',
129 # tooltip_n_popover, '>', x, '</a>')
130 # } else if (!is.null(tooltip_n_popover)) {
131 # x <- paste0('<span style="', cell_style, '" ',
132 # tooltip_n_popover, '>', x, '</span>')
133 # } else {
134 #
135 # }
136
Hao Zhuce5ee412017-10-23 01:14:38 -0400137
138 if (!is.null(angle)) {
139 rotate_css <- paste0("-webkit-transform: rotate(", angle,
140 "deg); -moz-transform: rotate(", angle,
141 "deg); -ms-transform: rotate(", angle,
142 "deg); -o-transform: rotate(", angle,
143 "deg); transform: rotate(", angle,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400144 "deg); display: inline-block; ")
145 x <- paste0('<span style="', rotate_css, '">', x, '</span>')
Hao Zhuce5ee412017-10-23 01:14:38 -0400146 }
Hao Zhu6f362bb2017-10-23 23:21:38 -0400147
148 return(x)
Hao Zhubacd2f32017-10-11 14:06:36 -0400149}
150
151cell_spec_latex <- function(x, bold, italic, monospace,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400152 color, background, align, font_size, angle, escape,
Hao Zhuce5ee412017-10-23 01:14:38 -0400153 latex_background_in_cell) {
Hao Zhu6f362bb2017-10-23 23:21:38 -0400154 if (escape) x <- escape_latex(x)
Hao Zhubacd2f32017-10-11 14:06:36 -0400155 if (bold) x <- paste0("\\bfseries{", x, "}")
Hao Zhuce5ee412017-10-23 01:14:38 -0400156 if (italic) x <- paste0("\\em{", x, "}")
Hao Zhubacd2f32017-10-11 14:06:36 -0400157 if (monospace) x <- paste0("\\ttfamily{", x, "}")
Hao Zhu9ce317e2017-10-12 18:19:55 -0400158 if (!is.null(color)) {
159 color <- latex_color(color)
160 x <- paste0("\\textcolor", color, "{", x, "}")
161 }
162 if (!is.null(background)) {
163 background <- latex_color(background)
Hao Zhuce5ee412017-10-23 01:14:38 -0400164 background_env <- ifelse(latex_background_in_cell, "cellcolor", "colorbox")
165 x <- paste0("\\", background_env, background, "{", x, "}")
Hao Zhu457acb42017-10-14 17:37:02 -0400166 }
Hao Zhuce5ee412017-10-23 01:14:38 -0400167 if (!is.null(font_size)) {
Hao Zhu6f362bb2017-10-23 23:21:38 -0400168 x <- paste0("\\bgroup\\fontsize{", font_size, "}{", as.numeric(font_size) + 2,
169 "}\\selectfont ", x, "\\egroup{}")
Hao Zhuce5ee412017-10-23 01:14:38 -0400170 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400171 if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}")
Hao Zhuce5ee412017-10-23 01:14:38 -0400172 if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}")
Hao Zhubacd2f32017-10-11 14:06:36 -0400173 return(x)
174}
Hao Zhuce5ee412017-10-23 01:14:38 -0400175
176#' @rdname cell_spec
177#' @export
178text_spec <- function(x, format,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400179 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhuce5ee412017-10-23 01:14:38 -0400180 color = NULL, background = NULL,
181 align = NULL, font_size = NULL, angle = NULL,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400182 tooltip = NULL, popover = NULL, link = NULL,
183 escape = TRUE, background_as_tile = TRUE,
Hao Zhuce5ee412017-10-23 01:14:38 -0400184 latex_background_in_cell = FALSE) {
185 cell_spec(x, format, bold, italic, monospace, color, background, align,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400186 font_size, angle, tooltip, popover, link, escape, background_as_tile,
Hao Zhuce5ee412017-10-23 01:14:38 -0400187 latex_background_in_cell)
188}