【发布时间】:2019-06-28 19:34:26
【问题描述】:
当创建一组两个图形时,在 lapply 循环内打印将在 RStudio Plots Panel 中打印两次。
x=1:7
y=1:7
df1 = data.frame(x=x,y=y)
x=10:70
y=10:70
df2 = data.frame(x=x,y=y)
db <- list(df1, df2)
# Given a data frame, the function below creates a graph
create.graph <- function (df){
p <- ggplot(df,aes(x,y))+geom_point()
# here goes other stuff, such as ggsave()
return (p)
}
# collect.graph is a list of generated graphs
collect.graph <- lapply(db,create.graph)
# Finally, lapply prints the list of collected graphs
lapply(collect.graph,print)
代码工作正常,但它在 RStudio 中生成了两组图形,而不是只生成一组。
如何避免这种行为?
【问题讨论】:
-
只要
print(collect.graph)。 -
Ronak,我想打印两个对象。但是,当我出于某种原因使用 lapply 时,它会打印两个对象两次。 @www 的建议解决了这个问题。但是我还是不明白为什么 lapply 不能正常工作...谢谢。