|
| 1 | +#' Create a theme for a plotting or table package |
| 2 | +#' |
| 3 | +#' Create a theme using background and foreground colors (`theme_colors_*`) or |
| 4 | +#' using a **brand.yml** file (`theme_brand_*`). |
| 5 | +#' |
| 6 | +#' The use of the theme will depend on the package. See the vignettes for |
| 7 | +#' usage examples. |
| 8 | +#' |
| 9 | +#' @param bg The background color |
| 10 | +#' @param fg The foreground color |
| 11 | +#' @param brand_yml The path to a brand.yml file |
| 12 | + |
| 13 | + |
| 14 | +#' @rdname theme_helpers |
| 15 | +#' |
| 16 | +#' @export |
| 17 | +theme_colors_flextable <- function(bg, fg) { |
| 18 | + (function(x) { |
| 19 | + if (!inherits(x, "flextable")) { |
| 20 | + stop("theme_colors_flextable only supports flextable objects.") |
| 21 | + } |
| 22 | + x <- flextable::bg(x, bg = bg, part = "all") |
| 23 | + x <- flextable::color(x, color = fg, part = "all") |
| 24 | + flextable::autofit(x) |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +#' @rdname theme_helpers |
| 29 | +#' |
| 30 | +#' @export |
| 31 | +theme_brand_flextable <- function(brand_yml) { |
| 32 | + brand <- yaml::yaml.load_file(brand_yml) |
| 33 | + theme_colors_flextable(brand$color$background, brand$color$foreground) |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +#' @rdname theme_helpers |
| 38 | +#' |
| 39 | +#' @export |
| 40 | +theme_colors_ggplot <- function(bg, fg) { |
| 41 | + if (!requireNamespace("ggplot2", quietly = TRUE)) { |
| 42 | + return(NULL) |
| 43 | + } |
| 44 | + ggplot2::`%+%`(ggplot2::theme_minimal(base_size = 11), |
| 45 | + ggplot2::theme( |
| 46 | + panel.border = ggplot2::element_blank(), |
| 47 | + panel.grid.major.y = ggplot2::element_blank(), |
| 48 | + panel.grid.minor.y = ggplot2::element_blank(), |
| 49 | + panel.grid.major.x = ggplot2::element_blank(), |
| 50 | + panel.grid.minor.x = ggplot2::element_blank(), |
| 51 | + text = ggplot2::element_text(colour = fg), |
| 52 | + axis.text = ggplot2::element_text(colour = fg), |
| 53 | + rect = ggplot2::element_rect(colour = bg, fill = bg), |
| 54 | + plot.background = ggplot2::element_rect(fill = bg, colour = NA), |
| 55 | + axis.line = ggplot2::element_line(colour = fg), |
| 56 | + axis.ticks = ggplot2::element_line(colour = fg) |
| 57 | + )) |
| 58 | +} |
| 59 | + |
| 60 | +#' @rdname theme_helpers |
| 61 | +#' |
| 62 | +#' @export |
| 63 | +theme_brand_ggplot <- function(brand_yml) { |
| 64 | + brand <- yaml::yaml.load_file(brand_yml) |
| 65 | + theme_colors_ggplot(brand$color$background, brand$color$foreground) |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +#' @rdname theme_helpers |
| 70 | +#' |
| 71 | +#' @export |
| 72 | +theme_colors_gt <- function(bg, fg) { |
| 73 | + (function(table) { |
| 74 | + table |> |
| 75 | + gt::tab_options( |
| 76 | + table.background.color = bg, |
| 77 | + table.font.color = fg, |
| 78 | + ) |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +#' @rdname theme_helpers |
| 83 | +#' |
| 84 | +#' @export |
| 85 | +theme_brand_gt <- function(brand_yml) { |
| 86 | + brand <- yaml::yaml.load_file(brand_yml) |
| 87 | + theme_colors_gt(brand$color$background, brand$color$foreground) |
| 88 | +} |
| 89 | + |
| 90 | +#' @rdname theme_helpers |
| 91 | +#' |
| 92 | +#' @export |
| 93 | +theme_colors_plotly <- function(bg, fg) { |
| 94 | + (function(plot) { |
| 95 | + plot |> plotly::layout(paper_bgcolor = bg, |
| 96 | + plot_bgcolor = bg, |
| 97 | + font = list(color = fg) |
| 98 | + ) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +#' @rdname theme_helpers |
| 103 | +#' |
| 104 | +#' @export |
| 105 | +theme_brand_plotly <- function(brand_yml) { |
| 106 | + brand <- yaml::yaml.load_file(brand_yml) |
| 107 | + theme_colors_plotly(brand$color$background, brand$color$foreground) |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +#' @rdname theme_helpers |
| 112 | +#' |
| 113 | +#' @export |
| 114 | +theme_colors_thematic <- function(bg, fg) { |
| 115 | + (function() { |
| 116 | + thematic::thematic_rmd( |
| 117 | + bg = bg, |
| 118 | + fg = fg, |
| 119 | + )}) |
| 120 | +} |
| 121 | + |
| 122 | +#' @rdname theme_helpers |
| 123 | +#' |
| 124 | +#' @export |
| 125 | +theme_brand_thematic <- function(brand_yml) { |
| 126 | + brand <- yaml::yaml.load_file(brand_yml) |
| 127 | + theme_colors_thematic(brand$color$background, brand$color$foreground) |
| 128 | +} |
0 commit comments