Created
October 27, 2017 19:12
-
-
Save bhive01/de0a56aac598c6fc69ae7982d479cadf to your computer and use it in GitHub Desktop.
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(tidyverse) | |
# This is the example I used to show pwalk iterating and printing out things | |
#perhaps this is a horrible example and I'm using it incorrectly? | |
# if you substitute pmap for pwalk, you get exactly the same output. | |
# make a simple plotting function | |
plot_points <- function(t) ggplot(t, aes(mpg, wt)) + geom_point() | |
#function that prints cylinder info, ggplot, and table | |
print_func <- | |
function(info, plot, table) { | |
print(paste0("This is for ", info, " cylinders.\n")) | |
print(plot) | |
print(table) | |
} | |
mtc_plot_n_save <- | |
mtcars %>% # motortrend car dataset | |
group_by(cyl) %>% # group by cylinder | |
nest(.key = "data") %>% # nest by cylinder | |
mutate(plot = map(data, plot_points)) %>% # we made a graph for each subset | |
mutate(filepath = map_chr(cyl, ~ file.path(getwd(), paste0(.x, ".png")))) #make the save paths | |
# print all the info | |
pwalk(list(info = mtc_plot_n_save$cyl, plot = mtc_plot_n_save$plot, table = mtc_plot_n_save$data), print_func) | |
# print it all with pmap | |
pmap(list(info = mtc_plot_n_save$cyl, plot = mtc_plot_n_save$plot, table = mtc_plot_n_save$data), print_func) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment