I have a function for ploting like this:
f <- function(x,y){
for (i in 1:10){
png(getwd(), height=1500, width=500)
plot(x,y)
dev.off()
}
}
everything is all right when I use:
f(2,3)
but if I use f(2,"a") then the function is breaking before dev.off()
I can place on.exit(dev.off(), add=T) on the begening of the function but then in normal execution I will dev.off() too much 1 time.
I think the solution is to put sth like this to the function:
on.exit(if(dev != NULL) dev.off(), add=T)
but how to check that dev != NULL ?
dev.list()This doesnt work:on.exit(if(exists(dev.list()) == T){ dev.off()}, add=T)on.exit(if(is.null(dev.list()) == F){ dev.off()}, add=T)this is OK ?