r - 从 Shiny (R) 下载 png

标签 r download png shiny

我对 Shiny(和 R)还很陌生,并且正在努力将我在 Shiny 中制作的情节导出到 png 文件中。

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

Save plots made in a shiny app
Shiny downloadHandler doesn't save PNG files

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

非常感谢任何见解!

用户界面

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

  })  

最佳答案

shiny-discuss google group 上讨论了这种奇怪情况的解决方法。 .你能做的就是改变你的响应式(Reactive)plotInput声明成一个正常的函数。不知道为什么downloadHandler对 react 性对象不好。

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

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

您还可以删除 downloadHandler 中的打印语句称呼:
output$downloadPlot <- downloadHandler(
      filename = "Shinyplot.png",
      content = function(file) {
        png(file)
        plotInput()
        dev.off()
      })    

关于r - 从 Shiny (R) 下载 png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26764481/

相关文章:

php - 下载的 WAV 文件无法播放

png - 如何用gnuplot在标题中显示 '@'并且输出格式为png?

ios - 如何从链接正确显示图像? ios

r - 使用 mutate_at 在每列之后插入相对值(相对于 tibble 的第二列)

json - R:将特定行转换为列

r - 将字符附加到数据框中列中的特定位置

facebook - 跟踪谁从我的自定义 Facebook 选项卡下载了 PDF

r - ggplot2 中 qplot() 和 ggplot() 之间的选择

android - 从服务器下载文件到手机

Delphi - 使用透明度和索引调整 PNG 大小