title: Saving plot as pdf and simultaneously display it in the window (x11)
body: <p>I have written a function that creates a barplot. I would like to save this plot as a pdf as well as display it on my screen (x11) when applying this function. The code looks like this.</p>
  
  <pre><code>create.barplots &lt;- function(vec)
   {
     x11()                                  # opens the window
     ### Here is a code that creates a barplot and works perfectly
     ### but irrelevant for my question
     dev.copy(pdf("barplots.table.2.pdf")) # is supposed to copy the plot in pdf
                                           # under the name "barplots.table.2.pdf"
     dev.off()                             # is supposed to close the pdf device
   }
  </code></pre>
  
  <p>This creates the following error: 'device' should be a function</p>
  
  <p>When I modify the code to:</p>
  
  <pre><code>create.barplots &lt;- function(vec)
   {
     x11()
     ### Here is a code that creates a barplot and works perfectly
     ### but irrelevant for my question
     dev.copy(pdf) # This is the only difference to the code above
     dev.off()
   }
  </code></pre>
  
  <p>R displays the plot and creates a file called Rplots.pdf. This is a problem because of several reasons.</p>
  
  <p>I also tried to open the devices the other way around. First open the pdf device, than copy the content of the pdf device into the x11 device, than set the pdf device as active and than close the pdf device. The code here looks like this:</p>
  
  <pre><code>create.barplots &lt;- function(vec)
   {
     pdf("barplots.table.2.pdf") # open the pdf device
     ### Here is a code that creates a barplot and works perfectly
     ### but irrelevant for my question
     dev.copy(x11)              # copy the content of the pdf device into the x11 device
     dev.set(which = 2)         # set the pdf device as actice
     dev.off()                  # close the pdf device
   }
  </code></pre>
  
  <p>The problem here is that the wondow that is supposed to display the plot is empty!</p>
  
  <p>To sum up, I have two questions:
  1) How to save a plot as pdf and display it in x11 simultaneously? And
  2) How to save the plot not in the working directory somewhere else?</p>
  
  <p><em>EDIT</em></p>
  
  <p>The solutions above work great. But I still do not understand why</p>
  
  <pre><code>pdf("barplots.table.2")
  barplot(something)
  dev.copy(x11)
  </code></pre>
  
  <p>displays an empty grey window instead of copying the content of the pdf device in the window device! I also tried</p>
  
  <pre><code>pdf("barplots.table.2")
  barplot(something)
  dev.copy(window)
  </code></pre>
  
  <p>In which I failed as well...</p>