Shiny downloadHandler doesn't save PNG files

2019-02-27 14:50发布

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'
)

标签: r save shiny
1条回答
时光不老,我们不散
2楼-- · 2019-02-27 15:53

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.

查看更多
登录 后发表回答