Greg, others,

    Thanks for the info!  I suspect you are right, Greg.

    The main issue, as Sundar Dorai-Raj (who posts here at times) told me in
person and you say indirectly below, is that the "levelplot" function
returns an object and not a graph.  I didn't really know what that *meant*,
even though I'd read it, until he explained it to me.  In short, it means
that if you want levelplot to plot as usual, you need to wrap it in a print
statement (same as you're saying below).  I think this is hidden somewhere
in the FAQ's, but it still isn't obvious and it'd be great if this were
added directly in the ?levelplot help menu.  I'd originally assumed that
when something returned an object, it could simultaneously return both a
plot *and* an object.  Similar to how a "try" can return the evaluated
expression *and* the error code (being no error if the expression
evaluated).  This is not true, I guess.
    Once I did this print function wrap, all of the "dev.off()" calls worked
fine, and I never had anything weird happen when I'd attempted to comment
out a line.  Therefore, I suspect what you write below is correct.
Probably, since the levelplot call was "dangling", it saw the "#" on the
next line as a continuation of the levelplot, and this is why even when the
"dev.off()" was commented, it was messing things up.  I am not 100% sure,
but in the end, between you, Jim, and Sundar *I figured out all the problems
*.

                       Thanks to all!
                               Mike

On Mon, Jan 5, 2009 at 1:52 PM, Greg Snow <Greg.Snow@imail.org> wrote:

> I suspect that the graph not printing when the if statement follows the
> command (even commented out), may be faq 7.22, you are making the R parser
> guess at what you want and sometimes it guesses correctly, and sometimes
> not.  If you wrap your call to levelplot in a print function call, then R
> does not need to guess (this is good practice in any loops/scripts, even if
> it does not fix your problem).
>
> Hope this helps,
>
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow@imail.org
> 801.408.8111
>
>
> > -----Original Message-----
> > From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-
> > project.org] On Behalf Of Mike Williamson
> > Sent: Saturday, January 03, 2009 9:46 AM
> > To: jim holtman
> > Cc: r-help@r-project.org
> > Subject: Re: [R] How to capture multiple graph pages to .png ?
> >
> > Jim,
> >
> >     Thanks so much!  I feel kinda stupid now.  I read that part, but I
> > didn't really get what it was talking about... despite the fact that I
> > am
> > fairly familiar with the %d syntax when setting up formatting.
> >
> >                                           Regards,
> >                                                  Mike
> >
> > On Sat, Jan 3, 2009 at 8:36 AM, jim holtman <jholtman@gmail.com> wrote:
> >
> > > W.R.T. multiple pages, the HELP page says to use a filename like
> > this:
> > >
> > > png(filename = "Rplot%03d.png",
> > >
> > > where %03d is an incremented value for each page.
> > >
> > > On Fri, Jan 2, 2009 at 2:23 PM, Mike Williamson
> > <this.is.mvw@gmail.com>
> > > wrote:
> > > > Hello all and thanks in advance for any help,
> > > >
> > > >    There are really two issues that I am having, both related to
> > saving
> > > > graphs:
> > > > 1) It seems that the "dev...()" functions are unstable when using
> > higher
> > > > level graphs (specifically I was using levelplot)
> > > >
> > > > 2) I have a large grid of levelplot graphs that I can print out
> > into a
> > > > subset quite easily when I print to screen, but if I print to a
> > file, I
> > > > cannot print the subset.  (I mention .png in the subject because
> > that's
> > > what
> > > > I used, but really any format is acceptable to me).
> > > >
> > > > =========================================================
> > > >
> > > > 1) Unstable dev...() functions:
> > > >
> > > >
> > > > I have the following line of code embedded in a function using
> > simple
> > > "plot"
> > > > commands and it works fine:
> > > >
> > > >  if(!is.na(plotFileName)) dev.off()
> > > >
> > > >    I set "plotFIleName" as "NA" by default, in which case the plot
> > prints
> > > > to screen.  If a name is given, the print goes to the png()
> > function.
> > > > Therefore, I want to turn the device off to ensure that the next
> > plot
> > > isn't
> > > > sent to the same device & overwrites it.
> > > >    This works *perfectly fine* when using the plot function.  This
> > exact
> > > > same code, however, does not work for levelplot or (I am assuming)
> > any
> > > other
> > > > higher level trellis (sp!) graphs.  Below are snippets of the
> > entire
> > > code,
> > > > edited a bit for
> > > >
> > > >  if(!is.na(plotFileName)) {
> > > >    print(paste("Saving Zonal plot to file
> > ",plotFileName,".png",sep=""))
> > > >    print("(To display instead, do not pass any 'plotFileName'
> > argument to
> > > > cro.zone.plot function.)")
> > > >    maxLayout <- c(0,length(unique(miniDataSet$splitCol)))
> > > >    png(filename =
> > > >
> > >
> > paste(as.character(plotFileName),".png",sep=""),width=(1.2*plotSize*(ma
> > xLayout[2]^(1/3))),
> > > >        height=(plotSize*(maxLayout[2]^(1/3))))
> > > >  } else {
> > > >    devAskNewPage(ask = TRUE)
> > > >    maxLayout <- c(0,min(16,length(unique(miniDataSet$splitCol))))
> > > >  }
> > > >  levelplot(dataCol ~ siteX * siteY | splitCol,data = miniDataSet,
> > > >
> > > >
> > >
> > col.regions=rainbow(n=length(unique(miniDataSet$dataCol))+5,start=0.5,e
> > nd=0.95),
> > > >            layout = maxLayout,
> > > >            shrink = c(0.6,1.1),at =
> > > > keyCols,xlab=list(label=xlab,cex=labelScale),
> > > >            ylab=list(label=ylab,cex=labelScale),scales =
> > > > list(cex=axisScale),
> > > >            main=list(label=paste(dataCol,plotTitle,splitCol),
> > > >
> > cex=titleScale),colorkey=list(labels=list(cex=labelScale)))
> > > >
> > > >  if(!is.na(plotFileName)) dev.off()
> > > >
> > > > If I keep the code exactly as is, it will not work.  If I remove
> > *just
> > > the
> > > > last line*, the function will work, and then I simply have to
> > manually
> > > > remove the device "by hand" (also by using dev.off() ) outside of
> > the
> > > > function code.  Now the *really weird* thing:  if I comment out
> > that last
> > > > "if(!is.na... dev.off()" line, the function *still does not work*.
> > I
> > > > literally have to remove the line, I cannot simply comment it out!
> > This
> > > is
> > > > why I think it is "unstable".
> > > >    I have found a way around this (I simply put a loop in the front
> > of
> > > any
> > > > of my plotting functions to ensure they have turned off all devices
> > > before
> > > > doing anything... this seems to work), so this is not my biggest
> > problem,
> > > > personally.  But it does seem to be a bigger problem for R itself,
> > and
> > > for
> > > > this community.
> > > >    On a related note:  I have noticed that sometimes
> > "devAskNewPage(ask =
> > > > TRUE)" is also buggy.  Normally I can just alter the code slightly
> > to get
> > > it
> > > > to work, but the solutions are odd.  (For instance, I have found
> > that if
> > > > "devAskNewPage(ask = TRUE)" is in an "if" clause, it sometimes
> > won't work
> > > > unless I use the { } brackets around it, even with a one line if
> > clause.
> > > >
> > > > ========================================
> > > >
> > > > 2) Printing multiple pages to png:
> > > >
> > > > If you look through the code above, you'll see that I have altered
> > the
> > > size
> > > > of the grid that is printed, depending upon whether it is printed
> > to
> > > screen
> > > > or to png.  Specifically, look at:
> > > >
> > > >  if(!is.na(plotFileName)) {
> > > > ...
> > > >    maxLayout <- c(0,length(unique(miniDataSet$splitCol)))
> > > > ...
> > > >  } else {
> > > > ...
> > > >    maxLayout <- c(0,min(16,length(unique(miniDataSet$splitCol))))
> > > >  }
> > > >  levelplot(...layout = maxLayout...)
> > > >
> > > > The reason I do this is because when I print to screen, the
> > "askNewPage"
> > > > will allow me to look at the screen before the next one is printed.
> > I
> > > could
> > > > even screen capture that, if I wanted to choose a slow, ugly, and
> > > > non-scriptable solution.  But if I try to send to png, each of the
> > new
> > > pages
> > > > of the grid overwrite each other.
> > > >    Specifically, this levelplot is making a grid of ~ 60 graphs.
> > Since
> > > the
> > > > layout maxes to 16 graphs per page, it nicely forces it to a 4 x 4
> > matrix
> > > of
> > > > graphs, 4 pages long.  For the visual display, this is fine, as I
> > > mention.
> > > > For the save to png, only one of the pages is captured.  And,
> > oddly, it
> > > is
> > > > rarely the last page.  I don't quite understand this, but it is
> > usually
> > > > either the 2nd or the 3rd page that is saved to the png.
> > > >    You can see my solution to it in the code above:  when saving to
> > png,
> > > > all of the graphs are printed out in a gigantic grid, but I also
> > increase
> > > > the height & width of the graph set so that each individual plot
> > isn't
> > > too
> > > > tiny.  It isn't a totally useless solution, but still not ideal.
> > > >
> > > >    I think I just don't quite understand how to use these
> > "devices", as
> > > > they are called, and that is the root of the problem.  In fact, I
> > don't
> > > even
> > > > know how to bring one device (say, for instance, the device holding
> > the
> > > png
> > > > graph) to another device type.  E.g., a solution I was trying to
> > figure
> > > out
> > > > was to simply print everything to screen and then switch the screen
> > > device
> > > > to png.  This still isn't ideal because it requires constant user
> > > > interaction, but it nonetheless illuminated the fact that I don't
> > > understand
> > > > how to manage the devices.
> > > >
> > > >                                     Thanks in advance for any help!
> > > >                                                           Mike
> > > >
> > > >        [[alternative HTML version deleted]]
> > > >
> > > > ______________________________________________
> > > > R-help@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.
> > > >
> > >
> > >
> > >
> > > --
> > > Jim Holtman
> > > Cincinnati, OH
> > > +1 513 646 9390
> > >
> > > What is the problem that you are trying to solve?
> > >
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@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.
>



-- 
The official inflation rate in Zimbabwe has soared to about 231 million
percent while thousands of Zimbabweans stand in line for their daily
allowance of about 2 cents a day -- from their own bank accounts. The
allowance does not afford them a half loaf of bread.

	[[alternative HTML version deleted]]


