Shiny downloadHandler 不保存 PNG 文件 [英] Shiny downloadHandler doesn't save PNG files
问题描述
我有我的下载功能来做所有的事情,当另存为屏幕出现时,我指定的文件名就会出现.当我点击保存窗口关闭,但没有文件被保存...
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...
相同的绘图在应用程序中运行良好,唯一的问题是我似乎无法将其保存为 PNG 文件.
The same plot works fine in the app, the only problem is I cant seem to save it to a PNG file.
我在笔记本电脑上运行了 Shine 应用并使用了 RStudio.
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'
)
推荐答案
你想要
contentType = 'image/png'
不是
contentType = 'application/png'
虽然我不认为那是问题所在.您是在 RStudio 的预览窗格中还是在外部浏览器中运行它?使用预览窗格时,我在下载时遇到了同样的问题,但它在我的浏览器上运行良好.
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.
这篇关于Shiny downloadHandler 不保存 PNG 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!