title: Using saved ggplot2 figure with shiny in r
body: <p>I want to plot a density plot of figure save it and render it in the shiny app. What I want to add to the figure dynamically is a line from input showing where the new entered variable lies in the density plot: I saved the <code>ggplot</code> figure from iris data as :</p>
  
  <pre><code>library(ggplot2)
  twoClassIrisSepalLength &lt;- subset(iris[1:100,], select=c("Species","Sepal.Length"))
  
  twoClassIrisSepalLength$Species &lt;- factor(twoClassIrisSepalLength$Species)
  
  g &lt;- ggplot(twoClassIrisSepalLength,aes(x=Sepal.Length, fill=Species)) 
       + geom_density(alpha=0.5) + scale_size_area()  
  
  g &lt;- g+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
  axis.text=element_text(size=14),plot.title = element_text(size = rel(1.5), 
  colour = "black"),    axis.title=element_text(size=16,face="bold"),
  panel.background = element_blank(), axis.line = element_line(colour = "black"),
  legend.justification = c(1, 1), legend.position = c(1, 1))
  
  save(g,file="ggplot_sepal_length_density.rda")
  </code></pre>
  
  <p>When I add an intercept </p>
  
  <pre><code>g &lt;- g+geom_vline(aes(xintercept= 27%%7),  
       colour="#BB0000", linetype="dashed", size=2)
  </code></pre>
  
  <p>I get the figure as I want as shown below:</p>
  
  <p><a href="https://i.stack.imgur.com/g6R2j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g6R2j.png" alt="enter image description here"></a></p>
  
  <p>I want to draw that darkred line in value 6 dynamically in R shiny. Now in <code>shiny</code> my <code>ui.R</code> looks like:</p>
  
  <pre><code>library(ggplot2)
  shinyUI(pageWithSidebar(
    headerPanel('Density plots'),
    sidebarPanel(
      numericInput('sepal_length', 'Sepal Length', 3,
                   min = 1, max = 7)
    ),
    mainPanel(
      plotOutput('plot1')
    )
  ))
  </code></pre>
  
  <p>and my <code>server.R</code> is </p>
  
  <pre><code>shinyServer(function(input, output, session) {
  
  output$plot1 &lt;- renderPlot({
      load("ggplot_sepal_length_density.rda")
  
      g &lt;- g+geom_vline(aes(xintercept= as.numeric(input$sepal_length %% 7)), 
  colour="#BB0000", linetype="dashed", size=2)
      g
  
    })
  
  })
  </code></pre>
  
  <p>When I execute the program I get an error: <code>Error: object 'input' not found</code></p>
  
  <p>When I give a specific value (<code>not input$sepal_length</code>) like 5 it plots as it should, when I comment out the intercept line it renders the plot, without the redline as expected. When I change my <code>renderPlot</code> to <code>renderText</code> and print value of <code>input$sepal_length</code> it prints the entered value. Could you please guide where I went wrong?</p>