下载使用grid.draw()创建的图作为png in downloadHandler Shiny

时间:2015-08-27 23:10:41

标签: r ggplot2 shiny

我在Shiny的downloadHandler()函数中下载使用grid.draw()创建的绘图时遇到问题。这是我想要实现的可重现的例子:

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)

当我按下下载为png时,会显示在控制台中:

Error opening file: 2
Error reading: 9

在控制台中,此代码运行正常,图表按预期保存:

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

有没有办法解决这个问题?非常感谢!

1 个答案:

答案 0 :(得分:2)

内容函数必须有一个参数,它可以是任何东西(例如“file”),然后它转到png()函数。

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