A member of the R mailing list suggests using the dev.off() statement when writing R graphics to a file (see the "Saving Your R Work" section). The dev.off() statement closes R graphics devices.
While the q() statement does close the output graphics file (because R by default flushes buffers and closes open files as it shuts down), the proper way to close the file is to apply the dev.off() statement.
For example, the script that writes a PNG file of the S&P500 graph should be modified to be:
dv <- read.table("./SP500.txt", header=FALSE)