From cd458171d68cadeaa53850dfb4ec08457ca27b94 Mon Sep 17 00:00:00 2001 From: alice-hannah Date: Thu, 7 Aug 2025 11:52:57 +0100 Subject: [PATCH] Add quietly arg to use_sgplot and test --- NEWS.md | 3 +++ R/use_sgplot.R | 20 +++++++++++++++----- man/use_sgplot.Rd | 11 ++++++++++- tests/testthat/test-use_sgplot.R | 4 ++++ 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/test-use_sgplot.R diff --git a/NEWS.md b/NEWS.md index 5150a18..fe70fc8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # sgplot (development version) +* Add `quietly` argument to `use_sgplot()` to allow messages to be suppressed +(#67). + * Set minimum version of ggplot (>= 3.5.0) (#62). * Fix bug where `use_sgplot()` could not handle passing multiple arguments of diff --git a/R/use_sgplot.R b/R/use_sgplot.R index f64396a..d9f6bb4 100644 --- a/R/use_sgplot.R +++ b/R/use_sgplot.R @@ -5,6 +5,9 @@ #' #' @param palette_type Name of palette type to use. Defaults to "sg". For all #' available palette types, run `available_palette_types()`. +#' @param quietly Should the function run quietly? By default, `use_sgplot()` +#' prints information messages to the console. Set `quietly = TRUE` to suppress +#' these. #' @param ... Arguments passed to \code{sgplot::theme_sg()}. #' @param default_colour `r lifecycle::badge("deprecated")` Use the #' `palette_type` argument instead. @@ -35,6 +38,7 @@ use_sgplot <- function(palette_type = "sg", + quietly = FALSE, ..., default_colour = deprecated()) { @@ -56,8 +60,9 @@ use_sgplot <- function(palette_type = "sg", ggplot2::theme_set(theme_sg(...)) - cli::cli_alert_info("Default ggplot2 theme set to `theme_sg`.") - + if (!quietly) { + cli::cli_alert_info("Default ggplot2 theme set to `theme_sg`.") + } # Use sgplot colour palette ---- @@ -76,8 +81,11 @@ use_sgplot <- function(palette_type = "sg", } ) - cli::cli_alert_info("Default colours set to {.str {palette_type}} palettes.") - + if (!quietly) { + cli::cli_alert_info( + "Default colours set to {.str {palette_type}} palettes." + ) + } # Set default geom characteristics ---- @@ -148,6 +156,8 @@ use_sgplot <- function(palette_type = "sg", size = base_size / 8) ) - cli::cli_alert_info("Default geom aesthetics set.") + if (!quietly) { + cli::cli_alert_info("Default geom aesthetics set.") + } } diff --git a/man/use_sgplot.Rd b/man/use_sgplot.Rd index dbd8ed0..5946530 100644 --- a/man/use_sgplot.Rd +++ b/man/use_sgplot.Rd @@ -4,12 +4,21 @@ \alias{use_sgplot} \title{Use sgplot defaults.} \usage{ -use_sgplot(palette_type = "sg", ..., default_colour = deprecated()) +use_sgplot( + palette_type = "sg", + quietly = FALSE, + ..., + default_colour = deprecated() +) } \arguments{ \item{palette_type}{Name of palette type to use. Defaults to "sg". For all available palette types, run \code{available_palette_types()}.} +\item{quietly}{Should the function run quietly? By default, \code{use_sgplot()} +prints information messages to the console. Set \code{quietly = TRUE} to suppress +these.} + \item{...}{Arguments passed to \code{sgplot::theme_sg()}.} \item{default_colour}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Use the diff --git a/tests/testthat/test-use_sgplot.R b/tests/testthat/test-use_sgplot.R new file mode 100644 index 0000000..a42c586 --- /dev/null +++ b/tests/testthat/test-use_sgplot.R @@ -0,0 +1,4 @@ +test_that("`quietly` argument works", { + expect_message(use_sgplot()) + expect_no_message(use_sgplot(quietly = TRUE)) +})