Re: [R] jpeg() creating empty files with qplot() in a loop

From: Henrik Bengtsson <hb_at_stat.berkeley.edu>
Date: Thu, 21 Feb 2008 12:17:33 -0800

First,

please provide us with enough unambiguous information so we don't have to second guess that you are using qplot() in the 'ggplot' package. Report sessionInfo() is recommended.

It has nothing to do with jpeg() or any other device driver. You get the same problem with:

library(ggplot)
x <- y <- 1:10
for (kk in 1:3) { qplot(x,y,main=kk) }

or

foo <- function(x,y,...) {
  qplot(x,y, ...)
  invisible(FALSE)
}
foo(x,y)

You have to explicitly print() the value that qplot() returns, e.g.

for (kk in 1:3) { print(qplot(x,y,main=kk)) }

foo <- function(x,y,...) {
  print(qplot(x,y, ...))
  invisible(FALSE)
}
foo(x,y)

Your last example worked only because you did it at the prompt.

/Henrik

On Thu, Feb 21, 2008 at 10:51 AM, B. Bogart <bbogart_at_sfu.ca> wrote:
> Hello all,
>
> I'm stuck with a strange issue with writing jpegs of plots to a folder
> in a loop.
>
> This works:
>
> for (step in 1:length(steps)) {
> jpeg(filename=paste("frame_",sprintf("%05d",step),".jpg",sep=""))
> plot(steps[[step]])
> dev.off()
> }
>
> But if I use qplot to generate the plot (which is my aim):
>
> for (step in 1:length(steps)) {
> jpeg(filename=paste("frame_",sprintf("%05d",step),".jpg",sep=""))
> qplot(x, y, data=steps[[step]], geom="tile", fill=rgb(V1,V2,V3)) +
> scale_fill_identity() + opts(aspect.ratio = .75)
> dev.off()
> }
>
> I end up with a directory of empty files named correctly.
>
> The following does work:
>
> step <- 10
> jpeg(filename=paste("frame_",sprintf("%05d",step),".jpg",sep=""))
> qplot(x, y, data=steps[[step]], geom="tile", fill=rgb(V1,V2,V3)) +
> scale_fill_identity() + opts(aspect.ratio = .75)
> dev.off()
>
> So what could be the difference between the last example and it running
> in a loop, that the former generates a proper jpeg file and the lattter
> does not?
>
> Does something more special than dev.off() need to be done with qplot
> output when in a loop?

>
> Thanks,
> B. Bogart
>
> ______________________________________________
> 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.
>



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. Received on Thu 21 Feb 2008 - 20:38:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Feb 2008 - 21:30:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive