[R] dev.off and Cairo and ggplot2

Stephen Eick steve.eick at gmail.com
Wed Jul 13 23:55:50 CEST 2011


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?


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



More information about the R-help mailing list