title: Downloading png from Shiny (R)
body: <p>I am pretty new to Shiny (and R) and struggling with exporting the plot I make in Shiny to a png-file. </p>
  
  <p>I looked at these two threads but could not figure it out:</p>
  
  <p><a href="https://stackoverflow.com/questions/14810409/save-plots-made-in-a-shiny-app">Save plots made in a shiny app</a>
  <a href="https://stackoverflow.com/questions/24557541/shiny-downloadhandler-dont-save-png-file">Shiny downloadHandler doesn&#39;t save PNG files</a></p>
  
  <p>I manage to create the download button in the ui and the server seems to be doing everything I want it to do, too. When I hit the download button in the preview window, a pop up window asks me to specify the file location and name but no file is saved. When I do the same in a browser window, a png file is created but it is empty. </p>
  
  <p>Any insight is much appreciated!</p>
  
  <p>ui.R</p>
  
  <pre><code>library(shiny)
  
  shinyUI(fluidPage(
    titlePanel("This is a scatterplot"),
  
    sidebarLayout(
      sidebarPanel(
  
        fileInput('datafile', 'Choose CSV file',
                  accept=c('text/csv', 'text/comma-separated-values,text/plain')),
  
        uiOutput("varselect1"),
  
        uiOutput("varselect2"),
  
        downloadButton('downloadPlot', 'Download Plot')
  
        ),
  
      mainPanel(          
            h4("Here is your scatterplot"),
            plotOutput("plot1")
                    )
        ))
  )
  </code></pre>
  
  <p>server.R</p>
  
  <pre><code>library(foreign)
  
  shinyServer(function(session,input, output) {
  
      DataInput &lt;- reactive({
        infile &lt;- input$datafile
        if (is.null(infile)) {
  
          return(NULL)
        }
        read.csv(infile$datapath)
      })
  
  
      output$varselect1 &lt;- renderUI({
  
        if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL)
  
        cols &lt;- names(DataInput())
        selectInput("var1", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---"))
  
      })
  
      output$varselect2 &lt;- renderUI({
  
        if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL)
  
        cols &lt;- names(DataInput())
        selectInput("var2", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---"))
  
      })
  
  
  
      plotInput &lt;- reactive({
  
        a &lt;- which(names(DataInput())==input$var1)
        x_lab &lt;- as.numeric(DataInput()[,a])
  
  
        b &lt;- which(names(DataInput())==input$var2)
        y_lab &lt;- as.numeric(DataInput()[,b])      
  
        main.text &lt;- paste("Scatterplot of the variables",colnames(DataInput())[a],"and", colnames(DataInput())[b],sep = " ", collapse = NULL)
  
        plot(x_lab, y_lab, main=main.text, xlab=colnames(DataInput())[a], ylab=colnames(DataInput())[b], xlim=c(min(x_lab),max(x_lab)*1.05), ylim=c(min(y_lab), max(y_lab)*1.05))
  
        observations &lt;- DataInput()[,1]
  
        text(x_lab, y_lab, labels=observations, pos=3)
  
  
      })
  
      output$plot1 &lt;- renderPlot({
            print(plotInput())
      })
  
  
      output$downloadPlot &lt;- downloadHandler(
        filename = "Shinyplot.png",
        content = function(file) {
          png(file)
          print(plotInput())
          dev.off()
        })    
  
    })  
  </code></pre>