在R的Shiny中下载绘图和表格;在pickerInput中选择表/图表进行选择 [英] Download plots and tables in Shiny in R; Choosing from selection of tables/plots in pickerInput

查看:139
本文介绍了在R的Shiny中下载绘图和表格;在pickerInput中选择表/图表进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Shiny应用程序中添加一个下载功能,用户可以根据 pickerInput 的指定下载表格(以csv格式)或图表(以png格式).也许有一个比 pickerInput 更合适的替代方法,它将使之更加容易,但这是我到目前为止的代码(使用 mpg 数据作为可复制的最小示例制作而成)):-

I'd like to add in a download function within my Shiny app where users can download tables (as csv) or plots (as png) as specified from a pickerInput. Perhaps there is a more appropriate alternative than pickerInput which would make it easier, but here is the code that I have so far (made using the mpg data as a reproducible minimal example):-

#UI
ui<-fluidPage(
  tabPanel("Minimal Example",
           sidebarLayout(
             sidebarPanel(width = 4, 
                          
                          pickerInput("manufacturer", "Select manufacturer",
                                      choices = unique(mpg$manufacturer),options = list('actions-box'=TRUE, 'live-search'=TRUE), multiple = T),
                          pickerInput("model", "Select model",
                                      choices = unique(mpg$model),options = list('actions-box'=TRUE, 'live-search'=TRUE), multiple = T),
                          
                           pickerInput("eda_plotpick", "Select plot to save",
                                      choices = c("Scatter plot",
                                                 "Bar plot")),
                          pickerInput("eda_tablepick", "Select table to save",
                                      choices = c("mpg",
                                                  "mpg_filtered")),
                    
                          
                          actionButton("run_eda", "Run analysis"),
             downloadButton("downloadplot", "Download plot"),
           downloadButton("downloadtable", "Download table")),
             mainPanel(
       
               column(width = 8, box("Scatter plot", plotOutput("scatter"), width = "100%")),
               column(width = 8, box("Bar plot", plotOutput("bar"), width = "100%")),
               column(width = 8, box("mpg data", tableOutput("mpg"), width = "100%")),
               column(width = 8, box("mpg data (filtered)", tableOutput("mpg_filter"), width = "100%"))
              
             )          
             
           )
           
           
  )#end of tabpanel
  
)#end of fluidpage


#SERVER
server<-function(input,output,session){
  
  
  
  observeEvent(input$run_eda,{
   
    
    output$scatter<-renderPlot({
      
    scatterplot<-ggplot(mtcars, aes(x=wt, y=mpg)) + 
      geom_point(aes(size=qsec))
      return(scatterplot)
      
    })
    
    
    output$bar<-renderPlot({
      
      barplot<-ggplot(mpg,aes(y = class))+geom_bar()
      
      return(barplot)
      
    })
    
    
    output$mpg<-renderTable({
      
      
      return(mpg)
      
    })
    
    
    output$mpg_filter<-renderTable({
      
      
      
      mpg_filtered <- mpg %>%
        filter(manufacturer %in% input$manufacturer)%>%
        filter(model %in% input$model)
        
      
      
      
      return(mpg_filtered)
      
    })
    
    
    
    
    
  })#end of observe event
  
  
   output$downloadtable <- downloadHandler(
     filename = function() {
       paste('data-', input$eda_tablepick, '.csv', sep='')
     },
     content = function(con) {
       write.csv(data, con)
     }
   )
  
   
   output$downloadplot <- downloadHandler(
     filename = function() {
       paste('plot-', input$eda_plotpick,'.png', sep='')
     },
     content = function(con) {
       write.csv(data, con)
     }
   )
  
  
  
  
}#end of server


shinyApp(ui,server)

在此示例中,用户具有散点图和条形图.有两个表;完整的 mpg 数据集和已过滤的版本.

In this example, the user has a scatter plot and a bar plot. There are two tables; the full mpg data set and a filtered version.

我的问题是,我缺少什么额外的代码,这些代码会将 downloadHandler 函数链接到它们各自的 pickerInput 函数,以便用户可以指定哪个表或密谋下载到他们的机器?如果有一种更简单的方法(带或不带 pickerInput ),我会很高兴听到的:)

My question is, what is the additional code that I am missing which would link the downloadHandler functions to their respective pickerInput functions, so that the user can specify which table or plot to download to their machine? If there is a much simpler way of doing this (with or without pickerInput) I would be happy to hear it :)

推荐答案

这是一种实现方法:

#UI
ui<-fluidPage(
  tabPanel("Minimal Example",
           sidebarLayout(
             sidebarPanel(width = 4, 
                          
                          pickerInput("manufacturer", "Select manufacturer",
                                      choices = unique(mpg$manufacturer),options = list('actions-box'=TRUE, 'live-search'=TRUE), multiple = T),
                          pickerInput("model", "Select model",
                                      choices = unique(mpg$model),options = list('actions-box'=TRUE, 'live-search'=TRUE), multiple = T),
                          
                          pickerInput("eda_plotpick", "Select plot to save",
                                      choices = c("Scatterplot",
                                                  "Barplot")),
                          pickerInput("eda_tablepick", "Select table to save",
                                      choices = c("mpg",
                                                  "mpg_filtered")),
                          
                          
                          actionButton("run_eda", "Run analysis"),
                          downloadButton("downloadplot", "Download plot"),
                          downloadButton("downloadtable", "Download table")),
             mainPanel(
               
               column(width = 8, box("Selected plot", plotOutput("myplot"), width = "100%")),
               column(width = 8, box("Selected table", tableOutput("mytable"), width = "100%"))
               
             )          
             
           )
           
           
  )#end of tabpanel
  
)#end of fluidpage


#SERVER
server<-function(input,output,session){
  
  observeEvent(input$run_eda,{
  
  plot<- reactive({
    req(input$manufacturer,input$model)
    if (input$eda_plotpick=="Scatterplot"){
      plot<-ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(aes(size=qsec))
    }else plot<-ggplot(mpg,aes(y = class))+geom_bar()
    plot
  })
  output$myplot <- renderPlot({
    plot()
  })
  
  data <- reactive({
    req(input$manufacturer,input$model)
    if (input$eda_tablepick=="mpg_filtered"){
      data <- mpg %>%
        filter(manufacturer %in% input$manufacturer) %>%
        filter(model %in% input$model)
    }else data <- mpg
    data
  })
  output$mytable <- renderTable({
    data()
  })
  
  
  output$downloadtable <- downloadHandler(
    filename = function() {
      paste('data-', input$eda_tablepick, '.csv', sep='')
    },
    content = function(file) {
      write.csv(data(), file)
    }
  )
  
  output$downloadplot <- downloadHandler(
    filename = function() {
      paste('plot-', input$eda_plotpick,'.png', sep='')
    },
    content = function(con) {
      png(con, units = "px")
      print(plot())
      dev.off() 
    }, contentType = 'image/png'
  )
  
  })#end of observe event
  
}#end of server


shinyApp(ui,server)

这篇关于在R的Shiny中下载绘图和表格;在pickerInput中选择表/图表进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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