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 from print
. Check
lapply(1:5, print)
#[1] 1
#[1] 2
#[1] 3
#[1] 4
#[1] 5
#[[1]]
#[1] 1
#[[2]]
#[1] 2
#[[3]]
#[1] 3
#[[4]]
#[1] 4
#[[5]]
#[1] 5
Here, the first part of 1-5 is coming from print
whereas the next part of 1-5 which is in a list is returned from lapply
.
From ?lapply
lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.
So when you apply lapply
it returns the same object back which gets displayed and since the FUN
argument is print
it applies that function to every object in lapply
thus printing it twice.
The workaround as suggested by @www is use print(collect.graph)
or just collect.graph
in the console to print it only once.