I want to invoke the function wrap_vr using map or pmap of the purrr library.
First, I don't understand why I have to use df$v1 and df$v2 to pass the variables to the function. Why not v1 and v2 only?
Second, what is my error when I tried to use pmap?
library(tidyverse)
df <- tibble(cy = c('a', 'a', 'b', 'b'),
date = c(1,2,1,2),
v1 = c(1,2,3,1),
v2 = c(5,3,2,1))
wrap_vr <- function(df, vr, tit, ylab){
ggplot(data = df, aes(date, all_of(vr))) +
geom_line(color = "steelblue", size = 1) +
labs(title = tit,
y = ylab, x = "") +
facet_wrap(~ cy)
}
wrap_vr(df, df$v1, "title_1", "ylabel_1")
wrap_vr(df, df$v2, "title_2", "ylabel_2")
list_1 <- list(df, list(df$v1, df$v2), list("title_1", "title_2"), list("ylabel_1", "ylabel_2"))
# This gives an error
pmap(list_1, ~wrap_vr(.x))
#> Error: Element 2 of `.l` must have length 1 or 4, not 2
Created on 2021-07-06 by the reprex package (v2.0.0)



