Understanding purrr::map function

So, I'm taking the "Writting functions in R" course in DataCamp.

I was trying to reproduce the code in the image, but cyl is not present after loading purrr or ggplot2.

How could I make it available in my enviroment? It is not very clear from the video.

It's likely that cyl was defined on a previous slide(s). It should be a named list (based on the fact that it is used with map). Since mtcars is being used, I would guess that there was some grouping on cyl from mtcars done previously. Something like that:

cyl <- mtcars %>%
  dplyr::group_by(cyl) %>%

cyl_named <- dplyr::pull(cyl, data) %>%

Then you can use code from the slide like that:

plots <- cyl_named %>%
  map(~ggplot(., aes(mpg, wt)) + geom_point())
1 Like

I think @mishabalyasin hit it there. The table mtcars is included in the datasets library, which is included in base-R. And is frequently used in learning examples.
(In fact, I think you can refer to mtcars without having to explicitly load library(datasets) )

As @o_gonzales mentions, this question comes from DataCamp's Writing Functions in R course by Hadley and Charlotte Wickham at www.datacamp.com/courses/writing-functions-in-r/. I personally think this particular course is really useful.

I also wanted to note community's homework policy: FAQ: Homework Policy. The gist is that one shouldn't post questions or materials verbatim. Homework inspired questions, like this one, are certainly welcome.