Home > Mobile >  Disabling only a single ggplot2/tidyverse lifecycle/deprecated warning? (Not all lifecycle warnings)
Disabling only a single ggplot2/tidyverse lifecycle/deprecated warning? (Not all lifecycle warnings)

Time:01-24

I have some R code that uses ggplot2 and may continue to use aes_string() for a while despite that function being deprecated. The code also uses source() to bring in some other functions that use aes_string(), which I do not have access to edit without making/maintaining a separate copy of the included code.

I know I can disable all tidyverse lifecycle messages with: rlang::local_options(lifecycle_verbosity = "quiet") but I don't necessarily want to disable all of them - I may still update other parts of the code to match changes in ggplot2, even while continuing to use aes_string().

Is there any way to disable the deprecated message only for aes_string() and not for everything else?

CodePudding user response:

Just wrap the aes_string() call in suppressWarnings():

library(ggplot2)

ggplot(mpg, aes_string("cty", "hwy"))  
  geom_point()
# Warning message:
# `aes_string()` was deprecated in ggplot2 3.0.0.
# ℹ Please use tidy evaluation ideoms with `aes()` 

ggplot(mpg, suppressWarnings(aes_string("cty", "hwy")))  
  geom_point()
# [no warning printed]

Or if you want to be sure to suppress only that specific warning, you could define a custom handler function like:

suppressDeprecationWarning <- function(...) {
  withCallingHandlers(
    ..., 
    warning = \(w) if ("`aes_string()` was deprecated in ggplot2 3.0.0." %in% w$message) {
      rlang::cnd_muffle(w)
    }
  )
}

ggplot(mpg, suppressDeprecationWarning(aes_string("cty", "hwy")))  
  geom_point()
# [no warning printed]
  • Related