【问题标题】:Shiny: Export dynamic plot to image?Shiny:将动态图导出到图像?
【发布时间】:2015-06-17 08:47:31
【问题描述】:

我正在使用闪亮,我想创建一个下载按钮,将当前绘图存储为图像。这行得通:

  output$downloadPlot <- downloadHandler(
    filename <- function() {
      paste(input$group,'-top6_plot', Sys.Date(),'.png',sep='') },
    content <- function(file) {
      png(file, width = 980, height = 400, units = "px", pointsize = 12,
          bg = "white", res = NA)

      plot(sin, -pi, 2*pi)

      dev.off()},
    contentType = 'image/png'
  )

但我正在使用 dygraphs 来绘制动态图,这会创建一个空白图像:

  output$downloadPlot <- downloadHandler(
    filename <- function() {
      paste(input$group,'-top6_plot', Sys.Date(),'.png',sep='') },
    content <- function(file) {
      png(file, width = 980, height = 400, units = "px", pointsize = 12,
          bg = "white", res = NA)

      ReshapedVariables<-variablesForPlot()

      if(input$timeframe == 1){
        Title ="Timeframe: 1 Month"
      } else if(input$timeframe==2){
        Title ="Timeframe: 3 Months"
      } else if(input$timeframe==3){
        Title ="Timeframe: 6 Months"
      } else if(input$timeframe==4){
        Title ="Timeframe: Year to date"
      } else if(input$timeframe==5){
        Title ="Timeframe: 3 Years"
      } else if(input$timeframe==6){
        Title ="Timeframe: All"
      } else {
        Title ="Timeframe: Year to date"
      }

      dygraph(ReshapedVariables, main=Title) %>%
        #dyLegend(width = 200, labelsSeparateLines = TRUE, labelsDiv="VariablePlotLegend", show="always")  %>%
        dyLegend(labelsSeparateLines = FALSE, labelsDiv="VariablePlotLegend", show="always") %>%
        dyOptions(strokeWidth=2, axisLineColor=GRAPH_BLUE, axisLabelColor=GRAPH_BLUE, gridLineWidth=0.1)


      dev.off()},
    contentType = 'image/png'
  )

但 dygraphs 的绘图代码通常可以工作...因为在 web 闪亮应用程序中它正确显示了绘图。

【问题讨论】:

    标签: r shiny dygraphs


    【解决方案1】:

    这个问题已经很老了,但也许这个答案可能对您或其他人仍然有用。这个问题与这个问题直接相关:How to save Leaflet in R map as png or jpg file?。我遇到了和你一样的问题,这是我为我的情况解决的方法:

    您需要安装“htmlwidgets”和“webshot”包。那么,

    library(htmlwidgets)
    library(webshot)
    

    您还需要安装 PhantomJS。那么,

    使用通用函数调用将您的绘图保存为服务器端的单独对象。您需要在单独的反应式环境中创建变量 ReshapeVariables,因此它现在将被视为一个函数:

    dyplot <- function(){
    dygraph(ReshapedVariables(), main=Title) %>%
    labelsDiv="VariablePlotLegend", show="always")  %>%
    dyLegend(labelsSeparateLines = FALSE,  labelsDiv="VariablePlotLegend",show="always") %>%
    dyOptions(strokeWidth=2, axisLineColor=GRAPH_BLUE, axisLabelColor=GRAPH_BLUE, gridLineWidth=0.1)}
    

    然后下载:

    output$downloadData <- downloadHandler(
    filename = function () {paste(input$group,'-top6_plot', Sys.Date(),'.png',sep='') },
    content = function(file) {
      saveWidget(dyplot(), "temp.html", selfcontained = FALSE)
      webshot("temp.html", file = file)
    }
     )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 2013-07-15
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      相关资源
      最近更新 更多