Skip to content

Commit 1981402

Browse files
gt & cleanup
1 parent 2b6476c commit 1981402

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Suggests:
3131
curl,
3232
flextable,
3333
ggplot2,
34+
gt,
3435
knitr,
3536
rsconnect (>= 0.8.26),
3637
testthat (>= 3.1.7),

R/theme.R

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
1+
#' Create a flextable theme using background and foreground colors
2+
#'
3+
#' Create a flextable theme that uses the specified background and
4+
#' foreground colors.
5+
#'
6+
#' @param bg The background color
7+
#' @param fg The foreground color
8+
#'
9+
#' @export
10+
flextable_theme_colors <- function(bg, fg) {
11+
(function(x) {
12+
if (!inherits(x, "flextable")) {
13+
stop("flextable_theme_colors only supports flextable objects.")
14+
}
15+
x <- flextable::bg(x, bg = bg, part = "all")
16+
x <- flextable::color(x, color = fg, part = "all")
17+
flextable::autofit(x)
18+
})
19+
}
20+
21+
#' Create a flextable theme from a brand.yml file
22+
#'
23+
#' Create a flextable theme that uses colors from the brand file.
24+
#'
25+
#' @param brand_yml The path to the brand.yml file
26+
#'
27+
#' @export
28+
flextable_theme_brand <- function(brand_yml) {
29+
brand <- yaml::yaml.load_file(brand_yml)
30+
flextable_theme_colors(brand$color$background, brand$color$foreground)
31+
}
32+
33+
134
#' Create a ggplot2 theme using background and foreground colors
235
#'
336
#' Create a ggplot2 theme that uses the specified background and
437
#' foreground colors.
538
#'
6-
#' @param bgcolor The background color
7-
#' @param fgcolor The foreground color
39+
#' @param bg The background color
40+
#' @param fg The foreground color
841
#'
942
#' @export
10-
ggplot_theme_colors <- function(bgcolor, fgcolor) {
11-
if(!requireNamespace("ggplot2", quietly = TRUE)) {
43+
ggplot_theme_colors <- function(bg, fg) {
44+
if (!requireNamespace("ggplot2", quietly = TRUE)) {
1245
return(NULL)
1346
}
1447
ggplot2::`%+%`(ggplot2::theme_minimal(base_size = 11),
@@ -18,12 +51,12 @@ ggplot_theme_colors <- function(bgcolor, fgcolor) {
1851
panel.grid.minor.y = ggplot2::element_blank(),
1952
panel.grid.major.x = ggplot2::element_blank(),
2053
panel.grid.minor.x = ggplot2::element_blank(),
21-
text = ggplot2::element_text(colour = fgcolor),
22-
axis.text = ggplot2::element_text(colour = fgcolor),
23-
rect = ggplot2::element_rect(colour = bgcolor, fill = bgcolor),
24-
plot.background = ggplot2::element_rect(fill = bgcolor, colour = NA),
25-
axis.line = ggplot2::element_line(colour = fgcolor),
26-
axis.ticks = ggplot2::element_line(colour = fgcolor)
54+
text = ggplot2::element_text(colour = fg),
55+
axis.text = ggplot2::element_text(colour = fg),
56+
rect = ggplot2::element_rect(colour = bg, fill = bg),
57+
plot.background = ggplot2::element_rect(fill = bg, colour = NA),
58+
axis.line = ggplot2::element_line(colour = fg),
59+
axis.ticks = ggplot2::element_line(colour = fg)
2760
))
2861
}
2962

@@ -40,34 +73,33 @@ ggplot_theme_brand <- function(brand_yml) {
4073
}
4174

4275

43-
#' Create a flextable theme using background and foreground colors
76+
77+
#' Create a gt theme using background and foreground colors
4478
#'
45-
#' Create a flextable theme that uses the specified background and
79+
#' Create a gt theme that uses the specified background and
4680
#' foreground colors.
4781
#'
4882
#' @param bg The background color
4983
#' @param fg The foreground color
5084
#'
5185
#' @export
52-
flextable_theme_colors <- function(bg, fg) {
53-
(function(x) {
54-
if (!inherits(x, "flextable")) {
55-
stop("flextable_theme_colors only supports flextable objects.")
56-
}
57-
x <- flextable::bg(x, bg = bg, part = "all")
58-
x <- flextable::color(x, color = fg, part = "all")
59-
flextable::autofit(x)
86+
gt_theme_colors <- function(bg, fg) {
87+
(function(table) {
88+
table |>
89+
gt::tab_options(
90+
table.background.color = bg,
91+
table.font.color = fg,
92+
)
6093
})
6194
}
62-
63-
#' Create a flextable theme from a brand.yml file
95+
#' Create a gt theme from a brand.yml file
6496
#'
65-
#' Create a flextable theme that uses colors from the brand file.
97+
#' Create a gt theme that uses colors from the brand file.
6698
#'
6799
#' @param brand_yml The path to the brand.yml file
68100
#'
69101
#' @export
70-
flextable_theme_brand <- function(brand_yml) {
102+
gt_theme_brand <- function(brand_yml) {
71103
brand <- yaml::yaml.load_file(brand_yml)
72-
flextable_theme_colors(brand$color$background, brand$color$foreground)
104+
gt_theme_colors(brand$color$background, brand$color$foreground)
73105
}

0 commit comments

Comments
 (0)