我一直试图在R闪亮中使用eventReactive时下载plotout。 以下是示例代码
library(shiny)
server <- function(input, output) {
getplot = eventReactive(input$plot,{
plot(iris[,1])
})
output$plot = renderPlot({
getplot()
})
output$downloadplot <- downloadHandler(filename ="test.png",
content = function(file) {
png(file)
getplot()
dev.off()
},
contentType = "image/png")
}
ui <- fluidPage(
actionButton("plot","test plot"),
plotOutput("plot"),
downloadButton("downloadplot", label = "Save png")
)
shinyApp(ui = ui, server = server)
任何人都可以帮助必须更改以便成功下载文件吗? (目前它说文件未找到) p.s:我已经尝试过使用这个R Shiny eventReactive actionBotton interaction中提到的couter仍然没有成功
答案 0 :(得分:0)
您的代码存在的问题是您使用eventReactive绘制函数,它仅用于响应actionButton&#34; test plot&#34;。因此,点击&#34;保存png&#34;按钮,因为你没有点击&#34;测试情节&#34;同时。 小心:
此版本应在浏览器和查看器窗格中都有效:
library(shiny)
server <- function(input, output) {
getplot = function() {plot(iris[,1])} # plotting function independent from any button
output$plot = renderPlot({
# no plot displayed if "test plot" button hasn't been clicked
if (input$plotButton[[1]] == 0) # this value turns to 1 when button is clicked
return()
getplot()
})
output$downloadplot <- downloadHandler(filename ="test.png",
content = function(file) {
png(file)
getplot() # simple, independent function
dev.off()
},
contentType = "image/png")
}
ui <- fluidPage(
actionButton("plotButton", "test plot"),
plotOutput("plot"),
downloadButton("downloadplot", label = "Save png")
)
shinyApp(ui = ui, server = server)
我希望这会有所帮助