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) |
| 11 | #' @param color A character string for text color. Here please pay |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 12 | #' attention to the differences in color codes between HTML and LaTeX. |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 13 | #' @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 Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 21 | #' @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] | 22 | #' options including `xx-small`, `x-small`, `small`, `medium`, `large`, |
| 23 | #' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`. |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 24 | #' @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] | 25 | #' @param tooltip A vector of strings to be displayed as tooltip. |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 26 | #' 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. |
| 35 | #' @param escape T/F value showing whether special characters should be escaped. |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 36 | #' @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] | 37 | #' cornered tile as background in HTML. |
| 38 | #' @param latex_background_in_cell T/F value. It only takes effect in LaTeX |
| 39 | #' when `background` provided, Default value is `TRUE`. If it's `TRUE`, the |
| 40 | #' background only works in a table cell. If it's `FALSE`, it works outside of a |
| 41 | #' table environment. |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 42 | #' |
| 43 | #' @export |
| 44 | cell_spec <- function(x, format, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 45 | bold = FALSE, italic = FALSE, monospace = FALSE, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 46 | color = NULL, background = NULL, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 47 | align = NULL, font_size = NULL, angle = NULL, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 48 | tooltip = NULL, popover = NULL, link = NULL, |
| 49 | escape = TRUE, |
| 50 | background_as_tile = TRUE, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 51 | latex_background_in_cell = TRUE) { |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 52 | |
| 53 | if (missing(format) || is.null(format)) format = getOption('knitr.table.format') |
| 54 | if (is.null(format)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 55 | message("Setting cell_spec format as html") |
| 56 | format <- "html" |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | if (tolower(format) == "html") { |
| 60 | return(cell_spec_html(x, bold, italic, monospace, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 61 | color, background, align, font_size, angle, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 62 | tooltip, popover, link, |
| 63 | escape, background_as_tile)) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 64 | } |
| 65 | if (tolower(format) == "latex") { |
| 66 | return(cell_spec_latex(x, bold, italic, monospace, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 67 | color, background, align, font_size, angle, escape, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 68 | latex_background_in_cell)) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | cell_spec_html <- function(x, bold, italic, monospace, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 73 | color, background, align, font_size, angle, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 74 | tooltip, popover, link, |
| 75 | escape, background_as_tile) { |
| 76 | if (escape) x <- escape_html(x) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 77 | cell_style <- NULL |
| 78 | if (bold) cell_style <- paste(cell_style,"font-weight: bold;") |
| 79 | if (italic) cell_style <- paste(cell_style, "font-style: italic;") |
| 80 | if (monospace) cell_style <- paste(cell_style, "font-family: monospace;") |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 81 | if (!is.null(color)) { |
| 82 | cell_style <- paste0(cell_style, "color: ", html_color(color), ";") |
| 83 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 84 | if (!is.null(background)) { |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 85 | cell_style <- paste0( |
| 86 | cell_style, |
| 87 | ifelse(background_as_tile, "border-radius: 4px; ", ""), |
| 88 | "padding-right: 4px; padding-left: 4px; ", |
| 89 | "background-color: ", html_color(background), ";" |
| 90 | ) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 91 | } |
| 92 | if (!is.null(align)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 93 | cell_style <- paste0(cell_style, "text-align: ", align, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 94 | } |
| 95 | if (!is.null(font_size)) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 96 | if (is.numeric(font_size)) font_size <- paste0(font_size, "px") |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 97 | cell_style <- paste0(cell_style, "font-size: ", font_size, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 98 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 99 | |
| 100 | # favor popover over tooltip |
| 101 | if (!is.null(popover)) { |
| 102 | if (class(popover) != "ke_popover") popover <- spec_popover(popover) |
| 103 | tooltip_n_popover <- popover |
| 104 | } else if (!is.null(tooltip)) { |
| 105 | if (class(tooltip) != "ke_tooltip") tooltip <- spec_tooltip(tooltip) |
| 106 | tooltip_n_popover <- tooltip |
| 107 | } else { |
| 108 | tooltip_n_popover <- NULL |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 109 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 110 | |
| 111 | if (!is.null(link)) { |
| 112 | x <- paste0('<a href="', link, '" style="', cell_style, '" ', |
| 113 | tooltip_n_popover, '>', x, '</a>') |
| 114 | } else { |
| 115 | x <- paste0('<span style="', cell_style, '" ', |
| 116 | tooltip_n_popover, '>', x, '</span>') |
| 117 | } |
| 118 | |
| 119 | # if (!is.null(link)) { |
| 120 | # x <- paste0('<a href="', link, '" style="', cell_style, '" ', |
| 121 | # tooltip_n_popover, '>', x, '</a>') |
| 122 | # } else if (!is.null(tooltip_n_popover)) { |
| 123 | # x <- paste0('<span style="', cell_style, '" ', |
| 124 | # tooltip_n_popover, '>', x, '</span>') |
| 125 | # } else { |
| 126 | # |
| 127 | # } |
| 128 | |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 129 | |
| 130 | if (!is.null(angle)) { |
| 131 | rotate_css <- paste0("-webkit-transform: rotate(", angle, |
| 132 | "deg); -moz-transform: rotate(", angle, |
| 133 | "deg); -ms-transform: rotate(", angle, |
| 134 | "deg); -o-transform: rotate(", angle, |
| 135 | "deg); transform: rotate(", angle, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 136 | "deg); display: inline-block; ") |
| 137 | x <- paste0('<span style="', rotate_css, '">', x, '</span>') |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 138 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 139 | |
| 140 | return(x) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | cell_spec_latex <- function(x, bold, italic, monospace, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 144 | color, background, align, font_size, angle, escape, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 145 | latex_background_in_cell) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 146 | if (escape) x <- escape_latex(x) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 147 | if (bold) x <- paste0("\\bfseries{", x, "}") |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 148 | if (italic) x <- paste0("\\em{", x, "}") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 149 | if (monospace) x <- paste0("\\ttfamily{", x, "}") |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 150 | if (!is.null(color)) { |
| 151 | color <- latex_color(color) |
| 152 | x <- paste0("\\textcolor", color, "{", x, "}") |
| 153 | } |
| 154 | if (!is.null(background)) { |
| 155 | background <- latex_color(background) |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 156 | background_env <- ifelse(latex_background_in_cell, "cellcolor", "colorbox") |
| 157 | x <- paste0("\\", background_env, background, "{", x, "}") |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 158 | } |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 159 | if (!is.null(font_size)) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 160 | x <- paste0("\\bgroup\\fontsize{", font_size, "}{", as.numeric(font_size) + 2, |
| 161 | "}\\selectfont ", x, "\\egroup{}") |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 162 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 163 | if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}") |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 164 | if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 165 | return(x) |
| 166 | } |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 167 | |
| 168 | #' @rdname cell_spec |
| 169 | #' @export |
| 170 | text_spec <- function(x, format, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 171 | bold = FALSE, italic = FALSE, monospace = FALSE, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 172 | color = NULL, background = NULL, |
| 173 | align = NULL, font_size = NULL, angle = NULL, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 174 | tooltip = NULL, popover = NULL, link = NULL, |
| 175 | escape = TRUE, background_as_tile = TRUE, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 176 | latex_background_in_cell = FALSE) { |
| 177 | cell_spec(x, format, bold, italic, monospace, color, background, align, |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 178 | font_size, angle, tooltip, popover, link, escape, background_as_tile, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame] | 179 | latex_background_in_cell) |
| 180 | } |