簡體   English   中英

在 R 中下載 Shiny 中的繪圖和表格; 從 pickerInput 中選擇的表/繪圖中進行選擇

[英]Download plots and tables in Shiny in R; Choosing from selection of tables/plots in pickerInput

我想在我的 Shiny 應用程序中添加下載 function ,用戶可以在其中下載從pickerInput指定的表格(作為 csv)或繪圖(作為 png)。 也許有一個比pickerInput更合適的替代方法,它會更容易,但這是我到目前為止的代碼(使用mpg數據作為可重現的最小示例): -

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

在此示例中,用戶具有散點 plot 和條形 plot。 有兩張桌子; 完整的mpg數據集和過濾版本。

我的問題是,我缺少哪些附加代碼,它將downloadHandler函數鏈接到它們各自的pickerInput函數,以便用戶可以指定哪個表或 plot 下載到他們的機器? 如果有更簡單的方法(有或沒有pickerInput )我會很高興聽到它:)

這是一種方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM