从Shiny(R)下载PNG

20

我对Shiny(和R)还很陌生,正在努力将我在Shiny中制作的图表导出为png文件。

我查看了这两个主题,但无法找出解决方法:

保存在Shiny应用程序中制作的图表 Shiny downloadHandler无法保存PNG文件

我已经成功创建了ui中的下载按钮,并且服务器似乎也在按照我的意愿完成所有操作。当我在预览窗口中点击下载按钮时,弹出一个窗口要求我指定文件位置和名称,但没有文件被保存。当我在浏览器窗口中执行相同的操作时,会创建一个空的png文件。

非常感谢任何帮助和见解!

ui.R

library(shiny)

shinyUI(fluidPage(
  titlePanel("This is a scatterplot"),

  sidebarLayout(
    sidebarPanel(

      fileInput('datafile', 'Choose CSV file',
                accept=c('text/csv', 'text/comma-separated-values,text/plain')),

      uiOutput("varselect1"),

      uiOutput("varselect2"),

      downloadButton('downloadPlot', 'Download Plot')

      ),

    mainPanel(          
          h4("Here is your scatterplot"),
          plotOutput("plot1")
                  )
      ))
)

服务器.R

library(foreign)

shinyServer(function(session,input, output) {

    DataInput <- reactive({
      infile <- input$datafile
      if (is.null(infile)) {

        return(NULL)
      }
      read.csv(infile$datapath)
    })


    output$varselect1 <- renderUI({

      if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL)

      cols <- names(DataInput())
      selectInput("var1", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---"))

    })

    output$varselect2 <- renderUI({

      if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL)

      cols <- names(DataInput())
      selectInput("var2", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---"))

    })



    plotInput <- reactive({

      a <- which(names(DataInput())==input$var1)
      x_lab <- as.numeric(DataInput()[,a])


      b <- which(names(DataInput())==input$var2)
      y_lab <- as.numeric(DataInput()[,b])      

      main.text <- paste("Scatterplot of the variables",colnames(DataInput())[a],"and", colnames(DataInput())[b],sep = " ", collapse = NULL)

      plot(x_lab, y_lab, main=main.text, xlab=colnames(DataInput())[a], ylab=colnames(DataInput())[b], xlim=c(min(x_lab),max(x_lab)*1.05), ylim=c(min(y_lab), max(y_lab)*1.05))

      observations <- DataInput()[,1]

      text(x_lab, y_lab, labels=observations, pos=3)


    })

    output$plot1 <- renderPlot({
          print(plotInput())
    })


    output$downloadPlot <- downloadHandler(
      filename = "Shinyplot.png",
      content = function(file) {
        png(file)
        print(plotInput())
        dev.off()
      })    

  })  
1个回答

26
shiny-discuss google group中讨论了一种奇怪情况的解决方法。你可以简单地将你的响应式plotInput语句更改为普通函数。不确定为什么downloadHandler与响应式对象不兼容。
# change
plotInput <- reactive({...})

# into this
plotInput <- function(){...}

您还可以在调用downloadHandler时删除打印语句:

output$downloadPlot <- downloadHandler(
      filename = "Shinyplot.png",
      content = function(file) {
        png(file)
        plotInput()
        dev.off()
      })    

这太棒了。花了几个小时尝试下载一组反应式图,但无法弄清楚出了什么问题。我的大脑已经太累了,无法理解从“反应式”更改为“函数”是否存在任何负面影响,但它似乎运行良好。 - Adrian
这个有效。令人惊讶的是,即使不在响应式函数中,图形也会更新。 - Gimelist

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接