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

How do you suppress the ggdark warning message when using a function in lapply

I’m plotting my way through a list of data using pblapply. The plots use ggdark, which prints an error message each time it plots. It’s messing up my progress bar for pblapply. I’ve read around and tried suppressWarnings(), but it doesn’t work. Any ideas for how to suppress this warning message?

Inverted geom defaults of fill and color/colour.
To change them back, use invert_geom_defaults().

Here’s a demo:

library(ggplot2)
library(pbapply)
library(ggdark)

# Make a list
p = split(diamonds, f=diamonds$cut)

# Open pdf
pdf("diamonds.pdf", onefile = T)

# Iterate through the list
pblapply(p, function(x) {
  
  print(
    ggplot(x) + 
      geom_point(aes(carat, price, color = cut)) + 
      scale_y_continuous(label = scales::dollar) +
      guides(color = guide_legend(reverse = TRUE)) +
      labs(title = "Prices of 50,000 round cut diamonds by carat and cut",
           x = "Weight (carats)",
           y = "Price in US dollars",
           color = "Quality of the cut") +
      dark_theme_bw()
  )
  
  # Revert to geom default
  invert_geom_defaults()
  
})

# Close pdf
dev.off()

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 :

It’s not a warning, it’s a message. You need suppressMessages rather than suppressWarnings

pblapply(p, function(x) {
  
  print(
    suppressMessages(
    ggplot(x) + 
      geom_point(aes(carat, price, color = cut)) + 
      scale_y_continuous(label = scales::dollar) +
      guides(color = guide_legend(reverse = TRUE)) +
      labs(title = "Prices of 50,000 round cut diamonds by carat and cut",
           x = "Weight (carats)",
           y = "Price in US dollars",
           color = "Quality of the cut") +
      dark_theme_bw()
  )
  )
  
  # Revert to geom default
  invert_geom_defaults()
  
})
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