【问题标题】:Saving PNG plot non-ggplot in Shiny在 Shiny 中保存 PNG 图非 ggplot
【发布时间】:2015-06-24 04:49:37
【问题描述】:

我正在尝试在 Shiny 中下载一个非 ggplot 文件。我可以在应用程序中看到绘图,但是当我单击 UI 中的 plotDownload 按钮时,它会下载一个 EMPTY png 文件。有人可以知道我做错了什么?

server.R

library(shiny)
shinyServer(
   function(input, output) {

      plotInput <- reactive({
        plot(rnorm(sample(100:1000,1)))
      })

      output$pngPlot <- renderPlot({ plotInput() })


      output$downloadPlot <- downloadHandler(
        filename = "myPlot.png",
        content = function(file){
          png(file, width=800, res=100)
          print(plotInput())
          dev.off()
        })    
  }
)

ui.R

require(shiny)
pageWithSidebar(
  headerPanel("Output to png"),
  sidebarPanel(
    downloadButton('downloadPlot')
  ),
  mainPanel({ mainPanel(plotOutput("pngPlot")) })
)

谢谢。

【问题讨论】:

    标签: r download png shiny


    【解决方案1】:

    您需要在 downloadHandler() 中重新绘制:

    library(shiny)
    shinyServer(
      function(input, output) {
    
        plotInput <- reactive({
          plot(rnorm(sample(100:1000,1)))
        })
    
        output$pngPlot <- renderPlot({ plotInput() })
    
    
        output$downloadPlot <- downloadHandler(
          filename = "myPlot.png",
          content = function(file){
            png(file, width=800, res=100)
            plot(rnorm(sample(100:1000,1)))
            dev.off()
    
            })    
      }
    )
    

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 1970-01-01
      • 2021-11-02
      • 2021-04-17
      • 2016-05-24
      • 2011-01-19
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多