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`. |
| 21 | #' @param font_size j |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 22 | #' @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] | 23 | #' @param tooltip A vector of strings to be displayed as tooltip. |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 24 | #' Obviously, this feature is only available in HTML. |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 25 | #' @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^] | 26 | #' cornered tile as background in HTML. |
| 27 | #' @param latex_background_in_cell T/F value. It only takes effect in LaTeX |
| 28 | #' when `background` provided, Default value is `TRUE`. If it's `TRUE`, the |
| 29 | #' background only works in a table cell. If it's `FALSE`, it works outside of a |
| 30 | #' table environment. |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 31 | #' |
| 32 | #' @export |
| 33 | cell_spec <- function(x, format, |
| 34 | bold = F, italic = F, monospace = F, |
| 35 | color = NULL, background = NULL, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 36 | align = NULL, font_size = NULL, angle = NULL, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 37 | tooltip = NULL, background_as_tile = TRUE, |
| 38 | latex_background_in_cell = TRUE) { |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 39 | |
| 40 | if (missing(format) || is.null(format)) format = getOption('knitr.table.format') |
| 41 | if (is.null(format)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 42 | message("Setting cell_spec format as html") |
| 43 | format <- "html" |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | if (tolower(format) == "html") { |
| 47 | return(cell_spec_html(x, bold, italic, monospace, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 48 | color, background, align, font_size, angle, |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 49 | tooltip, background_as_tile)) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 50 | } |
| 51 | if (tolower(format) == "latex") { |
| 52 | return(cell_spec_latex(x, bold, italic, monospace, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 53 | color, background, align, font_size, angle, |
| 54 | latex_background_in_cell)) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | cell_spec_html <- function(x, bold, italic, monospace, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 59 | color, background, align, font_size, angle, |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 60 | tooltip, background_as_tile) { |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 61 | cell_style <- NULL |
| 62 | if (bold) cell_style <- paste(cell_style,"font-weight: bold;") |
| 63 | if (italic) cell_style <- paste(cell_style, "font-style: italic;") |
| 64 | if (monospace) cell_style <- paste(cell_style, "font-family: monospace;") |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 65 | if (!is.null(color)) { |
| 66 | cell_style <- paste0(cell_style, "color: ", html_color(color), ";") |
| 67 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 68 | if (!is.null(background)) { |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 69 | cell_style <- paste0( |
| 70 | cell_style, |
| 71 | ifelse(background_as_tile, "border-radius: 4px; ", ""), |
| 72 | "padding-right: 4px; padding-left: 4px; ", |
| 73 | "background-color: ", html_color(background), ";" |
| 74 | ) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 75 | } |
| 76 | if (!is.null(align)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 77 | cell_style <- paste0(cell_style, "text-align: ", align, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 78 | } |
| 79 | if (!is.null(font_size)) { |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 80 | if (is.numeric) font_size <- paste0(font_size, "px") |
| 81 | cell_style <- paste0(cell_style, "font-size: ", font_size, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 82 | } |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 83 | if (!is.null(tooltip)) { |
| 84 | tooltip <- gsub("\n", "
", tooltip) |
| 85 | tooltip <- paste0("data-toggle='tooltip' title='", tooltip, "'") |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 86 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 87 | out <- paste0( |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 88 | '<span style="', cell_style, '"', tooltip, '>', x, '</span>' |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 89 | ) |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 90 | |
| 91 | if (!is.null(angle)) { |
| 92 | rotate_css <- paste0("-webkit-transform: rotate(", angle, |
| 93 | "deg); -moz-transform: rotate(", angle, |
| 94 | "deg); -ms-transform: rotate(", angle, |
| 95 | "deg); -o-transform: rotate(", angle, |
| 96 | "deg); transform: rotate(", angle, |
| 97 | "deg);") |
| 98 | out <- paste0('<div style="', rotate_css, '">', out, '</div>') |
| 99 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 100 | return(out) |
| 101 | } |
| 102 | |
| 103 | cell_spec_latex <- function(x, bold, italic, monospace, |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 104 | color, background, align, font_size, angle, |
| 105 | latex_background_in_cell) { |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 106 | if (bold) x <- paste0("\\bfseries{", x, "}") |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 107 | if (italic) x <- paste0("\\em{", x, "}") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 108 | if (monospace) x <- paste0("\\ttfamily{", x, "}") |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 109 | if (!is.null(color)) { |
| 110 | color <- latex_color(color) |
| 111 | x <- paste0("\\textcolor", color, "{", x, "}") |
| 112 | } |
| 113 | if (!is.null(background)) { |
| 114 | background <- latex_color(background) |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 115 | background_env <- ifelse(latex_background_in_cell, "cellcolor", "colorbox") |
| 116 | x <- paste0("\\", background_env, background, "{", x, "}") |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 117 | } |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 118 | if (!is.null(font_size)) { |
| 119 | x <- paste0("\\begingroup\\fontsize{", font_size, "}{", as.numeric(font_size) + 2, |
| 120 | "}\\selectfont ", x, "\\endgroup") |
| 121 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 122 | if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}") |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 123 | if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 124 | return(x) |
| 125 | } |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 126 | |
| 127 | #' @rdname cell_spec |
| 128 | #' @export |
| 129 | text_spec <- function(x, format, |
| 130 | bold = F, italic = F, monospace = F, |
| 131 | color = NULL, background = NULL, |
| 132 | align = NULL, font_size = NULL, angle = NULL, |
| 133 | tooltip = NULL, background_as_tile = TRUE, |
| 134 | latex_background_in_cell = FALSE) { |
| 135 | cell_spec(x, format, bold, italic, monospace, color, background, align, |
| 136 | font_size, angle, tooltip, background_as_tile, |
| 137 | latex_background_in_cell) |
| 138 | } |