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. |
| 24 | #' @param hover_message A vector of strings to be displayed as hover message. |
| 25 | #' Of course, this feature is nly available in HTML. |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 26 | #' |
| 27 | #' @export |
| 28 | cell_spec <- function(x, format, |
| 29 | bold = F, italic = F, monospace = F, |
| 30 | color = NULL, background = NULL, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 31 | align = NULL, font_size = NULL, angle = NULL, |
| 32 | hover_message = NULL) { |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 33 | |
| 34 | if (missing(format) || is.null(format)) format = getOption('knitr.table.format') |
| 35 | if (is.null(format)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 36 | message("Setting cell_spec format as html") |
| 37 | format <- "html" |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | if (tolower(format) == "html") { |
| 41 | return(cell_spec_html(x, bold, italic, monospace, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 42 | color, background, align, font_size, angle, |
| 43 | hover_message)) |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 44 | } |
| 45 | if (tolower(format) == "latex") { |
| 46 | return(cell_spec_latex(x, bold, italic, monospace, |
| 47 | color, background, align, angle)) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | cell_spec_html <- function(x, bold, italic, monospace, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 52 | color, background, align, font_size, angle, |
| 53 | hover_message) { |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 54 | cell_style <- NULL |
| 55 | if (bold) cell_style <- paste(cell_style,"font-weight: bold;") |
| 56 | if (italic) cell_style <- paste(cell_style, "font-style: italic;") |
| 57 | if (monospace) cell_style <- paste(cell_style, "font-family: monospace;") |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 58 | if (!is.null(color)) cell_style <- paste0(cell_style, "color: ", color, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 59 | if (!is.null(background)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 60 | cell_style <- paste0(cell_style, "border-radius: 4px; padding-right: 4px", |
| 61 | ";padding-left: 4px; background-color: ", background, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 62 | } |
| 63 | if (!is.null(align)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 64 | cell_style <- paste0(cell_style, "text-align: ", align, ";") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 65 | } |
| 66 | if (!is.null(font_size)) { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 67 | cell_style <- paste0(cell_style, "font-size: ", font_size, "px;") |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 68 | } |
| 69 | if (!is.null(angle)) { |
| 70 | cell_style <- paste0(cell_style, |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 71 | "-webkit-transform: rotate(", angle, |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 72 | "deg); -moz-transform: rotate(", angle, |
| 73 | "deg); -ms-transform: rotate(", angle, |
| 74 | "deg); -o-transform: rotate(", angle, "deg);") |
| 75 | } |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 76 | |
| 77 | if (!is.null(hover_message)) { |
| 78 | hover_message <- gsub("\n", "
", hover_message) |
| 79 | hover_message <- paste0("data-toggle='tooltip' title='", hover_message, "'") |
| 80 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 81 | out <- paste0( |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 82 | '<div style="', cell_style, '"', hover_message, '>', x, '</div>' |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 83 | ) |
| 84 | return(out) |
| 85 | } |
| 86 | |
| 87 | cell_spec_latex <- function(x, bold, italic, monospace, |
| 88 | color, background, align, angle) { |
| 89 | if (bold) x <- paste0("\\bfseries{", x, "}") |
| 90 | if (italic) x <-paste0("\\em{", x, "}") |
| 91 | if (monospace) x <- paste0("\\ttfamily{", x, "}") |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame^] | 92 | if (!is.null(color)) { |
| 93 | color <- latex_color(color) |
| 94 | x <- paste0("\\textcolor", color, "{", x, "}") |
| 95 | } |
| 96 | if (!is.null(background)) { |
| 97 | background <- latex_color(background) |
| 98 | x <- paste0("\\cellcolor", background, "{", x, "}") |
| 99 | } |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 100 | if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}") |
| 101 | if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}") |
| 102 | return(x) |
| 103 | } |