r4ds icon indicating copy to clipboard operation
r4ds copied to clipboard

Suggestion: replace `pwalk(list(...), ...)` by `walk2(..., ...)` in iteration chapter

Open prosoitos opened this issue 6 years ago • 1 comments

Current code:

library(ggplot2)
plots <- mtcars %>% 
  split(.$cyl) %>% 
  map(~ggplot(., aes(mpg, wt)) + geom_point())
paths <- stringr::str_c(names(plots), ".pdf")

pwalk(list(paths, plots), ggsave, path = tempdir())

Suggestion (watch the last line):

library(ggplot2)
plots <- mtcars %>% 
  split(.$cyl) %>% 
  map(~ggplot(., aes(mpg, wt)) + geom_point())
paths <- stringr::str_c(names(plots), ".pdf")

walk2(paths, plots, ggsave, path = tempdir())

Rationale:

  • simpler code
  • no reason to use pwalk() while there are only 2 lists

Note:

I submitted PR #668 with typos corrections and I push to it whenever I find new typos. If you agreed with this change, I could push it to that PR as well.

prosoitos avatar Apr 09 '18 19:04 prosoitos

Could you please do a new PR?

hadley avatar Jun 20 '18 09:06 hadley