title: Use reactive expressions in renderPlot and download handler when using own functions
body: <p><a href="https://stackoverflow.com/questions/24157990/use-reactive-expressions-in-renderplot-and-download-handler">Use reactive expressions in renderPlot and download handler</a></p>
  
  <p>I have had problems by using reactive expressions both in <code>renderPlot()</code> and in <code>downloadHandler()</code> in shiny applications. I want to do this to reduce maintenance and redundancy in my code.</p>
  
  <p>The above issue works with "normal" plot functions like plot, hist, etc. 
  In my application I'm using a more complex function which creates a plot. I have created a simplified version of it </p>
  
  <pre><code>helpfunc &lt;- function(mean, sd) {
      hist(rnorm(1000, mean, sd))
      lines(1:10)
  }
  </code></pre>
  
  <p>If you use this function now in your <code>shiny app</code>, it does not work when creating a reactive expression out of it. Neither by using <code>plot()</code> nor by using the reactive expression itself.</p>
  
  <pre><code>mwe &lt;- function() {
  app = list(
      ui = bootstrapPage(
           fluidPage(
              sidebarPanel(
                   sliderInput("mean", "choose mean", -10, 10, 1),
                   sliderInput("sd", "choose sd", 0, 5, 1)),
              mainPanel(
                  plotOutput("hist"),
                  downloadButton("histDownload")
              )
          )
          ),
  server = function(input, output) {
  
      output$hist &lt;- renderPlot(.hist())
  
      .hist &lt;- reactive(helpfunc(input$mean, input$sd))
  
      output$histDownload &lt;- downloadHandler(
          filename = function() {
              paste("hist.jpg")
          }, 
          content = function(file) {
              jpeg(file, quality = 100, width = 800, height = 800)
              .hist() ## works not for plot(.hist()) either
              dev.off()
          }
      )
  
  }
  </code></pre>