Thanks Michael.

That did the trick. Despite googling most of the day yesterday, I didn't
quite have the right search string to find that one. Almost feels like the
answer was hiding in plain sight, now that you point me to it.

I added some code to save the xyplots to a variable and then print it at
the end of the function.

before:

xyplot(...)

after:

xyp<-xyplot(...)
print(xyp)

Works great.

Regards,

-mike


On Sun, Jan 1, 2012 at 1:40 AM, R. Michael Weylandt <
michael.weylandt@gmail.com> wrote:

> I'm guessing R FAQ 7.22: http://cran.r-project.org/doc/FAQ/R-FAQ.html
>
> The subtlety is that in an interactive session print is automatically
> called at the final evaluation of most everything, but you have to
> prompt it in interactive use (and depending on details, in some
> function calls)
>
> Michael Weylandt
>
> On Sat, Dec 31, 2011 at 6:50 PM, Mike Dahman <mike.dahman@gmail.com>
> wrote:
> > New years greetings.
> >
> > I have been setting up a function to generate multiple jpeg charts. When
> > the calls are issued at the interactive console, the jpeg files are
> > generated without an issue. When I try to issue the same calls from a
> > function, some chart files are empty. It appears to only be related to
> > trellis charts. Any help to troubleshoot this is appreciated.
> >
> > Regards,
> >
> > -mike
> >
> >
> > R version 2.14.0 (2011-10-31)
> > Copyright (C) 2011 The R Foundation for Statistical Computing
> > ISBN 3-900051-07-0
> > Platform: x86_64-unknown-linux-gnu (64-bit)
> >
> >
> > # validate devices
> >> capabilities()
> >    jpeg      png     tiff    tcltk      X11     aqua http/ftp  sockets
> >    TRUE    FALSE    FALSE    FALSE     TRUE    FALSE     TRUE     TRUE
> >  libxml     fifo   cledit    iconv      NLS  profmem    cairo
> >    TRUE     TRUE     TRUE     TRUE     TRUE    FALSE    FALSE
> >
> > # Example functionality from the interactive console::::
> >
> > # I am going to use a zone variable to help duplicate the code in the
> > function
> >> zone
> > [1] "isoranp-z1"
> >
> > # call a function to pull in data and assign it to a data frame
> >> testz<-get_zonedata_url(2011,51,zone)
> >
> > # validate the data frame
> >> str(testz)
> > 'data.frame':   2016 obs. of  14 variables:
> >  $ ts     : Factor w/ 2016 levels "12/18/2011 00:00",..: 1 2 3 4 5 6 7 8
> 9
> > 10 ...
> >  $ server : Factor w/ 1 level "phx1npf4sn2": 1 1 1 1 1 1 1 1 1 1 ...
> >  $ zone   : Factor w/ 1 level "isoranp-z1": 1 1 1 1 1 1 1 1 1 1 ...
> >  $ pool   : Factor w/ 1 level "ORA-S1": 1 1 1 1 1 1 1 1 1 1 ...
> >  $ cpucap : num  4 4 4 4 4 4 4 4 4 4 ...
> >  $ memcap : int  26 26 26 26 26 26 26 26 26 26 ...
> >  $ swapcap: int  26 26 26 26 26 26 26 26 26 26 ...
> >  $ cpu    : num  78.2 206.8 198.4 366.4 112.1 ...
> >  $ poolsz : int  42 42 42 42 42 42 42 42 42 42 ...
> >  $ mem    : num  75.5 75.3 75.6 74.5 74.3 ...
> >  $ swp    : num  80.2 80.1 79.6 79 78.9 ...
> >  $ dates  : chr  "12/18/2011" "12/18/2011" "12/18/2011" "12/18/2011" ...
> >  $ times  : chr  "00:00:00" "00:05:00" "00:10:00" "00:15:00" ...
> >  $ dt     :Classes 'chron', 'dates', 'times'  atomic [1:2016] 15326 15326
> > 15326 15326 15326 ...
> >  .. ..- attr(*, "format")= chr [1:2] "m/d/y" "h:m:s"
> >  .. ..- attr(*, "origin")= Named num [1:3] 1 1 1970
> >  .. .. ..- attr(*, "names")= chr [1:3] "month" "day" "year"
> >
> > # set up a jpeg device
> >> trellis.device(jpeg,file=paste("charts/",zone,".zone_util.jpg",sep=""))
> >
> > # call a function to generate the chart
> > # the plot_zone_util function uses xyplot
> >> plot_zone_util(testz)
> >
> > #close the device
> >> dev.off()
> >
> > The jpeg file is generated as expected:
> > (User and group has been removed)
> >
> > ls -l isoranp-z1.zone_util.jpg
> > -rw-rw-r-- 1 <> <> 32123 Dec 31 16:13 isoranp-z1.zone_util.jpg
> >
> > # here is the plot_zone_util function for reference
> > plot_zone_util <- function(zone_df){
> >
> >    xyplot(cpu~dt|server,data=zone_df,ylim=c(0,100),
> >           main=paste(zone_df$zone[1]," CPU (Blue) & Memory (Red)
> > Util\n",zone_df$ts[1],"-",zone_df$ts[nrow(zone_df)],sep=""),
> >           xlab="date",
> >           ylab="utilization (%)",
> >           panel=function(x,y,subscripts){
> >               panel.lines(x,y)
> >
> >  panel.lines(zone_df$dt[subscripts],zone_df$mem[subscripts],col="red")
> >    }, as.Table=T, subscripts=T)
> > }
> >
> >
> > ##############################################
> >
> > # Try and do the same thing within a function:::
> >
> >> gen_zone_charts(zone,2011,51)
> >
> > # Note the zone_util.jpg file is zero length. It is a lattice chart.
> > The other two charts generate ok. User and group has been removed.
> >
> > -rw-rw-r-- 1 <> <> 22376 Dec 31 16:20 isoranp-z1.zone_cpu.jpg
> > -rw-rw-r-- 1 <> <> 18910 Dec 31 16:20 isoranp-z1.zone_mem.jpg
> > -rw-rw-r-- 1 <> <>     0 Dec 31 16:20 isoranp-z1.zone_util.jpg
> >
> > # here is the gen_zone_charts function:
> >
> >> gen_zone_charts
> > function(zone,year,wk){
> >
> >        data_frame<-get_zonedata_url(year,wk,zone)
> >
> >        # this results in a 0 length file
> > # i have tried using jpeg(), and trellis.device() with the same results
> >        #jpeg(file=paste("charts/",zone,".zone_util.jpg",sep=""))
> >
> > trellis.device(jpeg,file=paste("charts/",zone,".zone_util.jpg",sep=""))
> >        #uses xyplot - works fine being called from the console
> >        plot_zone_util(data_frame)
> >        dev.off()
> >
> >        # this works ok
> >        jpeg(file=paste("charts/",zone,".zone_cpu.jpg",sep=""))
> >        # uses combination of boxplot and plot with a preceeding par()
> >        plot_zone_cpu(data_frame)
> >        dev.off()
> >
> >        # this works ok
> >        jpeg(file=paste("charts/",zone,".zone_mem.jpg",sep=""))
> >        # uses combination of plot and plot with a preceeding par()
> >        plot_zone_mem(data_frame)
> >        dev.off()
> >
> > }
> >
> >        [[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.
>

	[[alternative HTML version deleted]]

