Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading