Skip to content

Handle filename better in ggsave() #5116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2022

Conversation

teunbrand
Copy link
Collaborator

This PR comes out of the discussion in #5114.

ggsave() gives some unhelpful error messages when the filename argument was inappropriate:

library(ggplot2)

p <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
filenames <- c("plot1.png", "plot2.png")

# Casually plotting
ggsave(filenames, p)
#> Error in `ggsave()`:
#> ! `device` must be "NULL", a string or a function.

# Alright, let's try to fix the above error...
ggsave(filenames, p, "png")
#> Saving 7 x 5 in image
#> Error in if (!dir.exists(dir)) {: the condition has length > 1

# Might programmatically provide 0-length vector for some reason
ggsave(character(), p, "png")
#> Saving 7 x 5 in image
#> Error in if (!dir.exists(dir)) {: argument is of length zero

With this PR:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggsave(filenames, p)
#> Warning: `filename` must have length 1, not length 2.
#> ! Only the first, 'plot1.png', will be used.
#> Saving 7 x 5 in image

ggsave(character(), p, "png")
#> Error in `ggsave()`:
#> ! `filename` cannot be empty.

Created on 2022-12-22 by the reprex package (v2.0.1)

Copy link
Member

@yutannihilation yutannihilation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@yutannihilation yutannihilation merged commit ef00be7 into tidyverse:main Dec 25, 2022
@teunbrand teunbrand deleted the ggsave_warn_filename branch December 25, 2022 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants