Too many open devices r
You are supposed to put your jpeg
command before you call your plot
command.
jpeg(file=mypath)
plot(...)
dev.off()
Combination of the answers from Mohammed Shaker and asb gives this really simple code:
for (i in dev.list()[1]:dev.list()[length(dev.list())]) {
dev.off()
}
This removes all open graphics devices:
> dev.list()
NULL