I have a function that sometimes returns NULL and I try to pass it later on using pmap. When I call the same function directly it works fine, but not with pmap. Is this expected, if so, why? Any workaround?
library(tidyverse)
plot_fun <- function(data, color_by){
plot <- ggplot(data, aes_string(x = 'Sepal.Length',
y = 'Sepal.Width',
color = color_by)) +
geom_point()
return(plot)
}
# works fine:
plot_fun(iris, 'Species')
plot_fun(iris, NULL)
pmap(list(list(iris), 'Species'), plot_fun)
# does not work:
pmap(list(list(iris), NULL), plot_fun)
pmap(list(list(iris), NULL), ~plot_fun(..1, ..2))