Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 1 | #' Specify Cell format |
| 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()`. |
| 8 | #' @param bold A T/F value to control whether the text of the selected column |
| 9 | #' need to be bolded. |
| 10 | #' @param italic A T/F value to control whether the text of the selected column |
| 11 | #' need to be emphasized. |
| 12 | #' @param monospace A T/F value to control whether the text of the selected column |
| 13 | #' need to be monospaced (verbatim) |
| 14 | #' @param color A character string for column text color. Here please pay |
| 15 | #' attention to the differences in color codes between HTML and LaTeX. |
| 16 | #' @param background A character string for column background color. Here please |
| 17 | #' pay attention to the differences in color codes between HTML and LaTeX. |
| 18 | #' @param align A character string for cell alignment. For HTML, possible values could |
| 19 | #' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit` |
| 20 | #' while for LaTeX, you can only choose from `l`, `c` & `r`. |
| 21 | #' @param font_size Only if you want to specify font size locally in HTML. |
| 22 | #' This feature is not available in LaTeX |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 23 | #' @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] | 24 | #' @param tooltip A vector of strings to be displayed as tooltip. |
| 25 | #' Of course, this feature is only available in HTML. |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 26 | #' @param background_as_tile T/F value indicating if you want to have round |
| 27 | #' cornered tile as background. |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 28 | #' |
| 29 | #' @export |
| 30 | cell_spec <- function(x, format, |
| 31 | bold = F, italic = F, monospace = F, |
| 32 | color = NULL, background = NULL, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 33 | align = NULL, font_size = NULL, angle = NULL, |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 34 | tooltip = NULL, background_as_tile = TRUE) { |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 35 | |
| 36 | if (missing(format) || is.null(format)) format = getOption('knitr.table.format') |
| 37 | if (is.null(format)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 38 | message("Setting cell_spec format as html") |
| 39 | format <- "html" |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | if (tolower(format) == "html") { |
| 43 | return(cell_spec_html(x, bold, italic, monospace, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 44 | color, background, align, font_size, angle, |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 45 | tooltip, background_as_tile)) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 46 | } |
| 47 | if (tolower(format) == "latex") { |
| 48 | return(cell_spec_latex(x, bold, italic, monospace, |
| 49 | color, background, align, angle)) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | cell_spec_html <- function(x, bold, italic, monospace, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 54 | color, background, align, font_size, angle, |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 55 | tooltip, background_as_tile) { |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 56 | cell_style <- NULL |
| 57 | if (bold) cell_style <- paste(cell_style,"font-weight: bold;") |
| 58 | if (italic) cell_style <- paste(cell_style, "font-style: italic;") |
| 59 | if (monospace) cell_style <- paste(cell_style, "font-family: monospace;") |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 60 | if (!is.null(color)) { |
| 61 | cell_style <- paste0(cell_style, "color: ", html_color(color), ";") |
| 62 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 63 | if (!is.null(background)) { |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 64 | cell_style <- paste0( |
| 65 | cell_style, |
| 66 | ifelse(background_as_tile, "border-radius: 4px; ", ""), |
| 67 | "padding-right: 4px; padding-left: 4px; ", |
| 68 | "background-color: ", html_color(background), ";" |
| 69 | ) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 70 | } |
| 71 | if (!is.null(align)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 72 | cell_style <- paste0(cell_style, "text-align: ", align, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 73 | } |
| 74 | if (!is.null(font_size)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 75 | cell_style <- paste0(cell_style, "font-size: ", font_size, "px;") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 76 | } |
| 77 | if (!is.null(angle)) { |
| 78 | cell_style <- paste0(cell_style, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 79 | "-webkit-transform: rotate(", angle, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 80 | "deg); -moz-transform: rotate(", angle, |
| 81 | "deg); -ms-transform: rotate(", angle, |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 82 | "deg); -o-transform: rotate(", angle, |
| 83 | "deg); transform: rotate(", angle, |
| 84 | "deg);") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 85 | } |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 86 | |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 87 | if (!is.null(tooltip)) { |
| 88 | tooltip <- gsub("\n", "
", tooltip) |
| 89 | tooltip <- paste0("data-toggle='tooltip' title='", tooltip, "'") |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 90 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 91 | out <- paste0( |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 92 | '<div style="', cell_style, '"', tooltip, '>', x, '</div>' |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 93 | ) |
| 94 | return(out) |
| 95 | } |
| 96 | |
| 97 | cell_spec_latex <- function(x, bold, italic, monospace, |
| 98 | color, background, align, angle) { |
| 99 | if (bold) x <- paste0("\\bfseries{", x, "}") |
| 100 | if (italic) x <-paste0("\\em{", x, "}") |
| 101 | if (monospace) x <- paste0("\\ttfamily{", x, "}") |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 102 | if (!is.null(color)) { |
| 103 | color <- latex_color(color) |
| 104 | x <- paste0("\\textcolor", color, "{", x, "}") |
| 105 | } |
| 106 | if (!is.null(background)) { |
| 107 | background <- latex_color(background) |
| 108 | x <- paste0("\\cellcolor", background, "{", x, "}") |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 109 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 110 | if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}") |
| 111 | if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}") |
| 112 | return(x) |
| 113 | } |