When creating a set of two graphs, then printing inside a lapply loop will print twice in 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)
The code works ok, but it generates two sets of graphs in RStudio instead of only one.
How to avoid this behavior?
The object gets printed twice is because one output is from
lapply
and the other one is fromprint
. CheckHere, the first part of 1-5 is coming from
print
whereas the next part of 1-5 which is in a list is returned fromlapply
.From
?lapply
So when you apply
lapply
it returns the same object back which gets displayed and since theFUN
argument isprint
it applies that function to every object inlapply
thus printing it twice.The workaround as suggested by @www is use
print(collect.graph)
or justcollect.graph
in the console to print it only once.