Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 1 | #' Specify Cell/Text format |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 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()`. |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 8 | #' @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 Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 11 | #' @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 Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 15 | #' @param color A character string for text color. Here please pay |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 16 | #' attention to the differences in color codes between HTML and LaTeX. |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 17 | #' @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 Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 25 | #' @param font_size A numeric input for font size. For HTML, you can also use |
Hao Zhu | 8b32b19 | 2017-10-24 14:51:48 -0400 | [diff] [blame] | 26 | #' options including `xx-small`, `x-small`, `small`, `medium`, `large`, |
| 27 | #' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`. |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 28 | #' @param angle 0-360, degree that the text will rotate. Can be a vector. |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 29 | #' @param tooltip A vector of strings to be displayed as tooltip. |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 30 | #' 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 Zhu | dcbdff6 | 2020-08-10 13:29:53 -0400 | [diff] [blame] | 39 | #' @param new_tab T/F for whether to open up the new link in new tab. |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 40 | #' @param extra_css Extra css text to be passed into the cell |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 41 | #' @param escape T/F value showing whether special characters should be escaped. |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 42 | #' @param background_as_tile T/F value indicating if you want to have round |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 43 | #' cornered tile as background in HTML. |
| 44 | #' @param latex_background_in_cell T/F value. It only takes effect in LaTeX |
| 45 | #' when `background` provided, Default value is `TRUE`. If it's `TRUE`, the |
| 46 | #' background only works in a table cell. If it's `FALSE`, it works outside of a |
| 47 | #' table environment. |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 48 | #' |
| 49 | #' @export |
| 50 | cell_spec <- function(x, format, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 51 | bold = FALSE, italic = FALSE, monospace = FALSE, |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 52 | underline = FALSE, strikeout = FALSE, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 53 | color = NULL, background = NULL, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 54 | align = NULL, font_size = NULL, angle = NULL, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 55 | tooltip = NULL, popover = NULL, link = NULL, |
Hao Zhu | dcbdff6 | 2020-08-10 13:29:53 -0400 | [diff] [blame] | 56 | new_tab = FALSE, extra_css = NULL, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 57 | escape = TRUE, |
| 58 | background_as_tile = TRUE, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 59 | latex_background_in_cell = TRUE) { |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 60 | |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 61 | if (missing(format) || is.null(format)) { |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 62 | if (knitr::is_latex_output()) { |
| 63 | format <- "latex" |
| 64 | } else { |
| 65 | format <- "html" |
| 66 | } |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 67 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 68 | if (tolower(format) == "html") { |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 69 | return(cell_spec_html(x, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 70 | color, background, align, font_size, angle, |
Hao Zhu | dcbdff6 | 2020-08-10 13:29:53 -0400 | [diff] [blame] | 71 | tooltip, popover, link, new_tab, extra_css, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 72 | escape, background_as_tile)) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 73 | } |
| 74 | if (tolower(format) == "latex") { |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 75 | return(cell_spec_latex(x, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 76 | color, background, align, font_size, angle, escape, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 77 | latex_background_in_cell)) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 81 | cell_spec_html <- function(x, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 82 | color, background, align, font_size, angle, |
Hao Zhu | dcbdff6 | 2020-08-10 13:29:53 -0400 | [diff] [blame] | 83 | tooltip, popover, link, new_tab, extra_css, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 84 | escape, background_as_tile) { |
| 85 | if (escape) x <- escape_html(x) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 86 | cell_style <- NULL |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 87 | 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 Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 91 | 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 Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 95 | if (!is.null(color)) { |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 96 | cell_style <- paste0(cell_style, "color: ", html_color(color), |
| 97 | " !important;") |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 98 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 99 | if (!is.null(background)) { |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 100 | cell_style <- paste0( |
| 101 | cell_style, |
| 102 | ifelse(background_as_tile, "border-radius: 4px; ", ""), |
| 103 | "padding-right: 4px; padding-left: 4px; ", |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 104 | "background-color: ", html_color(background), " !important;" |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 105 | ) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 106 | } |
Hao Zhu | b1de967 | 2018-01-08 16:29:24 -0500 | [diff] [blame] | 107 | if (!is.null(extra_css)) { |
| 108 | cell_style <- paste0(cell_style, extra_css) |
| 109 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 110 | if (!is.null(align)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 111 | cell_style <- paste0(cell_style, "text-align: ", align, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 112 | } |
| 113 | if (!is.null(font_size)) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 114 | if (is.numeric(font_size)) font_size <- paste0(font_size, "px") |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 115 | cell_style <- paste0(cell_style, "font-size: ", font_size, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 116 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 117 | |
| 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 Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 127 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 128 | |
| 129 | if (!is.null(link)) { |
Hao Zhu | dcbdff6 | 2020-08-10 13:29:53 -0400 | [diff] [blame] | 130 | if (new_tab) { |
| 131 | target_blank = 'target="_blank" ' |
| 132 | } else { |
| 133 | target_blank = NULL |
| 134 | } |
| 135 | x <- paste0('<a href="', link, '" style="', cell_style, '" ', target_blank, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 136 | tooltip_n_popover, '>', x, '</a>') |
| 137 | } else { |
| 138 | x <- paste0('<span style="', cell_style, '" ', |
| 139 | tooltip_n_popover, '>', x, '</span>') |
| 140 | } |
| 141 | |
| 142 | # if (!is.null(link)) { |
| 143 | # x <- paste0('<a href="', link, '" style="', cell_style, '" ', |
| 144 | # tooltip_n_popover, '>', x, '</a>') |
| 145 | # } else if (!is.null(tooltip_n_popover)) { |
| 146 | # x <- paste0('<span style="', cell_style, '" ', |
| 147 | # tooltip_n_popover, '>', x, '</span>') |
| 148 | # } else { |
| 149 | # |
| 150 | # } |
| 151 | |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 152 | |
| 153 | if (!is.null(angle)) { |
| 154 | rotate_css <- paste0("-webkit-transform: rotate(", angle, |
| 155 | "deg); -moz-transform: rotate(", angle, |
| 156 | "deg); -ms-transform: rotate(", angle, |
| 157 | "deg); -o-transform: rotate(", angle, |
| 158 | "deg); transform: rotate(", angle, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 159 | "deg); display: inline-block; ") |
| 160 | x <- paste0('<span style="', rotate_css, '">', x, '</span>') |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 161 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 162 | |
| 163 | return(x) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 166 | cell_spec_latex <- function(x, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 167 | color, background, align, font_size, angle, escape, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 168 | latex_background_in_cell) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 169 | if (escape) x <- escape_latex(x) |
Hao Zhu | ef0c830 | 2018-01-12 13:30:20 -0500 | [diff] [blame] | 170 | x <- sprintf(ifelse(bold, "\\textbf{%s}", "%s"), x) |
| 171 | x <- sprintf(ifelse(italic, "\\em{%s}", "%s"), x) |
| 172 | x <- sprintf(ifelse(monospace, "\\ttfamily{%s}", "%s"), x) |
| 173 | x <- sprintf(ifelse(underline, "\\underline{%s}", "%s"), x) |
| 174 | x <- sprintf(ifelse(strikeout, "\\sout{%s}", "%s"), x) |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 175 | if (!is.null(color)) { |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 176 | color <- latex_color(color, escape = FALSE) |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 177 | x <- paste0("\\textcolor", color, "{", x, "}") |
| 178 | } |
| 179 | if (!is.null(background)) { |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame^] | 180 | background <- latex_color(background, escape = FALSE) |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 181 | background_env <- ifelse(latex_background_in_cell, "cellcolor", "colorbox") |
| 182 | x <- paste0("\\", background_env, background, "{", x, "}") |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 183 | } |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 184 | if (!is.null(font_size)) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 185 | x <- paste0("\\bgroup\\fontsize{", font_size, "}{", as.numeric(font_size) + 2, |
| 186 | "}\\selectfont ", x, "\\egroup{}") |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 187 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 188 | if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}") |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 189 | if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 190 | return(x) |
| 191 | } |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 192 | |
| 193 | #' @rdname cell_spec |
| 194 | #' @export |
| 195 | text_spec <- function(x, format, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 196 | bold = FALSE, italic = FALSE, monospace = FALSE, |
Hao Zhu | d9fc3d4 | 2018-01-12 13:38:14 -0500 | [diff] [blame] | 197 | underline = FALSE, strikeout = FALSE, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 198 | color = NULL, background = NULL, |
| 199 | align = NULL, font_size = NULL, angle = NULL, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 200 | tooltip = NULL, popover = NULL, link = NULL, |
Hao Zhu | dcbdff6 | 2020-08-10 13:29:53 -0400 | [diff] [blame] | 201 | new_tab = FALSE, extra_css = NULL, |
Hao Zhu | c78f8d0 | 2018-04-10 20:05:05 -0400 | [diff] [blame] | 202 | escape = TRUE, |
| 203 | background_as_tile = TRUE, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 204 | latex_background_in_cell = FALSE) { |
Hao Zhu | d9fc3d4 | 2018-01-12 13:38:14 -0500 | [diff] [blame] | 205 | cell_spec(x, format, bold, italic, monospace, underline, strikeout, |
| 206 | color, background, align, |
Hao Zhu | dcbdff6 | 2020-08-10 13:29:53 -0400 | [diff] [blame] | 207 | font_size, angle, tooltip, popover, link, new_tab, |
Hao Zhu | c78f8d0 | 2018-04-10 20:05:05 -0400 | [diff] [blame] | 208 | extra_css, escape, background_as_tile, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 209 | latex_background_in_cell) |
| 210 | } |