[R] dev.off and Cairo and ggplot2

David Winsemius dwinsemius at comcast.net
Thu Jul 14 00:46:13 CEST 2011


On Jul 13, 2011, at 5:55 PM, Stephen Eick wrote:

> I have a weird example which fails and I don't understand why.  If I
> put dev.off() inside my function, it produces a bad image file.  But
> if I run what I think should be equivalent code with dev.off() running
> outside my function, it works.  Why does the location of dev.off
> matter?

It's in the misc section of the R-FAQ:

(Title is something along the lines of "Why don't lattice plots show  
up?" but its more general than lattice and also applies to ggplot/ 
ggplot2)

-- 
David.

>
>
> library(Cairo)
> library(ggplot2)
>
> foo<-function(){
>   Cairo(file="a.png")
>   cat(dev.cur(), "\n")
>   qplot(1:10,1:10)
>   dev.off();
> }
>
> foo()
>
> # file a.png is bad
>
> foo1<-function(){
>   Cairo(file="a.png")
>   cat(dev.cur(), "\n")
>   qplot(1:10,1:10)
> }
>
> foo1(); dev.off()
>
> # file a.png is good
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list