Search code examples
rggplot2shinyquantmod

Interactive shiny app // Error:Discrete value supplied to continuous scale


I downloaded some financial data (closing stock prices for each day of 2016) from quantmod using this code:

library(quantmod)
DJ30_smbl_list = c("AAPL", "AXP", "BA", "CAT", "CSCO", "CVX","DWDP", "DIS", "GE",
               "GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM",
               "MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V",
               "VZ", "WMT", "XOM")

# use a for loop to import closing price to DJIA
DJIA <- c()
for(j in DJ30_smbl_list){
DJIA<- cbind(DJIA, getSymbols(j, auto.assign = F, from =   "2016-01-01", to = "2017-01-01")[,4])
    }
colnames(DJIA) = DJ30_smbl_list

I am trying to create an interactive shiny app, in which I pick 2 stocks from DJIA (.xts) and get a correlation plot of them using this code:

ui=fluidPage(
titlePanel("Correlation of Stocks"),
selectInput(inputId="stock",label="Choose stock 1:",c("AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DWDP", "DIS", "GE",
               "GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM",
               "MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V",
               "VZ", "WMT", "XOM")),
selectInput(inputId="stock2",label="Choose stock 2:",c("AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DWDP", "DIS", "GE",
               "GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM",
               "MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V",
               "VZ", "WMT", "XOM")),
plotOutput(outputId="scatter"))
server=function(input,output){
output$scatter<-renderPlot({ggplot(DJIA, aes(input$stock, input$stock2)) + geom_point() + scale_x_continuous(input$stock, breaks = seq(0, 150,10))+ scale_y_continuous(input$stock2, breaks = seq(0, 150,by = 10))+ theme_bw()+labs(title="STOCKS")})
}
shinyApp(ui=ui,server=server)
```

After that, I get the error written in the title. Could you help me out? thanks in advance


Solution

  • You forgot the get in the aesthetics of ggplot.

    Your render function should be as follows:

    server=function(input,output){
    
      output$scatter<-renderPlot({
        ggplot(DJIA, aes(get(input$stock), get(input$stock2))) + 
         geom_point() + 
         scale_x_continuous(input$stock, breaks = seq(0, 150,10))+ 
         scale_y_continuous(input$stock2, breaks = seq(0, 150,by = 10))+ 
        theme_bw()+
         labs(title="STOCKS")})