From 2240c3ddb44b615c116822b629fdb803f801638d Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Wed, 29 Oct 2025 16:25:45 +0000 Subject: [PATCH 1/4] Add option to turn off use_af --- DESCRIPTION | 2 +- NEWS.md | 3 + R/use_afcharts.R | 245 +++++++++++++++++++++++++++++--------------- man/use_afcharts.Rd | 9 +- 4 files changed, 176 insertions(+), 83 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 101827c..a5008a8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,7 +20,7 @@ URL: BugReports: https://github.com/best-practice-and-impact/afcharts/issues Encoding: UTF-8 LazyData: true -RoxygenNote: 7.3.2 +RoxygenNote: 7.3.3 Depends: R (>= 3.5) Imports: diff --git a/NEWS.md b/NEWS.md index dedc8cc..fb49b85 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,7 @@ # afcharts (development version) + +- `use_afcharts` gains a `reset` argument to turn off analysis function styling of charts. + - Set the `main` colour palettes to `categorical`. The Scale_*_discrete_af functions now use the 6 colour cateogrical palette by default, rather than the 4 colour. - Added `af_dark_blue`, `af_orange` and `af_grey` to give easier access to the hex codes of these colours. diff --git a/R/use_afcharts.R b/R/use_afcharts.R index 3fb6410..279acd5 100644 --- a/R/use_afcharts.R +++ b/R/use_afcharts.R @@ -6,6 +6,8 @@ #' @param default_colour Default colour/fill for geoms. Default value is 'blue' #' from `af_colour_values`. #' @param ... Arguments passed to `theme_af()`. +#' @param reset Logical. Turn off use_afcharts. This aims to reset the default +#' chart setting to their status when `use_afcharts` was first called. #' #' @returns NULL. Function is used for side effects of setting ggplot2 plot #' theme, colour palette and geom aesthetic defaults. @@ -26,98 +28,179 @@ #' @export -use_afcharts <- function( - default_colour = afcharts::af_colour_values["dark-blue"], - ...) { +use_afcharts <- function(default_colour = afcharts::af_colour_values["dark-blue"], + ..., + reset = FALSE) { - # Use afcharts theme ---- + if(!rlang::is_bool(reset)) { + cli::cli_abort("{.arg reset} must be {.code TRUE} or {.code FALSE}") + } - ggplot2::theme_set(theme_af(...)) + if (isFALSE(reset)) { - cli::cli_alert_info("Default ggplot2 theme set to `theme_af`.") + # Use afcharts theme ---- + old_theme <- ggplot2::theme_set(theme_af(...)) - # Use use_afcharts colour palette ---- + if (!isTRUE(getOption("afcharts.use_afcharts"))) { + options("afcharts.old.theme" = old_theme) + } + + cli::cli_alert_info("Default ggplot2 theme set to `theme_af`.") + + + # Use use_afcharts colour palette ---- + + old_scales <- options( + ggplot2.continuous.fill = scale_fill_continuous_af, + ggplot2.continuous.colour = scale_colour_continuous_af, + ggplot2.discrete.fill = scale_fill_discrete_af, + ggplot2.discrete.colour = scale_colour_discrete_af + ) - options(ggplot2.continuous.fill = scale_fill_continuous_af, - ggplot2.continuous.colour = scale_colour_continuous_af, - ggplot2.discrete.fill = scale_fill_discrete_af, - ggplot2.discrete.colour = scale_colour_discrete_af) + if (!isTRUE(getOption("afcharts.use_afcharts"))) { + options("afcharts.old.scales" = old_scales) + } - cli::cli_alert_info("Default colours set.") + cli::cli_alert_info("Default colour palettes set.") - # Set default geom characteristics ---- - # Get default base sizes used in theme - default <- formals(theme_af) + # Set default geom characteristics ---- - # Update default values with those passed to use_afcharts - new_values <- c(...) - for (i in seq_along(new_values)) { - default <- replace(default, - which(names(default) == names(new_values)[i]), - new_values[i]) - } + # Get default base sizes used in theme + default <- formals(theme_af) + + # Update default values with those passed to use_afcharts + new_values <- c(...) + for (i in seq_along(new_values)) { + default <- replace(default, + which(names(default) == names(new_values)[i]), + new_values[i]) + } + + # Evaluate base_size values for use in geom defaults + base_size <- eval(default$base_size) + base_line_size <- eval(default$base_line_size) + + # Lines + old_line <- ggplot2::update_geom_defaults( + geom = "line", + new = list(colour = default_colour, + linewidth = base_line_size) + ) + + old_hline <- ggplot2::update_geom_defaults( + geom = "hline", + new = list(colour = default_colour, + linewidth = base_line_size) + ) + + old_vline <- ggplot2::update_geom_defaults( + geom = "vline", + new = list(colour = default_colour, + linewidth = base_line_size) + ) + + # Col + old_col <- ggplot2::update_geom_defaults( + geom = "col", + new = list(fill = default_colour) + ) - # Evaluate base_size values for use in geom defaults - base_size <- eval(default$base_size) - base_line_size <- eval(default$base_line_size) - - # Lines - ggplot2::update_geom_defaults( - geom = "line", - new = list(colour = default_colour, - linewidth = base_line_size) - ) - - ggplot2::update_geom_defaults( - geom = "hline", - new = list(colour = default_colour, - linewidth = base_line_size) - ) - - ggplot2::update_geom_defaults( - geom = "vline", - new = list(colour = default_colour, - linewidth = base_line_size) - ) - - # Col - ggplot2::update_geom_defaults( - geom = "col", - new = list(fill = default_colour) - ) - - # Bar - ggplot2::update_geom_defaults( - geom = "bar", - new = list(fill = default_colour) - ) - - # Text - ggplot2::update_geom_defaults( - geom = "text", - new = list(colour = "black", - size = base_size / ggplot2::.pt) - ) - - ggplot2::update_geom_defaults( - geom = "label", - new = list(colour = "black", - size = base_size / ggplot2::.pt) - ) - - # Point - ggplot2::update_geom_defaults( - geom = "point", - new = list(colour = default_colour, - fill = default_colour, - size = base_size / 8) - ) - - cli::cli_alert_info("Default geom aesthetics set.") - - invisible(NULL) + # Bar + old_bar <- ggplot2::update_geom_defaults( + geom = "bar", + new = list(fill = default_colour) + ) + # Text + old_text <- ggplot2::update_geom_defaults( + geom = "text", + new = list(colour = "black", + size = base_size / ggplot2::.pt) + ) + + old_label <- ggplot2::update_geom_defaults( + geom = "label", + new = list(colour = "black", + size = base_size / ggplot2::.pt) + ) + + # Point + old_point <- ggplot2::update_geom_defaults( + geom = "point", + new = list(colour = default_colour, + fill = default_colour, + size = base_size / 8) + ) + + + if (!isTRUE(getOption("afcharts.use_afcharts"))) { + options( + afcharts.old.geoms = list( + line = old_line, + hline = old_hline, + vline = old_vline, + col = old_col, + bar = old_bar, + text = old_text, + label = old_label, + point = old_point + ) + ) + } + + cli::cli_alert_info("Default geom aesthetics set.") + + # Record that using use_af ---- + + options("afcharts.use_afcharts" = TRUE) + + invisible(NULL) + + } else { + + if (isTRUE(getOption("afcharts.use_afcharts"))) { + + # Reset theme + old_theme <- getOption("afcharts.old.theme") + + if(!is.null(old_theme)) { + ggplot2::theme_set(old_theme) + cli::cli_alert_info("Reverting theme.") + options("afcharts.old.theme" = NULL) + } + + # Reset scales + old_scales <- getOption("afcharts.old.scales") + + if (!is.null(old_scales)) { + options(old_scales) + cli::cli_alert_info("Reverting colour palettes.") + options("afcharts.old.scales" = NULL) + } + + # Reset geoms + + old.geoms <- getOption("afcharts.old.geoms") + + if(!is.null(old.geoms)) { + purrr::walk2( + names(old.geoms), old.geoms, + \(geom, default) ggplot2::update_geom_defaults( + geom, default + ) + ) + cli::cli_alert_info("Reverting geom aesthetics.") + options("afcharts.old.geoms" = NULL) + } + + # Turn off use_afcharts + options("afcharts.use_afcharts" = NULL) + + } + + + } } diff --git a/man/use_afcharts.Rd b/man/use_afcharts.Rd index ec7661b..80aaab5 100644 --- a/man/use_afcharts.Rd +++ b/man/use_afcharts.Rd @@ -4,13 +4,20 @@ \alias{use_afcharts} \title{Use afcharts defaults.} \usage{ -use_afcharts(default_colour = afcharts::af_colour_values["dark-blue"], ...) +use_afcharts( + default_colour = afcharts::af_colour_values["dark-blue"], + ..., + reset = FALSE +) } \arguments{ \item{default_colour}{Default colour/fill for geoms. Default value is 'blue' from \code{af_colour_values}.} \item{...}{Arguments passed to \code{theme_af()}.} + +\item{reset}{Logical. Turn off use_afcharts. This aims to reset the default +chart setting to their status when \code{use_afcharts} was first called.} } \value{ NULL. Function is used for side effects of setting ggplot2 plot From b17570c33e734d97529c6fa6bb2a1e49f9aa7406 Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Thu, 30 Oct 2025 09:16:27 +0000 Subject: [PATCH 2/4] Fix lint issues --- R/use_afcharts.R | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/R/use_afcharts.R b/R/use_afcharts.R index 279acd5..096bf94 100644 --- a/R/use_afcharts.R +++ b/R/use_afcharts.R @@ -28,11 +28,12 @@ #' @export -use_afcharts <- function(default_colour = afcharts::af_colour_values["dark-blue"], +use_afcharts <- function(default_colour = + afcharts::af_colour_values["dark-blue"], ..., reset = FALSE) { - if(!rlang::is_bool(reset)) { + if (!rlang::is_bool(reset)) { cli::cli_abort("{.arg reset} must be {.code TRUE} or {.code FALSE}") } @@ -166,7 +167,7 @@ use_afcharts <- function(default_colour = afcharts::af_colour_values["dark-blue" # Reset theme old_theme <- getOption("afcharts.old.theme") - if(!is.null(old_theme)) { + if (!is.null(old_theme)) { ggplot2::theme_set(old_theme) cli::cli_alert_info("Reverting theme.") options("afcharts.old.theme" = NULL) @@ -183,14 +184,15 @@ use_afcharts <- function(default_colour = afcharts::af_colour_values["dark-blue" # Reset geoms - old.geoms <- getOption("afcharts.old.geoms") + old_geoms <- getOption("afcharts.old.geoms") - if(!is.null(old.geoms)) { + if (!is.null(old_geoms)) { purrr::walk2( - names(old.geoms), old.geoms, - \(geom, default) ggplot2::update_geom_defaults( - geom, default - ) + names(old_geoms), old_geoms, + \(geom, default) { + ggplot2::update_geom_defaults(geom, default) + } + ) cli::cli_alert_info("Reverting geom aesthetics.") options("afcharts.old.geoms" = NULL) From 3475b8e7c4aa594512f1ae6b1e0086fb60eb2451 Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Thu, 30 Oct 2025 10:51:10 +0000 Subject: [PATCH 3/4] Add test for use_afcharts --- DESCRIPTION | 7 +- .../_snaps/chart-output/use-afcharts-1.svg | 68 ++++++++++++++++ .../_snaps/chart-output/use-afcharts-2.svg | 60 ++++++++++++++ .../_snaps/chart-output/use-afcharts-3.svg | 79 ++++++++++++++++++ .../_snaps/chart-output/use-afcharts-4.svg | 68 ++++++++++++++++ tests/testthat/test-chart-output.R | 80 ++++++++++++++++--- 6 files changed, 346 insertions(+), 16 deletions(-) create mode 100644 tests/testthat/_snaps/chart-output/use-afcharts-1.svg create mode 100644 tests/testthat/_snaps/chart-output/use-afcharts-2.svg create mode 100644 tests/testthat/_snaps/chart-output/use-afcharts-3.svg create mode 100644 tests/testthat/_snaps/chart-output/use-afcharts-4.svg diff --git a/DESCRIPTION b/DESCRIPTION index a5008a8..14196d2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,7 +28,8 @@ Imports: scales, cli, rlang, - dplyr + dplyr, + purrr Suggests: ggtext, knitr, @@ -36,7 +37,6 @@ Suggests: tibble, tidyr, glue, - purrr, stringr, testthat (>= 2.1.0), plotly, @@ -45,6 +45,7 @@ Suggests: ragg (>= 1.2.6), gapminder, diffviewer, - vdiffr + vdiffr, + withr VignetteBuilder: knitr Roxygen: list(markdown = TRUE) diff --git a/tests/testthat/_snaps/chart-output/use-afcharts-1.svg b/tests/testthat/_snaps/chart-output/use-afcharts-1.svg new file mode 100644 index 0000000..f7c0146 --- /dev/null +++ b/tests/testthat/_snaps/chart-output/use-afcharts-1.svg @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +2.5 +5.0 +7.5 + + + + + + + + + +expedition 2wd +explorer 4wd +f150 pickup 4wd +mustang +model +count + +class + + + +pickup +subcompact +suv +use_afcharts_1 + + diff --git a/tests/testthat/_snaps/chart-output/use-afcharts-2.svg b/tests/testthat/_snaps/chart-output/use-afcharts-2.svg new file mode 100644 index 0000000..d69cfd1 --- /dev/null +++ b/tests/testthat/_snaps/chart-output/use-afcharts-2.svg @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +2.5 +5.0 +7.5 + + + + + + + + + +expedition 2wd +explorer 4wd +f150 pickup 4wd +mustang +model +count +use_afcharts_2 + + diff --git a/tests/testthat/_snaps/chart-output/use-afcharts-3.svg b/tests/testthat/_snaps/chart-output/use-afcharts-3.svg new file mode 100644 index 0000000..c3aa37f --- /dev/null +++ b/tests/testthat/_snaps/chart-output/use-afcharts-3.svg @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +2.5 +5.0 +7.5 + + + + + + + + +expedition 2wd +explorer 4wd +f150 pickup 4wd +mustang +model +count + +class + + + + + + +pickup +subcompact +suv +use_afcharts_3 + + diff --git a/tests/testthat/_snaps/chart-output/use-afcharts-4.svg b/tests/testthat/_snaps/chart-output/use-afcharts-4.svg new file mode 100644 index 0000000..a78f550 --- /dev/null +++ b/tests/testthat/_snaps/chart-output/use-afcharts-4.svg @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +2.5 +5.0 +7.5 + + + + + + + + +expedition 2wd +explorer 4wd +f150 pickup 4wd +mustang +model +count +use_afcharts_4 + + diff --git a/tests/testthat/test-chart-output.R b/tests/testthat/test-chart-output.R index 35db7e5..d918217 100644 --- a/tests/testthat/test-chart-output.R +++ b/tests/testthat/test-chart-output.R @@ -77,16 +77,70 @@ test_that("scale_colour_continuous_af works", { }) -# Test commented out until there is an way to turn off use_afcharts - -# test_that("use_afcharts works", { -# -# use_afcharts() -# -# d <- subset(mpg, manufacturer == "ford") -# -# plot <- ggplot2::ggplot(d, ggplot2::aes(x = model, fill = class, colour = class)) + -# ggplot2::geom_bar() -# -# expect_match_plot("use_afcharts", plot) -# }) +# Test use_afcharts + +test_that("use_afcharts works", { + + # Set default theme, geom and colour scale + + # Set theme + + old_theme <- ggplot2::theme_set(ggplot2::theme_dark()) + + withr::defer(ggplot2::theme_set(old_theme)) + + + # Set geom + + old_geom <- ggplot2::update_geom_defaults( + geom = "bar", + new = list(fill = "red") + ) + + withr::defer( + ggplot2::update_geom_defaults( + geom = "bar", + new = old_geom + ) + ) + + + # Set scale + + old_options <- options(ggplot2.discrete.fill = ggplot2::scale_fill_viridis_d) + withr::defer(options(old_options)) + + + # Turn on use_afcharts - should ignore the above defaults + + use_afcharts() + + d <- subset(ggplot2::mpg, manufacturer == "ford") + + plot1 <- ggplot2::ggplot(d, ggplot2::aes(x = model, fill = class)) + + ggplot2::geom_bar() + + plot2 <- ggplot2::ggplot(d, ggplot2::aes(x = model)) + + ggplot2::geom_bar() + + expect_match_plot("use_afcharts_1", plot1) + expect_match_plot("use_afcharts_2", plot2) + + + # Turn use_afcharts off and check default plot settings revert to what they + # were prior to using use_afcharts + + use_afcharts(reset = TRUE) + + plot3 <- ggplot2::ggplot(d, ggplot2::aes(x = model, fill = class)) + + ggplot2::geom_bar() + + plot4 <- ggplot2::ggplot(d, ggplot2::aes(x = model)) + + ggplot2::geom_bar() + + expect_match_plot("use_afcharts_3", plot3) + expect_match_plot("use_afcharts_4", plot4) + +}) + + From 7d62683d4e70270ee5168db05ee9c95a9c1481e3 Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Thu, 30 Oct 2025 10:52:05 +0000 Subject: [PATCH 4/4] Fix lint issues --- tests/testthat/test-chart-output.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testthat/test-chart-output.R b/tests/testthat/test-chart-output.R index d918217..55e7cef 100644 --- a/tests/testthat/test-chart-output.R +++ b/tests/testthat/test-chart-output.R @@ -142,5 +142,3 @@ test_that("use_afcharts works", { expect_match_plot("use_afcharts_4", plot4) }) - -