[R] wait for graph to finish plotting

Joshua Wiley jwiley.psych at gmail.com
Thu Oct 28 19:55:48 CEST 2010


Hi aleblanc,

Does something like this work?  See inline comments for details.


## If you want them in separate files, you might use onefile = FALSE
## I also often find I need to adjust width and height
## which could be done with height = 9, width = 9, for example
pdf(file = "~/Dropbox/phd/results/graphs/TEST%03d.pdf", onefile = FALSE)
for (dataset in c("breastCancer","pima","heartDisease",
                  "crx","ionosphere","votes"))
{
 p <- eval(parse(text=paste(dataset,"Probs",sep="")))
 ## I believe using get() is a simpler way (uncomment below)
 ## p <- get(paste(dataset,"Probs",sep=""))
 m <- melt(t(p))
 title <- titles[[dataset]]
 filename <- paste("~/Dropbox/phd/results/graphs/",dataset,
                   "Probs_barcharts.eps",sep="")
 ## this is a lattice function which needs to be wrapped in print()
 ## inside a for loop if I'm not mistaken
 ## also code is more readable when broken up
 print(barchart(
       value~X1|X2, data=m, origin=0, layout=c(1,10),
       scales=list(alternating=1, tck=c(0.6,0)),
       ylab="Difference from test probabilities",
       xlab="Interval",
       main=title,
       col=c(rep("black",8),"red","white","blue"),
       ylim=c(-1,1)))
}
dev.off()


Cheers,

Josh


On Thu, Oct 28, 2010 at 10:27 AM,  <news at aleblanc.cotse.net> wrote:
>
> pdf(file = "~/Dropbox/phd/results/graphs/TEST%03d.pdf")
> for (dataset in c("breastCancer","pima","heartDisease","crx","ionosphere","votes"))
> {
>  p <- eval(parse(text=paste(dataset,"Probs",sep="")))
>  m <- melt(t(p))
>  title <- titles[[dataset]]
>  filename=paste("~/Dropbox/phd/results/graphs/",dataset,"Probs_barcharts.eps",sep="")
>  barchart(value~X1|X2,data=m,origin=0,layout=c(1,10),scales=list(alternating=1,tck=c(0.6,0)),ylab="Difference from test probabilities",xlab="Interval",col=c(rep("black",8),"red","white","blue"),main=title,ylim=c(-1,1))
> }
> dev.off()
>
>
> Tal Galili <tal.galili at gmail.com> writes:
>
>> Could you please paste the exact code you are using? (the one with the pdf and
>> dev.off, outside the loop )
>>
>> ----------------Contact
>> Details:-------------------------------------------------------
>> Contact me: Tal.Galili at gmail.com |  972-52-7275845
>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
>> www.r-statistics.com (English)
>> ----------------------------------------------------------------------------------------------
>>
>>
>>
>>
>> On Sat, Oct 23, 2010 at 4:53 PM, <news at aleblanc.cotse.net> wrote:
>>
>>     David Winsemius <dwinsemius at comcast.net> writes:
>>
>>     > On Oct 22, 2010, at 12:50 PM, <news at aleblanc.cotse.net> wrote:
>>     >
>>     >> Tal Galili <tal.galili at gmail.com> writes:
>>     >>
>>     >>> I suspect that using "dev.copy2eps" Is not going to help you here.
>>     >>>
>>     >>> Please try again using:
>>     >>>
>>     >>>  pdf(...)  # Check: ?pdf
>>     >>>  for(i in something)
>>     >>>  {
>>     >>>     plot(things)
>>     >>>  }
>>     >>>  dev.off()
>>     >>>
>>     >>> But give pdf() the file path.
>>     >>> Make sure you can make it work with simple plots.  then check it on
>>     >>> your
>>     >>> situation, and let us know if it works :)
>>     >>> (I had it work with very heavy plots - so I trust it will work for
>>     >>> you)
>>     >>>
>>     >>> (p.s: please keep cc'ing the r-help list so others will know the
>>     >>> stage of your
>>     >>> problem, in case others would like to add more help :) )
>>     >>>
>>     >>> Cheers,
>>     >>> Tal
>>     >>>
>>     >>
>>     >> The problem is that the filename changes with each iteration of the
>>     >> loop, so I need to
>>     >> save the graph inside the loop.
>>     >> I tried this:
>>     >>
>>     >> for(...)
>>     >> {
>>     >>    ...
>>     >>    pdf(...)
>>     >>    barplot(...)
>>     >> }
>>     >>
>>     >> but I just got a load of empty graphs.
>>     >
>>     > Because you failed to follow Ted directions to use dev.off()
>>     >
>>     > pdf() just opens the file for writing, dev.off() executes the writing
>>     > and closes properly.
>>     >
>>     > --
>>     > David.
>>     I just tried with dev.off() but still the same problem.
>>     I tried with pdf() and dev.off() inside the loop, and with them outside
>>     (pdf before the loop,
>>     and dev.off after). Same results each time: an empty pdf file.
>>     I am using R version 2.11.1 if this makes any difference.
>>     --
>>     aleblanc
> Hi Tal,
>
>
> --
> aleblanc
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list