I think your confusion is because of how pwalk (& pmap etc) is passing the data into the function, especially after the nest call. They all pass a single list into the .f argument, unlike map2 etc, so when you use a ~ function the .f arguments for the two are:
function(..., .x = ..1, .y = ..2, . = ..1) {
# do something here
}
You're also currently trying to use the '.' to reference the data frame within the called function (after the ~) which won't work as you now have a different context - in a ~ function the '.' now refers to the first argument, which walks across the unique values of 'country' in your pwalk example. You could learn more about the new context by running:
data.frame(mean=c(0),sd=c(1)) %>%
map2(.x=.$mean,.y=.$sd,~{
browser()
}, n=10)
and
gapminder %>%
group_by(country) %>%
nest() %>%
pwalk(~{
browser()
})
and these will probably work:
gapminder %>%
group_by(country) %>%
nest() %>%
pwalk(~write_csv(list(...)$data, paste0(list(...)$country, ".csv")))
gapminder %>%
group_by(country) %>%
nest() %>%
pwalk(~write_csv(tibble(...)$data, paste0(tibble(...)$country[1], ".csv")))
gapminder %>%
group_by(country) %>%
nest() %>%
pwalk(~write_csv(.y$data, paste0(.x, ".csv")))
gapminder %>%
group_by(country) %>%
nest() %>%
pwalk(~write_csv(..2$data, paste0(..1, ".csv")))
gapminder %>%
group_by(country) %>%
nest() %>%
select(data, country) %>%
pwalk(~write_csv(x = . , path = paste0(.y, ".csv") )
gapminder %>%
group_by(country) %>%
nest() %>%
pwalk(function(country, data, ...) {
write_csv(data, paste0(country, ".csv"))
})
I have not tested these, & they may not all work, but it'll give you some idea how to troubleshoot these things in future. Hope it helps.
Edit: now tested & errors fixed!