title: How to stop R from creating empty Rplots.pdf file when using ggsave and Rscript
body: <p>I have an R script that saves some plots using ggsave. When I run the script from the command line, it not only saves my plots but also an empty Rplots.pdf file. How can I prevent R from creating this unnecessary file?</p>
  
  <p>Here is an example script that reproduces the error:</p>
  
  <pre><code>#!/usr/bin/env Rscript
  
  # Code that creates unnecessary Rplots.pdf file
  library(ggplot2)
  my.data &lt;- data.frame(x = 1:10, y = 1:10)
  my.plot &lt;- qplot(x, y, data = my.data)
  ggsave('example.png', my.plot) 
  </code></pre>
  
  <p>All the following ways of running the script create the unnecessary file:</p>
  
  <pre><code>Rscript script.R
  Rscript --vanilla script.R
  chmod a+x script.R
  ./script.R 
  </code></pre>
  
  <p>Also, when I source the code from within an interactive session, an unnecessary blank R Graphics Device window opens.</p>
  
  <p>Furthermore, I do not have these problems if I use the the following more verbose code in place of ggsave:</p>
  
  <pre><code>#!/usr/bin/env Rscript
  
  # Code that does NOT create unnecessary Rplots.pdf file
  library(ggplot2)
  my.data &lt;- data.frame(x = 1:10, y = 1:10)
  my.plot &lt;- qplot(x, y, data = my.data)
  png(file = 'example.png')
  print(my.plot)
  dev.off()
  </code></pre>
  
  <p>Here is my session info (which is the same whether running Rscript or interactively):</p>
  
  <pre><code>R version 3.0.1 (2013-05-16)
  Platform: x86_64-pc-linux-gnu (64-bit)
  
  locale:
   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
   [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
   [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
   [7] LC_PAPER=C                 LC_NAME=C                 
   [9] LC_ADDRESS=C               LC_TELEPHONE=C            
  [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
  
  attached base packages:
  [1] methods   stats     graphics  grDevices utils     datasets  base     
  
  other attached packages:
  [1] ggplot2_0.9.3.1
  
  loaded via a namespace (and not attached):
   [1] colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3       grid_3.0.1        
   [5] gtable_0.1.2       labeling_0.1       MASS_7.3-26        munsell_0.4       
   [9] plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 reshape2_1.2.2    
  [13] scales_0.2.3       stringr_0.6.2   
  </code></pre>
  
  <p><strong>Update 5 years later (2018-08-02)</strong>: This problem comes and goes. ggplot2 2.2.1 does not produce the empty file, ggplot2 3.0.0 does, and the ggplot2 team is currently working to fix this. For development history, see ggplot2 Issues <a href="https://github.com/tidyverse/ggplot2/issues/1326" rel="noreferrer">#1326</a>, <a href="https://github.com/tidyverse/ggplot2/issues/2363" rel="noreferrer">#2363</a>, <a href="https://github.com/tidyverse/ggplot2/pull/2758" rel="noreferrer">#2758</a>, and <a href="https://github.com/tidyverse/ggplot2/issues/2787" rel="noreferrer">#2787</a>.</p>