尝试利用RShiny在网页中设置Download按钮,下载ggplot2绘制的图(存储在`graph`变量中),但存储到本地后打开pdf,出现“文件已经损坏”。代码如下
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
downloadLink(outputId = "downloadGraph",
label = "Download")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
output$downloadGraph <- downloadHandler(
filename = function() {
paste("graph-", Sys.Date(), ".pdf", sep="")
},
content = function(file) {
pdf(file)
graph # ggplot2绘图存储的变量
dev.off()
}
)
}
shinyApp(ui, server)
}