r - How do pipes work with purrr map() function and the "." (dot) symbol -
when using both pipes , map() function purrr, confused how data , variables passed along. instance, code works expect:
library(tidyverse) cars %>% select_if(is.numeric) %>% map(~hist(.))
yet, when try similar using ggplot, behaves in strange way.
cars %>% select_if(is.numeric) %>% map(~ggplot(cars, aes(.)) + geom_histogram())
i'm guessing because "." in case passing vector aes(), expecting column name. either way, wish pass each numeric column ggplot function using pipes , map(). in advance!
cars %>% select_if(is.numeric) %>% map2(., names(.), ~{ggplot(data_frame(.x), aes(.x)) + geom_histogram() + labs(x = .y) })
there's few steps.
- use
map2
instead ofmap
. first argument dataframe you're passing it, , second argument vector ofnames
of dataframe, knowsmap
over. - you need explicitly enframe data, since
ggplot
works on dataframes , coercible objects. - this gives access
.y
pronoun name plots.
Comments
Post a Comment