【发布时间】:2020-04-06 14:14:38
【问题描述】:
我有一个有时会返回 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))
【问题讨论】:
-
谢谢。您能否发表您的评论作为答案,因为我发现解释和答案非常好?