0

I have multiple graphics to render but for this example I am just showing two. I am trying to save the current graphic to a file. How can I create a button on my shiny app to save the current graphic displayed on the mainPanel?

    Here is what I got so far:

    library(shiny)
    library(ggplot2)
    library(tidyverse)
    library(shinythemes)
    library(plotly)
    library(scales)
    library(shinyWidgets)
    library(shinydashboard)

   # Define input choices
    type <- c("lambda", "annualjan")
     #Data for lambda
    table <- structure(list(year = 1991:2010, lambda = c(0.68854, 0.75545, 
    1.63359, 1.22282, 1.70744, 1.09692, 0.51159, 1.3904, 1.09132, 
    0.59846, 0.43055, 0.80135, 0.69027, 0.65646, 0.95485, 1.04818, 
    0.67859, 1.00461, 1.16665, 1.28203)), row.names = c(NA, -20L), class = "data.frame")
    
     #Data for annualjan
    table2 <- structure(list(year = c(1991L, 1991L, 1991L, 1991L, 1992L, 1992L, 
    1992L, 1992L, 1993L, 1993L, 1993L, 1993L, 1994L, 1994L, 1994L, 
    1994L, 1995L, 1995L, 1995L, 1995L, 1996L, 1996L, 1996L, 1996L, 
    1997L, 1997L, 1997L, 1997L, 1998L, 1998L, 1998L, 1998L, 1999L, 
    1999L, 1999L, 1999L, 2000L, 2000L, 2000L, 2000L, 2001L, 2001L, 
    2001L, 2001L, 2002L, 2002L, 2002L, 2002L, 2003L, 2003L, 2003L, 
    2003L, 2004L, 2004L, 2004L, 2004L, 2005L, 2005L, 2005L, 2005L, 
    2006L, 2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 2007L, 2008L, 
    2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2010L, 2010L, 
    2010L, 2010L, 2011L, 2011L, 2011L, 2011L), abundance = c("year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm", "year1_old", 
    "year1_and_juv", "allfish_over60mm", "allfemales_over60mm"), 
        total = c(80372, 971430, 730930, 357880, 81426, 670560, 583530, 
        297440, 46075, 550860, 556240, 275690, 43815, 969140, 720140, 
        353120, 85057, 1175700, 956960, 478530, 99783, 2135900, 1829100, 
        917100, 207690, 2317200, 660650, 328460, 205870, 1060200, 
        956130, 470000, 85635, 1748100, 1098900, 550060, 160860, 
        1846300, 979710, 490980, 154870, 1052200, 508990, 256230, 
        84071, 458980, 324640, 164010, 35441, 417140, 249270, 122330, 
        35290, 285390, 146140, 78097, 22697, 200110, 127420, 64698, 
        16838, 213050, 96616, 48067, 20102, 221910, 24482, 12245, 
        19675, 130790, 102490, 51171, 10330, 162810, 135170, 67562, 
        13983, 194190, 124770, 64635, 17233, 264040, 153190, 77608
        )), class = "data.frame", row.names = c(NA, -84L))
 
       ui <- fluidPage(
       navbarPage("Fish",
            windowTitle = "Fish Graphs",
            sidebarPanel(
            h3("Select Output to Visualize"),
            
            #Dropdown to select the desired kind of graphic
            selectInput(inputId = "graphtype",
                          label = "Graphic",
                          choices = type,
                          selected = "lambda"),
            
            #Slider to select custom years
            sliderInput(inputId = "Yearslider",
                      label="Years to plot",
                      sep="",
                      min=1991,
                      max=2011,
                      value=c(1991,2011))),
               #Graphic Area
                      mainPanel(plotOutput("plot"))),
   
      ))
                                            
   ##############################################
   server<- function (input, output, session) {
    session$onSessionEnded(function() {
    stopApp()
     }) 
  
    plot_data <- reactive({
    table[table$year >= input$Yearslider[1] & table$year <= input$Yearslider[2], ] 
    })
  
     output$plot <- renderPlot({
    theme_set(theme_classic())
    xlabels <- 1991:2011
    switch(input$graphtype,
          "lambda" = ggplot(plot_data(),aes(year,lambda)) + geom_line(size=1.5,colour="blue") 
                    + geom_point(colour="orange",size=4) + 
                     scale_x_continuous("",breaks = xlabels) + 
                     theme(axis.text.x = element_text(angle = 45, vjust = 0.5)) + 
                     labs(x="",y=expression("Lambda ("~lambda *")"),
                          title= paste0("Population growth rate - fraction per year- \nof Fish 
        ", table[table$year >= input$Yearslider[1] & table$year <= input$Yearslider[2], ])),
      
      "annualjan" = ggplot(plot_data2(),aes(year,total,color=abundance))  + 
      geom_line(size=1.3) + 
      geom_point(shape=21,fill="gray",color="black",size=3) + 
      scale_x_continuous(labels=xlabels,breaks=xlabels) + scale_y_continuous(labels = comma) + 
      facet_wrap(abundance~.,scales="free",ncol=2) +  theme(legend.position = "none") +
      theme(axis.text.x = element_text(angle = 60,vjust=0.5)) + 
      labs(y="Abundance", x="", title="Abundance of individual Fish")) 
      })
  
      }
   
      shinyApp(ui = ui, server = server)
Salvador
  • 1,229
  • 1
  • 11
  • 19
  • If the current graphic changes, do you want to overwrite the file saved previously ? – Julien Jun 26 '22 at 13:03
  • For `plotly`, have you considered the `ggplotly` function https://plotly.com/ggplot2/getting-started/ – Julien Jun 26 '22 at 13:05
  • Yes, everytime the graphic changes I want to save a file replacing the existing file. Basically, I want a data file of the graphic shown on the main panel. – Salvador Jun 26 '22 at 16:37
  • Does this answer solve your problem https://stackoverflow.com/a/22901025/8806649 ? – Julien Jun 28 '22 at 15:03

0 Answers0