Skip to content
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

create.dir argument for ggsave() #5492

Merged
merged 8 commits into from
Oct 30, 2023

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Oct 24, 2023

This PR aims to fix #5489.

Briefly, it adds a create.dir argument to ggsave(), which controls whether a new directory can be created.
As an implementation detail, I've isolated all the path checking into its own function.

Example:

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

p <- ggplot()

ggsave("foobar/test.png", plot = p)
#> Error in `ggsave()`:
#> ! Cannot find directory 'foobar'.
#> ℹ Please supply an existing directory or use `create.dir = TRUE`.

ggsave("foobar/test.png", plot = p, create.dir = TRUE)
#> ✔ Created directory: 'foobar'.
#> Saving 7 x 5 in image

Created on 2023-10-24 with reprex v2.0.2

Because the default create.dir = FALSE might be pain for interactive use, it prompts a small menu choice in interactive mode (which I don't know how to reprex)

ggsave("foobar/test.png", plot = p)
#> Cannot find directory foobar.
#> ℹ Would you like to create a new directory?
#>
#> 1: Yes
#> 2: No
#>
#> Selection: 1
#> ✔ Created directory: foobar.
#> Saving 7 x 7 in image

Copy link
Contributor

@olivroy olivroy left a comment

Choose a reason for hiding this comment

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

I think that it would be better to pass down the error call to the type checks as well!

R/save.R Outdated Show resolved Hide resolved
R/save.R Outdated Show resolved Hide resolved
R/save.R Outdated Show resolved Hide resolved
@teunbrand
Copy link
Collaborator Author

Thanks for these suggestions olivroy!

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

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

LGTM - maybe add a check for whether create.dir is NA

@teunbrand
Copy link
Collaborator Author

There is a check_bool(create.dir, call = call) in there that disallows NA values for create.dir.

@teunbrand teunbrand merged commit 916dc89 into tidyverse:main Oct 30, 2023
12 checks passed
@teunbrand teunbrand deleted the ggsave_directories branch October 30, 2023 11:01
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.

ggsave creating new directories when saving jpg/tiff/png devices
3 participants