Using saved ggplot2 figure with shiny in r

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 ggplot figure from iris data as :

twoClassIrisSepalLength <- subset(iris[1:100,], select=c("Species","Sepal.Length"))

twoClassIrisSepalLength$Species <- factor(twoClassIrisSepalLength$Species)

g <- ggplot(twoClassIrisSepalLength,aes(x=Sepal.Length, fill=Species)) 
     + geom_density(alpha=0.5) + scale_size_area()  

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


When I add an intercept

g <- g+geom_vline(aes(xintercept= 27%%7),  
     colour="#BB0000", linetype="dashed", size=2)

I get the figure as I want as shown below:

I want to draw that darkred line in value 6 dynamically in R shiny. Now in shiny my ui.R looks like:

  headerPanel('Density plots'),
    numericInput('sepal_length', 'Sepal Length', 3,
                 min = 1, max = 7)

and my server.R is

shinyServer(function(input, output, session) {

output$plot1 <- renderPlot({

    g <- g+geom_vline(aes(xintercept= as.numeric(input$sepal_length %% 7)), 
colour="#BB0000", linetype="dashed", size=2)



When I execute the program I get an error: Error: object 'input' not found

When I give a specific value (not input$sepal_length) 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 renderPlot to renderText and print value of input$sepal_length it prints the entered value. Could you please guide where I went wrong?


Your using mapping (aes) to insert a variable to `geom_vline? change:

g <- g+geom_vline(aes(xintercept= as.numeric(input$sepal_length %% 7)), 
colour="#BB0000", linetype="dashed", size=2)


g <- g+geom_vline(xintercept= as.numeric(input$sepal_length %% 7), 
colour="#BB0000", linetype="dashed", size=2)

and it should work.

Need Your Help

RSA, calculating value from lists

python rsa primes

I am writing my script to provide me with an RSA public and private key. I am trying to calculate the values of 'e' and 'd' where ed = 1mod64. I know the values will be e=5 and d=13. I have two lis...

SQL Server 2005/2008: Identify current user

sql-server sql-server-2005 tsql sql-server-2008 user-management

I have a web application, which is using a SQL Server 2005 database.