I don't have a complete answer for you but I can show some things perhaps.
.data can be used in an anonymous function :
vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length")
p <- map(vars, function(my_var) {
iris %>%
ggplot() +
aes(x = .data[[my_var]]) +
geom_histogram()
})
The difference between this and your original is that there is not a context of making a tibble ; I think its therefore something in how the tibble is constructed.
Here's some more playing around with this that I've done:
library(tidyverse)
# This doesn't quite give you the output you suggest, but it shows how the `.data` pronoun can be used in an anonymous function
vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length")
plots <- map(vars, function(var) {
iris %>% ggplot(aes(x = .data[[var]])) + geom_histogram()
})
# however it doesn't seem to work well within the tibble structure you suggest
iris_plots <-
tibble(
vars = vars,
plots = map(vars, function(var) {
iris %>% ggplot(aes(x = .data[[var]])) + geom_histogram()
})
)
iris_plots$plots
#> [[1]]
#> Error: Unknown input: function
# I thought it might work better with `mutate`, but no
iris_plots <-
tibble(
vars = vars) %>%
mutate(plots = across(vars, function(var) {
iris %>% ggplot(aes(x = .data[[var]])) + geom_histogram()
})
)
#> Error: Problem with `mutate()` column `plots`.
#> i `plots = across(...)`.
#> x Input must be a vector, not a <gg/ggplot> object.
# however you can just make your tibble like this:
iris_plots <-
tibble(
vars = vars,
plots = plots
)
iris_plots$plots
#> [[1]]
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#>
#> [[2]]
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#>
#> [[3]]
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.