1

Here is the code to reproduce my problem:

library(ggplot2)

data <- data.frame(randomnums <- rnorm(50,100,200))

plot1 <- ggplot(data, aes(randomnums)) + geom_histogram()
print(plot1)
ggsave(plot1, filename="figures/plot1.pdf", width=5, height=3)

plot2 <- ggplot(data, aes(randomnums)) + geom_histogram()
print(plot2)
ggsave(plot2, filename="figures/plot2.pdf", width=5, height=3)

plot3 <- ggplot(data, aes(randomnums)) + geom_histogram()
print(plot3)
ggsave(plot3, filename="figures/plot3.pdf", width=5, height=3)

If each line of the above code is run line by line and waiting for each command to complete, then everything works fine. However, if I select all and run, things break down:

  • the first pdf file (plot1.pdf) that is saved will have a file size of 0k, and cannot be opened until I close my rsession (via rstudio)
  • My plots are not printed onto the screen. In RStudio, I there are no plots in the "plots" tab.

If I restart rstudio, everything will work fine again if I run the code line by line. Am I missing a command to make sure the code can be run as an entire selection?

EDIT: As requested - my session info.

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.9.3.1

loaded via a namespace (and not attached):
 [1] colorspace_1.2-1   dichromat_2.0-0    digest_0.6.3       grid_2.15.2        gtable_0.1.2       labeling_0.1      
 [7] lavaan_0.5-12      MASS_7.3-23        munsell_0.4        plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5
[13] reshape2_1.2.2     scales_0.2.3       stats4_2.15.2      stringr_0.6.2      tools_2.15.2      

EDIT: It was discovered that this only breaks in RStudio. It works fine in RGui.

Jim
  • 11,229
  • 20
  • 79
  • 114
  • 1
    I can't reproduce this. Maybe add your `sessionInfo()`? – joran May 10 '13 at 19:41
  • Some ideas: (1) Try clicking "Clear All" in RStudio's plot panel. (2) Maybe upgrade R? Somehow this feels more like an RStudio issue than an R issue, but that's just a hunch. – joran May 10 '13 at 19:47
  • @joran I upgraded R to 3.0.0. Same issue occurs if I select all and run. I also tested sourcing the entire file (e.g. source("plots.R")). Sourcing the file, the 2nd plot (plot2.pdf) is the one with the issues. Very strange. – Jim May 10 '13 at 19:57
  • Then I'd try from the R GUI and from the command line. If it only happens in RStudio, you know where the problem is. (It works everywhere for me.) – joran May 10 '13 at 19:59
  • @joran Everything works fine from RGui. It must be something about the difference in RStudio's handling of plots. Thanks for the suggestions. – Jim May 10 '13 at 20:07
  • This looks like a bug in RStudio. Probably the plot stuck in cache. Or maybe its handle remains opened until RStudio is closed. – DrDom May 10 '13 at 20:19
  • I remember seeing something similar recently involving Rstudio and ggsave; try including a `dev.off()` after the `ggsave` call. It shouldn't be needed, but may be due to a bug. – Brian Diggs May 10 '13 at 21:13
  • Or better, see this answer: http://stackoverflow.com/a/16488462/892313 – Brian Diggs May 10 '13 at 21:18
  • @BrianDiggs that's not the only question on this issue. There was also [**this one**](http://stackoverflow.com/q/16400028/1478381) which seems very simillar. – Simon O'Hanlon May 10 '13 at 21:35
  • @SimonO101 Ah, yes. That's the one that I was thinking of but couldn't find off hand. – Brian Diggs May 10 '13 at 21:52

0 Answers0