3

I've got my download function to do everything right, when the save as screen comes up, the file name I specified appears. When I click on save the window closes, but no file gets saved...

The same plot works fine in the app, the only problem is I cant seem to save it to a PNG file.

I run the shine app on my laptop and use RStudio.

Here is some extracts of my code.

ui.R

downloadButton('downloadSMemPlot', 'Download Graph')


server.R

'#draw membersip plot
s.MemPlotInput <- reactive({

'#some code to get data

s.MemPlot <- ggplot() + 
 geom_density(aes(x=Age, fill = Years), data=s.ben, alpha = 0.5) + 
 ggtitle("Density of beneficiary ages") + 
 theme_igray() + 
 theme(plot.title = element_text(lineheight=.8, face="bold")) +
 xlab("Age in full years") + ylab("Density")+
 scale_fill_hue()
})

output$s.memplot <- renderPlot({
  print(s.MemPlotInput())
})

'#download membership plot  
output$downloadSMemPlot <- downloadHandler(
  filename = "MembershipPlot.png",
  content = function(file) {
    png(file, type='cairo')
    print(s.MemPlotInput())
    dev.off()
  },
  contentType = 'application/png'
)
geotheory
  • 22,624
  • 29
  • 119
  • 196
Jan
  • 31
  • 1
  • 4

1 Answers1

2

You want

contentType = 'image/png'

not

contentType = 'application/png'

Although I don't think that's the problem. Are you running it within RStudio's preview pane or in an external browser? I had the same problem with downloading when using the preview pane but it worked fine on my browser.

Chris Beeley
  • 591
  • 6
  • 22
  • I had this same problem and couldn't figure out the issue. PDF worked but no other image type. Opening in the browser instead of RStudio preview panel, as suggested by Chris, solved the problem – Adrian Apr 23 '16 at 08:29