下载在downloadHandler

下载在downloadHandler

本文介绍了下载在downloadHandler Shiny中使用grid.draw()创建为PNG的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Shiny的downloadHandler()函数中下载使用grid.draw()创建的绘图.这是我想要实现的可复制示例:

I'm having problems downloading a plot created with grid.draw() in the downloadHandler() function in Shiny. Here is a reproducible example of what I want to achieve:

library(gtable)
library(ggplot2)
ui <- shinyUI(fluidPage(
    titlePanel("Test app"),
    fluidRow(
        column(4,
               wellPanel(
                   downloadButton('download',label="Download plot as png")
               )
        ),
        column(8,
               plotOutput("plot")
        )
    )
))
server <- function(input,output){

    plotting<- reactive({

        data1=data.frame(x=rnorm(50),y=rnorm(50))
        data2=data.frame(x=rexp(50),y=rexp(50))

        plot1=ggplot(data1,aes(x,y))+geom_point()
        plot2=ggplot(data2,aes(x,y))+geom_point()

        gb1=ggplot_build(plot1)
        gb2=ggplot_build(plot2)

        gA <- ggplot_gtable(gb1)
        gB <- ggplot_gtable(gb2)

        both <- gtable:::rbind_gtable(gA, gB, "last")
        return(both)
    })

    output$plot <- renderPlot({
        grid.newpage()
        grid.draw(plotting())
    })

    output$download <- downloadHandler(
        filename <- "shinytestplot.png",

        content <- function(file=NULL){
            png(filename)
            grid.newpage()
            grid.draw(plotting())
            dev.off()
        }
    )
}
shinyApp(server=server,ui=ui)

当我按Download as png时,它将显示在控制台中:

When I press Download as png, this shows up in the console:

Error opening file: 2
Error reading: 9

在控制台中,此代码运行良好,并且绘图按预期方式保存:

In the console, this code runs fine and the plot saves as expected:

  data1=data.frame(x=rnorm(50),y=rnorm(50))
  data2=data.frame(x=rexp(50),y=rexp(50))
  plot1=ggplot(data1,aes(x,y))+geom_point()
  plot2=ggplot(data2,aes(x,y))+geom_point()

  gb1=ggplot_build(plot1)
  gb2=ggplot_build(plot2)

  gA <- ggplot_gtable(gb1)
  gB <- ggplot_gtable(gb2)

  both <- gtable:::rbind_gtable(gA, gB, "last")
  png("consoletestplot.png")
  grid.newpage()
  grid.draw(both)
  dev.off()

是否可以解决此问题?非常感谢!

Is there a way to fix this? Many thanks!

推荐答案

content函数必须有一个参数,它可以是任何东西(例如"file"),然后转到png()函数.

The content function has to have an argument, it can be anything (for instance "file") and it then goes to png() function.

#Changes:
content <- function(file){ ## file = NULL --> file
  png(file) # filename --> file

完整代码:

    library(gtable)
library(ggplot2)
ui <- shinyUI(fluidPage(
  titlePanel("Test app"),
  fluidRow(
    column(4,
           wellPanel(
             downloadButton('download',label="Download plot as png")
           )
    ),
    column(8,
           plotOutput("plot")
    )
  )
))
server <- function(input,output) {

  plotting<- reactive({

    data1=data.frame(x=rnorm(50),y=rnorm(50))
    data2=data.frame(x=rexp(50),y=rexp(50))

    plot1=ggplot(data1,aes(x,y))+geom_point()
    plot2=ggplot(data2,aes(x,y))+geom_point()

    gb1=ggplot_build(plot1)
    gb2=ggplot_build(plot2)

    gA <- ggplot_gtable(gb1)
    gB <- ggplot_gtable(gb2)

    both <- gtable:::rbind_gtable(gA, gB, "last")
    return(both)
  })

  output$plot <- renderPlot({
    grid.newpage()
    grid.draw(plotting())
  })

  output$download <- downloadHandler(
    filename <- "shinytestplot.png",

  # Changes:
  content <- function(file){ ## file = NULL --> file
    png(file) # filename --> file
    grid.newpage()
    grid.draw(plotting())
    dev.off()
  }
  )
}
shinyApp(server=server,ui=ui)

这篇关于下载在downloadHandler Shiny中使用grid.draw()创建为PNG的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:10