我有一个函数,有时会返回NULL,并且我尝试稍后使用pmap传递它。当我直接调用相同的函数时,它能够正常工作,但是在使用pmap时却不行。这是预期的吗?如果是,为什么?有没有解决方法?
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))