Skip to content

use bslib to load brand.yml files #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ URL: https://github.com/quarto-dev/quarto-r,
BugReports: https://github.com/quarto-dev/quarto-r/issues
Depends:
R (>= 3.6)
Imports:
Imports:
cli,
jsonlite,
later,
Expand All @@ -29,6 +29,7 @@ Imports:
utils,
yaml
Suggests:
bslib,
callr,
curl,
dplyr,
Expand Down
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export(quarto_update_extension)
export(quarto_use_template)
export(quarto_version)
export(theme_brand_flextable)
export(theme_brand_ggplot)
export(theme_brand_ggplot2)
export(theme_brand_gt)
export(theme_brand_plotly)
export(theme_brand_thematic)
export(theme_colors_flextable)
export(theme_colors_ggplot)
export(theme_colors_ggplot2)
export(theme_colors_gt)
export(theme_colors_plotly)
export(theme_colors_thematic)
Expand Down
64 changes: 53 additions & 11 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#'
#' @export
theme_colors_flextable <- function(bg, fg) {
rlang::check_installed(
"flextable",
"flextable is required for theme_colors_flextable"
)
(function(x) {
if (!inherits(x, "flextable")) {
stop("theme_colors_flextable only supports flextable objects.")
Expand All @@ -28,18 +32,24 @@ theme_colors_flextable <- function(bg, fg) {
#'
#' @export
theme_brand_flextable <- function(brand_yml) {
brand <- yaml::yaml.load_file(brand_yml)
rlang::check_installed(
"bslib",
"bslib is required for brand support in R",
version = "0.9.0"
)
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
theme_colors_flextable(brand$color$background, brand$color$foreground)
}


#' @rdname theme_helpers
#'
#' @export
theme_colors_ggplot <- function(bg, fg) {
if (!requireNamespace("ggplot2", quietly = TRUE)) {
return(NULL)
}
theme_colors_ggplot2 <- function(bg, fg) {
rlang::check_installed(
"ggplot2",
"ggplot2 is required for theme_colors_ggplot2"
)
ggplot2::`%+%`(
ggplot2::theme_minimal(base_size = 11),
ggplot2::theme(
Expand All @@ -61,16 +71,25 @@ theme_colors_ggplot <- function(bg, fg) {
#' @rdname theme_helpers
#'
#' @export
theme_brand_ggplot <- function(brand_yml) {
brand <- yaml::yaml.load_file(brand_yml)
theme_colors_ggplot(brand$color$background, brand$color$foreground)
theme_brand_ggplot2 <- function(brand_yml) {
rlang::check_installed(
"bslib",
"bslib is required for brand support in R",
version = "0.9.0"
)
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
theme_colors_ggplot2(brand$color$background, brand$color$foreground)
}


#' @rdname theme_helpers
#'
#' @export
theme_colors_gt <- function(bg, fg) {
rlang::check_installed(
"gt",
"gt is required for theme_colors_gt"
)
(function(table) {
table |>
gt::tab_options(
Expand All @@ -84,14 +103,23 @@ theme_colors_gt <- function(bg, fg) {
#'
#' @export
theme_brand_gt <- function(brand_yml) {
brand <- yaml::yaml.load_file(brand_yml)
rlang::check_installed(
"bslib",
"bslib is required for brand support in R",
version = "0.9.0"
)
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
theme_colors_gt(brand$color$background, brand$color$foreground)
}

#' @rdname theme_helpers
#'
#' @export
theme_colors_plotly <- function(bg, fg) {
rlang::check_installed(
"plotly",
"plotly is required for theme_colors_plotly"
)
(function(plot) {
plot |>
plotly::layout(
Expand All @@ -106,7 +134,12 @@ theme_colors_plotly <- function(bg, fg) {
#'
#' @export
theme_brand_plotly <- function(brand_yml) {
brand <- yaml::yaml.load_file(brand_yml)
rlang::check_installed(
"bslib",
"bslib is required for brand support in R",
version = "0.9.0"
)
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
theme_colors_plotly(brand$color$background, brand$color$foreground)
}

Expand All @@ -115,6 +148,10 @@ theme_brand_plotly <- function(brand_yml) {
#'
#' @export
theme_colors_thematic <- function(bg, fg) {
rlang::check_installed(
"thematic",
"thematic is required for theme_colors_thematic"
)
(function() {
thematic::thematic_rmd(
bg = bg,
Expand All @@ -127,6 +164,11 @@ theme_colors_thematic <- function(bg, fg) {
#'
#' @export
theme_brand_thematic <- function(brand_yml) {
brand <- yaml::yaml.load_file(brand_yml)
rlang::check_installed(
"bslib",
"bslib is required for brand support in R",
version = "0.9.0"
)
brand <- attr(bslib::bs_theme(brand = brand_yml), "brand")
theme_colors_thematic(brand$color$background, brand$color$foreground)
}
14 changes: 8 additions & 6 deletions man/quarto_publish_doc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/theme_helpers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ test_that("render ggiraph", {
.render(test_path("theme/ggiraph.qmd"))
})

test_that("render ggplot", {
.render(test_path("theme/ggplot.qmd"))
test_that("render ggplot2", {
.render(test_path("theme/ggplot2.qmd"))
})

test_that("render gt", {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/theme/ggiraph.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ library(quarto)
library(ggplot2)
library(ggiraph)

united_theme <- theme_brand_ggplot("united-brand.yml")
slate_theme <- theme_brand_ggplot("slate-brand.yml")
united_theme <- theme_brand_ggplot2("united-brand.yml")
slate_theme <- theme_brand_ggplot2("slate-brand.yml")
```

```{r}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ brand:
library(quarto)
library(ggplot2)

united_theme <- theme_brand_ggplot("united-brand.yml")
slate_theme <- theme_brand_ggplot("slate-brand.yml")
united_theme <- theme_brand_ggplot2("united-brand.yml")
slate_theme <- theme_brand_ggplot2("slate-brand.yml")
```

```{r}
Expand Down
4 changes: 2 additions & 2 deletions vignettes/theme-helpers.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ library(quarto)
library(ggplot2)
library(ggiraph)

blue_mauve_theme = theme_colors_ggplot("#111038", "#E0B0FF")
blue_mauve_theme = theme_colors_ggplot2("#111038", "#E0B0FF")

cars <- ggplot(mtcars, aes(mpg, wt)) +
geom_point_interactive(aes(colour = factor(cyl), tooltip = rownames(mtcars))) +
Expand All @@ -76,7 +76,7 @@ Demonstrates a ggplot2 plot with near-black background and green-grey foreground
library(quarto)
library(ggplot2)

black_greyn <- theme_colors_ggplot("#050411", "#8faf8e")
black_greyn <- theme_colors_ggplot2("#050411", "#8faf8e")

cars <- ggplot(mtcars, aes(mpg, wt)) +
geom_point(aes(colour = factor(cyl))) +
Expand Down