Created
July 11, 2018 17:48
-
-
Save stephhazlitt/cef14d7dcc5a72648cd18b45eb61b7bd to your computer and use it in GitHub Desktop.
envreportutils::png_retina & purrr troubles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(ggplot2) | |
library(purrr) | |
# make a small df | |
planets_i_know <- c("Tatooine", "Naboo", "Alderaan", "Endor") | |
data <- starwars %>% | |
filter(!is.na(homeworld)) %>% | |
group_by(homeworld, gender) %>% | |
summarise(num_char = n()) %>% | |
filter(homeworld %in% planets_i_know) | |
# plot for one planet | |
plot <- data %>% | |
filter(homeworld == "Tatooine") %>% | |
ggplot() + | |
geom_col(aes(gender, num_char)) + | |
labs(title = "Tatooine") | |
# plots for all planets - saved to a list object | |
plots <- | |
data %>% | |
split(.$homeworld) %>% | |
map(~ ggplot(.) + | |
geom_col(aes(gender, num_char)) + | |
labs(title = .$homeworld)) | |
# look at one plot in list object | |
plots[["Tatooine"]] | |
# look at all the plots in the list object | |
walk(plots, ~ { | |
plot(.x) | |
}) | |
# save plots in list as pngs using ggsave() - THIS WORKS | |
iwalk(.x = plots, | |
~ ggsave(filename = paste0(.y, "_ggsave.png"), | |
plot = .x)) | |
# save plots in list as pngs using png() - THIS WORKS | |
iwalk(.x = plots, | |
~ {png(filename = paste0(.y, "_png.png")) | |
plot(.x) | |
dev.off() | |
}) | |
# save plots in list as pngs using envreportutils::png_retina() - THIS DOES NOT WORK | |
library(envreportutils) #installed with remotes::install_github("bcgov/envreportutils") | |
iwalk(.x = plots, | |
~ {png_retina(filename = paste0(.y, "_retina.png")) | |
plot(.x) | |
dev.off() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works:
Doesn't work: