blob: faaf85bcbe598dcbde45acd202c70279116266b3 [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)
Hao Zhuef0c8302018-01-12 13:30:20 -050011#' @param underline A T/F value to control whether the text of the selected row
12#' need to be underlined
13#' @param strikeout A T/F value to control whether the text of the selected row
14#' need to be stricked out.
Hao Zhuce5ee412017-10-23 01:14:38 -040015#' @param color A character string for text color. Here please pay
Hao Zhubacd2f32017-10-11 14:06:36 -040016#' attention to the differences in color codes between HTML and LaTeX.
Hao Zhuce5ee412017-10-23 01:14:38 -040017#' @param background A character string for background color. Here please
18#' pay attention to the differences in color codes between HTML and LaTeX. Also
19#' note that in HTML, background defined in cell_spec won't cover the whole
20#' cell.
21#' @param align A character string for cell alignment. For HTML, possible
22#' values could be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`,
23#' `initial` and `inherit` while for LaTeX, you can only choose
24#' from `l`, `c` & `r`.
Hao Zhu6f362bb2017-10-23 23:21:38 -040025#' @param font_size A numeric input for font size. For HTML, you can also use
Hao Zhu8b32b192017-10-24 14:51:48 -040026#' options including `xx-small`, `x-small`, `small`, `medium`, `large`,
27#' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`.
Hao Zhu9ce317e2017-10-12 18:19:55 -040028#' @param angle 0-360, degree that the text will rotate. Can be a vector.
Hao Zhu064990d2017-10-17 18:08:42 -040029#' @param tooltip A vector of strings to be displayed as tooltip.
Hao Zhu6f362bb2017-10-23 23:21:38 -040030#' Obviously, this feature is only available in HTML. Read the package
31#' vignette to see how to use bootstrap tooltip css to improve the loading
32#' speed and look.
33#' @param popover Similar with tooltip but can hold more contents. The best way
34#' to build a popover is through `spec_popover()`. If you only provide a text
35#' string, it will be used as content. Note that You have to enable this
36#' bootstrap module manually. Read the package vignette to see how.
37#' @param link A vector of strings for url links. Can be used together with
38#' tooltip and popover.
Hao Zhub1de9672018-01-08 16:29:24 -050039#' @param extra_css Extra css text to be passed into the cell
Hao Zhu6f362bb2017-10-23 23:21:38 -040040#' @param escape T/F value showing whether special characters should be escaped.
Hao Zhu457acb42017-10-14 17:37:02 -040041#' @param background_as_tile T/F value indicating if you want to have round
Hao Zhuce5ee412017-10-23 01:14:38 -040042#' cornered tile as background in HTML.
43#' @param latex_background_in_cell T/F value. It only takes effect in LaTeX
44#' when `background` provided, Default value is `TRUE`. If it's `TRUE`, the
45#' background only works in a table cell. If it's `FALSE`, it works outside of a
46#' table environment.
Hao Zhubacd2f32017-10-11 14:06:36 -040047#'
48#' @export
49cell_spec <- function(x, format,
Hao Zhu6f362bb2017-10-23 23:21:38 -040050 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhuef0c8302018-01-12 13:30:20 -050051 underline = FALSE, strikeout = FALSE,
Hao Zhubacd2f32017-10-11 14:06:36 -040052 color = NULL, background = NULL,
Hao Zhu9ce317e2017-10-12 18:19:55 -040053 align = NULL, font_size = NULL, angle = NULL,
Hao Zhu6f362bb2017-10-23 23:21:38 -040054 tooltip = NULL, popover = NULL, link = NULL,
Hao Zhub1de9672018-01-08 16:29:24 -050055 extra_css = NULL,
Hao Zhu6f362bb2017-10-23 23:21:38 -040056 escape = TRUE,
57 background_as_tile = TRUE,
Hao Zhuce5ee412017-10-23 01:14:38 -040058 latex_background_in_cell = TRUE) {
Hao Zhubacd2f32017-10-11 14:06:36 -040059
Hao Zhu1ac13ad2018-01-08 16:12:24 -050060 if (missing(format) || is.null(format)) {
61 format <- getOption('knitr.table.format')
62 }
Hao Zhubacd2f32017-10-11 14:06:36 -040063 if (is.null(format)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -040064 message("Setting cell_spec format as html")
65 format <- "html"
Hao Zhubacd2f32017-10-11 14:06:36 -040066 }
67
68 if (tolower(format) == "html") {
Hao Zhuef0c8302018-01-12 13:30:20 -050069 return(cell_spec_html(x, bold, italic, monospace, underline, strikeout,
Hao Zhu9ce317e2017-10-12 18:19:55 -040070 color, background, align, font_size, angle,
Hao Zhub1de9672018-01-08 16:29:24 -050071 tooltip, popover, link, extra_css,
Hao Zhu6f362bb2017-10-23 23:21:38 -040072 escape, background_as_tile))
Hao Zhubacd2f32017-10-11 14:06:36 -040073 }
74 if (tolower(format) == "latex") {
Hao Zhuef0c8302018-01-12 13:30:20 -050075 return(cell_spec_latex(x, bold, italic, monospace, underline, strikeout,
Hao Zhu6f362bb2017-10-23 23:21:38 -040076 color, background, align, font_size, angle, escape,
Hao Zhuce5ee412017-10-23 01:14:38 -040077 latex_background_in_cell))
Hao Zhubacd2f32017-10-11 14:06:36 -040078 }
79}
80
Hao Zhuef0c8302018-01-12 13:30:20 -050081cell_spec_html <- function(x, bold, italic, monospace, underline, strikeout,
Hao Zhu9ce317e2017-10-12 18:19:55 -040082 color, background, align, font_size, angle,
Hao Zhub1de9672018-01-08 16:29:24 -050083 tooltip, popover, link, extra_css,
Hao Zhu6f362bb2017-10-23 23:21:38 -040084 escape, background_as_tile) {
85 if (escape) x <- escape_html(x)
Hao Zhubacd2f32017-10-11 14:06:36 -040086 cell_style <- NULL
Hao Zhu1ac13ad2018-01-08 16:12:24 -050087 cell_style <- paste(cell_style, ifelse(bold, "font-weight: bold;", ""))
88 cell_style <- paste(cell_style, ifelse(italic, "font-style: italic;", ""))
89 cell_style <- paste(cell_style,
90 ifelse(monospace, "font-family: monospace;", ""))
Hao Zhuef0c8302018-01-12 13:30:20 -050091 cell_style <- paste(cell_style,
92 ifelse(underline, "text-decoration: underline;", ""))
93 cell_style <- paste(cell_style,
94 ifelse(strikeout, "text-decoration: line-through;", ""))
Hao Zhu457acb42017-10-14 17:37:02 -040095 if (!is.null(color)) {
Hao Zhu72917f92019-03-15 18:41:42 -040096 cell_style <- paste0(cell_style, "color: ", html_color(color),
97 " !important;")
Hao Zhu457acb42017-10-14 17:37:02 -040098 }
Hao Zhubacd2f32017-10-11 14:06:36 -040099 if (!is.null(background)) {
Hao Zhu457acb42017-10-14 17:37:02 -0400100 cell_style <- paste0(
101 cell_style,
102 ifelse(background_as_tile, "border-radius: 4px; ", ""),
103 "padding-right: 4px; padding-left: 4px; ",
Hao Zhu72917f92019-03-15 18:41:42 -0400104 "background-color: ", html_color(background), " !important;"
Hao Zhu457acb42017-10-14 17:37:02 -0400105 )
Hao Zhubacd2f32017-10-11 14:06:36 -0400106 }
Hao Zhub1de9672018-01-08 16:29:24 -0500107 if (!is.null(extra_css)) {
108 cell_style <- paste0(cell_style, extra_css)
109 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400110 if (!is.null(align)) {
Hao Zhu9ce317e2017-10-12 18:19:55 -0400111 cell_style <- paste0(cell_style, "text-align: ", align, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -0400112 }
113 if (!is.null(font_size)) {
Hao Zhu6f362bb2017-10-23 23:21:38 -0400114 if (is.numeric(font_size)) font_size <- paste0(font_size, "px")
Hao Zhuce5ee412017-10-23 01:14:38 -0400115 cell_style <- paste0(cell_style, "font-size: ", font_size, ";")
Hao Zhubacd2f32017-10-11 14:06:36 -0400116 }
Hao Zhu6f362bb2017-10-23 23:21:38 -0400117
118 # favor popover over tooltip
119 if (!is.null(popover)) {
120 if (class(popover) != "ke_popover") popover <- spec_popover(popover)
121 tooltip_n_popover <- popover
122 } else if (!is.null(tooltip)) {
123 if (class(tooltip) != "ke_tooltip") tooltip <- spec_tooltip(tooltip)
124 tooltip_n_popover <- tooltip
125 } else {
126 tooltip_n_popover <- NULL
Hao Zhu9ce317e2017-10-12 18:19:55 -0400127 }
Hao Zhu6f362bb2017-10-23 23:21:38 -0400128
129 if (!is.null(link)) {
130 x <- paste0('<a href="', link, '" style="', cell_style, '" ',
131 tooltip_n_popover, '>', x, '</a>')
132 } else {
133 x <- paste0('<span style="', cell_style, '" ',
134 tooltip_n_popover, '>', x, '</span>')
135 }
136
137 # if (!is.null(link)) {
138 # x <- paste0('<a href="', link, '" style="', cell_style, '" ',
139 # tooltip_n_popover, '>', x, '</a>')
140 # } else if (!is.null(tooltip_n_popover)) {
141 # x <- paste0('<span style="', cell_style, '" ',
142 # tooltip_n_popover, '>', x, '</span>')
143 # } else {
144 #
145 # }
146
Hao Zhuce5ee412017-10-23 01:14:38 -0400147
148 if (!is.null(angle)) {
149 rotate_css <- paste0("-webkit-transform: rotate(", angle,
150 "deg); -moz-transform: rotate(", angle,
151 "deg); -ms-transform: rotate(", angle,
152 "deg); -o-transform: rotate(", angle,
153 "deg); transform: rotate(", angle,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400154 "deg); display: inline-block; ")
155 x <- paste0('<span style="', rotate_css, '">', x, '</span>')
Hao Zhuce5ee412017-10-23 01:14:38 -0400156 }
Hao Zhu6f362bb2017-10-23 23:21:38 -0400157
158 return(x)
Hao Zhubacd2f32017-10-11 14:06:36 -0400159}
160
Hao Zhuef0c8302018-01-12 13:30:20 -0500161cell_spec_latex <- function(x, bold, italic, monospace, underline, strikeout,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400162 color, background, align, font_size, angle, escape,
Hao Zhuce5ee412017-10-23 01:14:38 -0400163 latex_background_in_cell) {
Hao Zhu6f362bb2017-10-23 23:21:38 -0400164 if (escape) x <- escape_latex(x)
Hao Zhuef0c8302018-01-12 13:30:20 -0500165 x <- sprintf(ifelse(bold, "\\textbf{%s}", "%s"), x)
166 x <- sprintf(ifelse(italic, "\\em{%s}", "%s"), x)
167 x <- sprintf(ifelse(monospace, "\\ttfamily{%s}", "%s"), x)
168 x <- sprintf(ifelse(underline, "\\underline{%s}", "%s"), x)
169 x <- sprintf(ifelse(strikeout, "\\sout{%s}", "%s"), x)
Hao Zhu9ce317e2017-10-12 18:19:55 -0400170 if (!is.null(color)) {
171 color <- latex_color(color)
172 x <- paste0("\\textcolor", color, "{", x, "}")
173 }
174 if (!is.null(background)) {
175 background <- latex_color(background)
Hao Zhuce5ee412017-10-23 01:14:38 -0400176 background_env <- ifelse(latex_background_in_cell, "cellcolor", "colorbox")
177 x <- paste0("\\", background_env, background, "{", x, "}")
Hao Zhu457acb42017-10-14 17:37:02 -0400178 }
Hao Zhuce5ee412017-10-23 01:14:38 -0400179 if (!is.null(font_size)) {
Hao Zhu6f362bb2017-10-23 23:21:38 -0400180 x <- paste0("\\bgroup\\fontsize{", font_size, "}{", as.numeric(font_size) + 2,
181 "}\\selectfont ", x, "\\egroup{}")
Hao Zhuce5ee412017-10-23 01:14:38 -0400182 }
Hao Zhubacd2f32017-10-11 14:06:36 -0400183 if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}")
Hao Zhuce5ee412017-10-23 01:14:38 -0400184 if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}")
Hao Zhubacd2f32017-10-11 14:06:36 -0400185 return(x)
186}
Hao Zhuce5ee412017-10-23 01:14:38 -0400187
188#' @rdname cell_spec
189#' @export
190text_spec <- function(x, format,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400191 bold = FALSE, italic = FALSE, monospace = FALSE,
Hao Zhud9fc3d42018-01-12 13:38:14 -0500192 underline = FALSE, strikeout = FALSE,
Hao Zhuce5ee412017-10-23 01:14:38 -0400193 color = NULL, background = NULL,
194 align = NULL, font_size = NULL, angle = NULL,
Hao Zhu6f362bb2017-10-23 23:21:38 -0400195 tooltip = NULL, popover = NULL, link = NULL,
Hao Zhuc78f8d02018-04-10 20:05:05 -0400196 extra_css = NULL,
197 escape = TRUE,
198 background_as_tile = TRUE,
Hao Zhuce5ee412017-10-23 01:14:38 -0400199 latex_background_in_cell = FALSE) {
Hao Zhud9fc3d42018-01-12 13:38:14 -0500200 cell_spec(x, format, bold, italic, monospace, underline, strikeout,
201 color, background, align,
Hao Zhuc78f8d02018-04-10 20:05:05 -0400202 font_size, angle, tooltip, popover, link,
203 extra_css, escape, background_as_tile,
Hao Zhuce5ee412017-10-23 01:14:38 -0400204 latex_background_in_cell)
205}