Skip to content

Commit b178fb0

Browse files
use bslib to load brand.yml files (#241)
* use bslib to load brand.yml files * check for bslib and other suggested packages also rename *ggplot to *ggplot2 * artifacts
1 parent e653786 commit b178fb0

File tree

9 files changed

+77
-32
lines changed

9 files changed

+77
-32
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ URL: https://github.com/quarto-dev/quarto-r,
1717
BugReports: https://github.com/quarto-dev/quarto-r/issues
1818
Depends:
1919
R (>= 3.6)
20-
Imports:
20+
Imports:
2121
cli,
2222
jsonlite,
2323
later,
@@ -29,6 +29,7 @@ Imports:
2929
utils,
3030
yaml
3131
Suggests:
32+
bslib,
3233
callr,
3334
curl,
3435
dplyr,

NAMESPACE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ export(quarto_update_extension)
1919
export(quarto_use_template)
2020
export(quarto_version)
2121
export(theme_brand_flextable)
22-
export(theme_brand_ggplot)
22+
export(theme_brand_ggplot2)
2323
export(theme_brand_gt)
2424
export(theme_brand_plotly)
2525
export(theme_brand_thematic)
2626
export(theme_colors_flextable)
27-
export(theme_colors_ggplot)
27+
export(theme_colors_ggplot2)
2828
export(theme_colors_gt)
2929
export(theme_colors_plotly)
3030
export(theme_colors_thematic)

R/theme.R

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#'
1515
#' @export
1616
theme_colors_flextable <- function(bg, fg) {
17+
rlang::check_installed(
18+
"flextable",
19+
"flextable is required for theme_colors_flextable"
20+
)
1721
(function(x) {
1822
if (!inherits(x, "flextable")) {
1923
stop("theme_colors_flextable only supports flextable objects.")
@@ -28,18 +32,24 @@ theme_colors_flextable <- function(bg, fg) {
2832
#'
2933
#' @export
3034
theme_brand_flextable <- function(brand_yml) {
31-
brand <- yaml::yaml.load_file(brand_yml)
35+
rlang::check_installed(
36+
"bslib",
37+
"bslib is required for brand support in R",
38+
version = "0.9.0"
39+
)
40+
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
3241
theme_colors_flextable(brand$color$background, brand$color$foreground)
3342
}
3443

3544

3645
#' @rdname theme_helpers
3746
#'
3847
#' @export
39-
theme_colors_ggplot <- function(bg, fg) {
40-
if (!requireNamespace("ggplot2", quietly = TRUE)) {
41-
return(NULL)
42-
}
48+
theme_colors_ggplot2 <- function(bg, fg) {
49+
rlang::check_installed(
50+
"ggplot2",
51+
"ggplot2 is required for theme_colors_ggplot2"
52+
)
4353
ggplot2::`%+%`(
4454
ggplot2::theme_minimal(base_size = 11),
4555
ggplot2::theme(
@@ -61,16 +71,25 @@ theme_colors_ggplot <- function(bg, fg) {
6171
#' @rdname theme_helpers
6272
#'
6373
#' @export
64-
theme_brand_ggplot <- function(brand_yml) {
65-
brand <- yaml::yaml.load_file(brand_yml)
66-
theme_colors_ggplot(brand$color$background, brand$color$foreground)
74+
theme_brand_ggplot2 <- function(brand_yml) {
75+
rlang::check_installed(
76+
"bslib",
77+
"bslib is required for brand support in R",
78+
version = "0.9.0"
79+
)
80+
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
81+
theme_colors_ggplot2(brand$color$background, brand$color$foreground)
6782
}
6883

6984

7085
#' @rdname theme_helpers
7186
#'
7287
#' @export
7388
theme_colors_gt <- function(bg, fg) {
89+
rlang::check_installed(
90+
"gt",
91+
"gt is required for theme_colors_gt"
92+
)
7493
(function(table) {
7594
table |>
7695
gt::tab_options(
@@ -84,14 +103,23 @@ theme_colors_gt <- function(bg, fg) {
84103
#'
85104
#' @export
86105
theme_brand_gt <- function(brand_yml) {
87-
brand <- yaml::yaml.load_file(brand_yml)
106+
rlang::check_installed(
107+
"bslib",
108+
"bslib is required for brand support in R",
109+
version = "0.9.0"
110+
)
111+
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
88112
theme_colors_gt(brand$color$background, brand$color$foreground)
89113
}
90114

91115
#' @rdname theme_helpers
92116
#'
93117
#' @export
94118
theme_colors_plotly <- function(bg, fg) {
119+
rlang::check_installed(
120+
"plotly",
121+
"plotly is required for theme_colors_plotly"
122+
)
95123
(function(plot) {
96124
plot |>
97125
plotly::layout(
@@ -106,7 +134,12 @@ theme_colors_plotly <- function(bg, fg) {
106134
#'
107135
#' @export
108136
theme_brand_plotly <- function(brand_yml) {
109-
brand <- yaml::yaml.load_file(brand_yml)
137+
rlang::check_installed(
138+
"bslib",
139+
"bslib is required for brand support in R",
140+
version = "0.9.0"
141+
)
142+
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
110143
theme_colors_plotly(brand$color$background, brand$color$foreground)
111144
}
112145

@@ -115,6 +148,10 @@ theme_brand_plotly <- function(brand_yml) {
115148
#'
116149
#' @export
117150
theme_colors_thematic <- function(bg, fg) {
151+
rlang::check_installed(
152+
"thematic",
153+
"thematic is required for theme_colors_thematic"
154+
)
118155
(function() {
119156
thematic::thematic_rmd(
120157
bg = bg,
@@ -127,6 +164,11 @@ theme_colors_thematic <- function(bg, fg) {
127164
#'
128165
#' @export
129166
theme_brand_thematic <- function(brand_yml) {
130-
brand <- yaml::yaml.load_file(brand_yml)
167+
rlang::check_installed(
168+
"bslib",
169+
"bslib is required for brand support in R",
170+
version = "0.9.0"
171+
)
172+
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
131173
theme_colors_thematic(brand$color$background, brand$color$foreground)
132174
}

man/quarto_publish_doc.Rd

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/theme_helpers.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-theme.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ test_that("render ggiraph", {
1414
.render(test_path("theme/ggiraph.qmd"))
1515
})
1616

17-
test_that("render ggplot", {
18-
.render(test_path("theme/ggplot.qmd"))
17+
test_that("render ggplot2", {
18+
.render(test_path("theme/ggplot2.qmd"))
1919
})
2020

2121
test_that("render gt", {

tests/testthat/theme/ggiraph.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ library(quarto)
1212
library(ggplot2)
1313
library(ggiraph)
1414
15-
united_theme <- theme_brand_ggplot("united-brand.yml")
16-
slate_theme <- theme_brand_ggplot("slate-brand.yml")
15+
united_theme <- theme_brand_ggplot2("united-brand.yml")
16+
slate_theme <- theme_brand_ggplot2("slate-brand.yml")
1717
```
1818

1919
```{r}

tests/testthat/theme/ggplot.qmd renamed to tests/testthat/theme/ggplot2.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ brand:
1212
library(quarto)
1313
library(ggplot2)
1414
15-
united_theme <- theme_brand_ggplot("united-brand.yml")
16-
slate_theme <- theme_brand_ggplot("slate-brand.yml")
15+
united_theme <- theme_brand_ggplot2("united-brand.yml")
16+
slate_theme <- theme_brand_ggplot2("slate-brand.yml")
1717
```
1818

1919
```{r}

vignettes/theme-helpers.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ library(quarto)
5656
library(ggplot2)
5757
library(ggiraph)
5858
59-
blue_mauve_theme = theme_colors_ggplot("#111038", "#E0B0FF")
59+
blue_mauve_theme = theme_colors_ggplot2("#111038", "#E0B0FF")
6060
6161
cars <- ggplot(mtcars, aes(mpg, wt)) +
6262
geom_point_interactive(aes(colour = factor(cyl), tooltip = rownames(mtcars))) +
@@ -76,7 +76,7 @@ Demonstrates a ggplot2 plot with near-black background and green-grey foreground
7676
library(quarto)
7777
library(ggplot2)
7878
79-
black_greyn <- theme_colors_ggplot("#050411", "#8faf8e")
79+
black_greyn <- theme_colors_ggplot2("#050411", "#8faf8e")
8080
8181
cars <- ggplot(mtcars, aes(mpg, wt)) +
8282
geom_point(aes(colour = factor(cyl))) +

0 commit comments

Comments
 (0)