从 Shiny (R) 下载 png [英] Downloading png from Shiny (R)

查看:40
本文介绍了从 Shiny (R) 下载 png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Shiny(和 R)非常陌生,正在努力将我在 Shiny 中制作的图导出到 png 文件.

I am pretty new to Shiny (and R) and struggling with exporting the plot I make in Shiny to a png-file.

我查看了这两个线程,但无法弄清楚:

I looked at these two threads but could not figure it out:

保存在闪亮应用中制作的绘图Shiny downloadHandler 不保存 PNG 文件

我设法在 ui 中创建了下载按钮,服务器似乎也在做我想让它做的一切.当我点击预览窗口中的下载按钮时,会弹出一个窗口,要求我指定文件位置和名称,但没有保存文件.当我在浏览器窗口中做同样的事情时,会创建一个 png 文件,但它是空的.

I manage to create the download button in the ui and the server seems to be doing everything I want it to do, too. When I hit the download button in the preview window, a pop up window asks me to specify the file location and name but no file is saved. When I do the same in a browser window, a png file is created but it is empty.

非常感谢任何见解!

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")
                  )
      ))
)

server.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()
      })    

  })  

推荐答案

闪亮讨论谷歌群组.您可以做的只是将反应式 plotInput 语句更改为普通函数.不知道为什么 downloadHandler 不能很好地处理响应式对象.

A workaround for this strange scenario was discussed on the shiny-discuss google group. What you can do is simply change your reactive plotInput statement into a normal function. Not sure why downloadHandler doesn't play nice with reactive objects.

# change
plotInput <- reactive({...})

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

您也可以删除downloadHandler调用中的print语句:

You can also remove the print statement in the downloadHandler call:

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

这篇关于从 Shiny (R) 下载 png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆