[R] Newbie mistakes saving images to files

Henrik Bengtsson hb at stat.berkeley.edu
Fri Jan 15 01:24:21 CET 2010


?png says:

"If you plot more than one page on one of these devices and do not
include something like %d for the sequence number in file, the file
will contain the last page plotted."

meaning

png("foo%03d.png", width=300, height=300);
plot(1); plot(2); plot(3);
dev.off();

generates foo001.png, foo002.png, foo003.png.  Work from this example,
and figure out what's missing in your code.  Your custom plot
functions probably don't generate new plots per se.

Personally, I always make sure to do one explicit pair of png() and
dev.off() calls per plot/image file.  That always works and makes it
more clear what is generated.

/Henrik

On Thu, Jan 14, 2010 at 2:17 PM, Rex C. Eastbourne
<rex.eastbourne at gmail.com> wrote:
> On Thu, Jan 14, 2010 at 2:15 PM, Rex C. Eastbourne <rex.eastbourne at gmail.com
>> wrote:
>
>> I am encountering problems using the png() function to save around 20
>> charts to separate files.
>>
>> My script is conceptually structured as follows:
>>
>> ### <script>
>> png("Image %03d.png")
>>
>> # the following are calls to user-defined functions I wrote that call plot,
>> barchart, etc. with special arguments.
>>  myPlot("a")
>> myPlot("b")
>> myPlot("c")
>>
>> myBarchart("a")
>> myBarchart("b")
>> myBarchart("c")
>>
>>  myBarchart2("a")
>> myBarchart2("b")
>> myBarchart2("c")
>>
>> dev.off()
>> ### </script>
>>
>> When I run the code a few lines at a time it generates the images
>> correctly. However, when I execute all lines of the script at once (Ctrl+A,
>> Ctrl+R in the Rgui editor), the output doesn't get generated right. Most
>> notably:
>>
>> - Only one plot created by myBarchart (or myBarchart2) gets saved, instead
>> of all 3
>> - Some images are blank
>>
>> I'm having trouble isolating the problem to one line of code.
>>
>> I suspect there is some fundamental paradigm I'm not aware of about how R
>> devices and images work, perhaps relating to the importance of the order in
>> which the calls to png, dev.off(), etc. are executed, the required state of
>> my workspace before I run the code (e.g. whether the previous plot window
>> should be open or closed), or maybe even time dependency (do I need to
>> insert timed pauses so that my diagram has time to render before saving to a
>> file?).
>>
>> Does this ring a bell to anyone? I read the R documentation on devices and
>> png(), and searched the mailing list archives and the web, but haven't
>> figured out how to get this working. Any help would be appreciated.
>>
>> Thanks,
>>
>> Rex
>>
>
> I'm on Windows 7 and R 2.9.2, BTW.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list