I have a for loop producing a series of plots and want to plot each one to a file, such as in this answer. The problem is that I don't actually create a new plot every new iteration, but insert an overlapping line on the existing canvas. The code and error are exemplified below:
for(i in 1:10){
png(filename = paste0("~/image", i))
if(i == 1){
plot(runif(10))
}else{
lines(runif(10))
}
dev.off()
}
error message:
Error in plot.xy(xy.coords(x, y), type = type, ...) :
plot.new has not been called yet
I understand that it is because the canvas e not recreated, but do not know how to overcome this issue. I have tried using dev.print() with no success either.