title: How to avoid repeating the same procedure multiple times in Shiny app
body: <p>I have written a Shiny app where I am sampling from a poisson distribution. I have rewritten the function for each time I want to output a plot or summary of the data. I would rather only use the function once and then have all plots and summaries refer to the single time the function was called, otherwise the results will not be identical across the outputs. When I tried to call the function once and store the results as a variable to be used by ggplot I got the error that ggplot cannot use reactive data and I was not able to coerce it into a data frame. </p>
  
  <p>I tried variations of:</p>
  
  <pre><code>mydata &lt;- reactive({
    x &lt;- input$x
    y &lt;- input$y
    z &lt;- input$z
    Muts &lt;- as.data.frame(rpois(100,(x*y*z)))
    Muts
  })
  </code></pre>
  
  <p>However they did not work, as ggplot could not use the input. I think I am not using the reactive function correctly. Any help to reduce the redundancy in the code and ensure that the plots and summary are all using the same single underlying data are much appreciated. Thank you in advance.</p>
  
  <p>My current code:</p>
  
  <p>server.R</p>
  
  <pre><code>library(shiny)
  library(ggplot2)
  
  # Define server logic required to draw a histogram
  function(input, output) {
  
  
  
  
    output$distPlot &lt;- renderPlot({
  
      x &lt;- input$x
      y &lt;- input$y
      z &lt;- input$z
      Muts &lt;- as.data.frame(rpois(100,(x*y*z)))
  
  
      # draw the density plot
  
      ggplot(Muts, aes(Muts)) + geom_density() 
  
    })
  
    output$distPlot2 &lt;-renderPlot({
  
      x &lt;- input$x
      y &lt;- input$y
      z &lt;- input$z
      Muts &lt;- as.data.frame(rpois(100,(x*y*z)))
  
  
  
      Muts &lt;- as.data.frame(Muts)
  
      ggplot(Muts, aes(Muts)) + geom_histogram() 
  
    })
  
    output$summary &lt;- renderPrint({
      x &lt;- input$x
      y &lt;- input$y
      z &lt;- input$z
      Muts &lt;- as.data.frame(rpois(100,(x*y*z)))
  
      summary(Muts)
    })
  
  
  }
  </code></pre>