downloadButton用于在闪亮的服务器中下载多个renderPlot反应式 [英] downloadButton to download multiple renderPlot reactive in shiny server

查看:64
本文介绍了downloadButton用于在闪亮的服务器中下载多个renderPlot反应式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个显示几个图形的闪亮应用程序.我想通过按钮下载,下载所有图形显示

I am creating a shiny application that displays several graphics. And I will like through a button download, download all graphs display

我执行以下操作:

server = function(input, output) {
    df<-data.frame(q=c(1,3,5,7,9),w=c(2,4,6,8,10),z=c(1,2,3,4,5))

# output all plot
 output$p1 <- renderPlot({ 
   ggplot(df,aes(x=q,y=w)) + geom_point()
   })
 output$p2 <- renderPlot({ 
   ggplot(df,aes(x=z,y=w))+geom_point()
 })
 output$p3 <- renderPlot({ 
   ggplot(df,aes(x=q,y=z))+geom_point()
 })

# Here is my function to list all the reactive graphs in png
get_plot <- function(my_i){
path <- paste("p", my_i,".png", sep="")
   png(path)
   dev.off()
}

#The output button 
output$allgraphs = downloadHandler(
  filename =function() {
    'all_images.zip'
  }, 
 content = function(fname) {
 fs <- c()
 for (i in 1:3) {
      path <- paste("p", i, ".png", sep="")
      fs <- c(fs, path)
      get_plot(i)
    }
 zip::zipr(zipfile=fname, files=fs)
  },
  contentType = "application/zip")
  }
))

推荐答案

这是一种方法.

library(shiny)
library(ggplot2)

ui <- fluidPage(
  plotOutput("p1"), 
  plotOutput("p2"),
  plotOutput("p3"),
  downloadButton("allgraphs", "Download")
)

server = function(input, output) {
  df<-data.frame(q=c(1,3,5,7,9),w=c(2,4,6,8,10),z=c(1,2,3,4,5))

  p1 <- reactive({
    ggplot(df,aes(x=q,y=w)) + geom_point()
  })
  p2 <- reactive({
    ggplot(df,aes(x=z,y=w))+geom_point()
  })
  p3 <- reactive({
    ggplot(df,aes(x=q,y=z))+geom_point()
  })

  output$p1 <- renderPlot({ 
    p1()
  })
  output$p2 <- renderPlot({ 
    p2()
  })
  output$p3 <- renderPlot({ 
    p3()
  })

  output$allgraphs = downloadHandler(
    filename = function() {
      'all_images.zip'
    }, 
    content = function(fname) {
      fs <- replicate(3, tempfile(fileext = ".png"))
      ggsave(fs[1], p1())
      ggsave(fs[2], p2())
      ggsave(fs[3], p3())
      zip::zipr(zipfile=fname, files=fs)
    },
    contentType = "application/zip")
}

shinyApp(ui, server)

这篇关于downloadButton用于在闪亮的服务器中下载多个renderPlot反应式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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