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 |
| 23 | #' @param angle 0-360, degree that the text will rotate. |
| 24 | #' |
| 25 | #' @export |
| 26 | cell_spec <- function(x, format, |
| 27 | bold = F, italic = F, monospace = F, |
| 28 | color = NULL, background = NULL, |
| 29 | align = NULL, font_size = NULL, angle = NULL) { |
| 30 | |
| 31 | if (missing(format) || is.null(format)) format = getOption('knitr.table.format') |
| 32 | if (is.null(format)) { |
| 33 | warning("Output format for cell_formatter was not specified. Using ", |
| 34 | "html as default. You may consider to set it up via option knitr.table.format.", |
| 35 | "See ?cell_formatter for details. ") |
| 36 | return(x) |
| 37 | } |
| 38 | |
| 39 | if (tolower(format) == "html") { |
| 40 | return(cell_spec_html(x, bold, italic, monospace, |
| 41 | color, background, align, font_size, angle)) |
| 42 | } |
| 43 | if (tolower(format) == "latex") { |
| 44 | return(cell_spec_latex(x, bold, italic, monospace, |
| 45 | color, background, align, angle)) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | cell_spec_html <- function(x, bold, italic, monospace, |
| 50 | color, background, align, font_size, angle) { |
| 51 | cell_style <- NULL |
| 52 | if (bold) cell_style <- paste(cell_style,"font-weight: bold;") |
| 53 | if (italic) cell_style <- paste(cell_style, "font-style: italic;") |
| 54 | if (monospace) cell_style <- paste(cell_style, "font-family: monospace;") |
| 55 | if (!is.null(color)) cell_style <- paste0(cell_style, " color: ", color, ";") |
| 56 | if (!is.null(background)) { |
| 57 | cell_style <- paste0(cell_style, " border-radius: 4px; padding-right: 2px", |
| 58 | "; background-color: ", background, ";") |
| 59 | } |
| 60 | if (!is.null(align)) { |
| 61 | cell_style <- paste0(cell_style, " text-align: ", align, ";") |
| 62 | } |
| 63 | if (!is.null(font_size)) { |
| 64 | cell_style <- paste0(cell_style, " font-size: ", font_size, "px;") |
| 65 | } |
| 66 | if (!is.null(angle)) { |
| 67 | cell_style <- paste0(cell_style, |
| 68 | " -webkit-transform: rotate(", angle, |
| 69 | "deg); -moz-transform: rotate(", angle, |
| 70 | "deg); -ms-transform: rotate(", angle, |
| 71 | "deg); -o-transform: rotate(", angle, "deg);") |
| 72 | } |
| 73 | out <- paste0( |
| 74 | '<div style="', cell_style, '">', x, '</div>' |
| 75 | ) |
| 76 | return(out) |
| 77 | } |
| 78 | |
| 79 | cell_spec_latex <- function(x, bold, italic, monospace, |
| 80 | color, background, align, angle) { |
| 81 | if (bold) x <- paste0("\\bfseries{", x, "}") |
| 82 | if (italic) x <-paste0("\\em{", x, "}") |
| 83 | if (monospace) x <- paste0("\\ttfamily{", x, "}") |
| 84 | if (!is.null(color)) x <- paste0("\\textcolor{", color, "}{", x, "}") |
| 85 | if (!is.null(background)) x <- paste0("\\cellcolor{", background, "}{", x, "}") |
| 86 | if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}") |
| 87 | if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}") |
| 88 | return(x) |
| 89 | } |