From 43a05daf46b79f3e2183d61fefd588d7308aeacf Mon Sep 17 00:00:00 2001 From: VisruthSK <67435125+VisruthSK@users.noreply.github.com> Date: Fri, 19 Sep 2025 07:31:03 -0700 Subject: [PATCH 1/5] set_theme() resets theme if called with no arguments. Also added a simple test. --- R/theme-current.R | 6 ++++-- man/get_theme.Rd | 2 +- tests/testthat/test-theme.R | 6 ++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/R/theme-current.R b/R/theme-current.R index e42f8c1e68..6ce7117a94 100644 --- a/R/theme-current.R +++ b/R/theme-current.R @@ -91,9 +91,12 @@ get_theme <- function() { theme_get <- get_theme #' @rdname get_theme -#' @param new new theme (a list of theme elements) +#' @param new new theme (a list of theme elements). Sets theme to the default (theme_grey) if `new` isn't supplied. #' @export set_theme <- function(new) { + if (missing(new)) { + new <- ggplot_global$theme_default + } check_object(new, is_theme, "a {.cls theme} object") old <- ggplot_global$theme_current ggplot_global$theme_current <- new @@ -141,4 +144,3 @@ theme_replace <- replace_theme e1 } - diff --git a/man/get_theme.Rd b/man/get_theme.Rd index d8198c4197..24e062571a 100644 --- a/man/get_theme.Rd +++ b/man/get_theme.Rd @@ -31,7 +31,7 @@ theme_replace(...) e1 \%+replace\% e2 } \arguments{ -\item{new}{new theme (a list of theme elements)} +\item{new}{new theme (a list of theme elements). Sets theme to the default (theme_grey) if \code{new} isn't supplied.} \item{...}{named list of theme settings} diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index 6aa9081460..3f3fb9eeb9 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -56,6 +56,12 @@ test_that("modifying theme element properties with + operator works", { ) }) +test_that("set_theme() resets theme to default when called with no arguments", { + theme_set(theme_void()) + set_theme() + expect_identical(theme_get(), theme_grey()) +}) + test_that("adding theme object to ggplot object with + operator works", { ## test with complete theme p <- ggplot(data.frame(x = 1:3), aes(x, x)) + geom_point() + theme_grey() From 1d79f7eac524410b8d0f09b102f5da34ec97b87d Mon Sep 17 00:00:00 2001 From: VisruthSK <67435125+VisruthSK@users.noreply.github.com> Date: Fri, 19 Sep 2025 07:56:12 -0700 Subject: [PATCH 2/5] Link to default theme and treat `new` being null as `new` being missing. --- R/theme-current.R | 6 ++---- man/get_theme.Rd | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/R/theme-current.R b/R/theme-current.R index 6ce7117a94..e97969c701 100644 --- a/R/theme-current.R +++ b/R/theme-current.R @@ -91,12 +91,10 @@ get_theme <- function() { theme_get <- get_theme #' @rdname get_theme -#' @param new new theme (a list of theme elements). Sets theme to the default (theme_grey) if `new` isn't supplied. +#' @param new new theme (a list of theme elements). Sets theme to the default ([theme_grey()]) if `new` isn't supplied. #' @export set_theme <- function(new) { - if (missing(new)) { - new <- ggplot_global$theme_default - } + new <- new %||% ggplot_global$theme_default check_object(new, is_theme, "a {.cls theme} object") old <- ggplot_global$theme_current ggplot_global$theme_current <- new diff --git a/man/get_theme.Rd b/man/get_theme.Rd index 24e062571a..7c2d0e5065 100644 --- a/man/get_theme.Rd +++ b/man/get_theme.Rd @@ -31,7 +31,7 @@ theme_replace(...) e1 \%+replace\% e2 } \arguments{ -\item{new}{new theme (a list of theme elements). Sets theme to the default (theme_grey) if \code{new} isn't supplied.} +\item{new}{new theme (a list of theme elements). Sets theme to the default (\code{\link[=theme_grey]{theme_grey()}}) if \code{new} isn't supplied.} \item{...}{named list of theme settings} From 3ae9d8a9cee3d97bc62f9d905cb3076eadced551 Mon Sep 17 00:00:00 2001 From: VisruthSK <67435125+VisruthSK@users.noreply.github.com> Date: Fri, 19 Sep 2025 11:20:03 -0700 Subject: [PATCH 3/5] Fixed set_theme() default value for `new` --- R/theme-current.R | 2 +- man/get_theme.Rd | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/theme-current.R b/R/theme-current.R index e97969c701..b5839bf6f5 100644 --- a/R/theme-current.R +++ b/R/theme-current.R @@ -93,7 +93,7 @@ theme_get <- get_theme #' @rdname get_theme #' @param new new theme (a list of theme elements). Sets theme to the default ([theme_grey()]) if `new` isn't supplied. #' @export -set_theme <- function(new) { +set_theme <- function(new = null) { new <- new %||% ggplot_global$theme_default check_object(new, is_theme, "a {.cls theme} object") old <- ggplot_global$theme_current diff --git a/man/get_theme.Rd b/man/get_theme.Rd index 7c2d0e5065..16710e6c1c 100644 --- a/man/get_theme.Rd +++ b/man/get_theme.Rd @@ -16,9 +16,9 @@ get_theme() theme_get() -set_theme(new) +set_theme(new = null) -theme_set(new) +theme_set(new = null) update_theme(...) From 25fcc3a4e3a0700b8dbf6fafc5328afc484b075a Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Fri, 19 Sep 2025 14:33:05 -0400 Subject: [PATCH 4/5] Update R/theme-current.R --- R/theme-current.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/theme-current.R b/R/theme-current.R index b5839bf6f5..8aef19236c 100644 --- a/R/theme-current.R +++ b/R/theme-current.R @@ -93,7 +93,7 @@ theme_get <- get_theme #' @rdname get_theme #' @param new new theme (a list of theme elements). Sets theme to the default ([theme_grey()]) if `new` isn't supplied. #' @export -set_theme <- function(new = null) { +set_theme <- function(new = NULL) { new <- new %||% ggplot_global$theme_default check_object(new, is_theme, "a {.cls theme} object") old <- ggplot_global$theme_current From 974a6c8c9cdcb4b77fd68492a8c0adb6bdb5f069 Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Fri, 19 Sep 2025 14:57:50 -0400 Subject: [PATCH 5/5] Redocument --- man/get_theme.Rd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/man/get_theme.Rd b/man/get_theme.Rd index 16710e6c1c..8e54a84d93 100644 --- a/man/get_theme.Rd +++ b/man/get_theme.Rd @@ -16,9 +16,9 @@ get_theme() theme_get() -set_theme(new = null) +set_theme(new = NULL) -theme_set(new = null) +theme_set(new = NULL) update_theme(...)