Skip to content

Instantly share code, notes, and snippets.

@stephhazlitt
Created July 11, 2018 17:48
Show Gist options
  • Save stephhazlitt/cef14d7dcc5a72648cd18b45eb61b7bd to your computer and use it in GitHub Desktop.
Save stephhazlitt/cef14d7dcc5a72648cd18b45eb61b7bd to your computer and use it in GitHub Desktop.
envreportutils::png_retina & purrr troubles
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()
})
@ateucher
Copy link

Works:

lapply(names(plots), function(x) {
  png(filename = paste0(x, "_retina.png"))
  plot(plots[[x]])
  dev.off()
})

Doesn't work:

lapply(names(plots), function(x) {
  png_retina(filename = paste0(x, "_retina.png"))
  plot(plots[[x]])
  dev.off()
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment