0

Hello I am new to r shiny I am trying to create an app that plots stocks from quantmod in r like this enter image description here

this is the code I have

library(shiny)

server = function(input, output, session) {
  output$plot <- renderPlot({
    data <- getSymbols(input$stock, 
                       from = input$date[1],
                       to = input$date[2]
                      )

    chartSeries(data, theme = chartTheme("white"),
                type = "line", log.scale = input$log, TA = NULL)
  })
} # the server

ui = basicPage(
  h1("stock app"),
  textInput("stocks", "pick stock"),
  dateRangeInput("date", "date range ", start = "2013-01-01", end = "2020-03-15",min = "2007-01-01", max = "2020-03-15",format = "yyyy-mm-dd" ),
  plotOutput("plot")
) # the user interface

shinyApp(ui = ui, server = server) # perform app launch

however instead of plotting the stock series my app is returning an error like this

Error: chartSeries requires an xtsible object.

I would like to know why my application does not plot the stock in my input

pelumi obasa
  • 127
  • 5
  • 1
    Several answers for the same error message: [here](https://stackoverflow.com/questions/56229787/shiny-stock-chart-error-chartseries-requires-an-xtsible-object), [here](https://stackoverflow.com/questions/57699454/shiny-app-error-chartseries-requires-an-xtsible-object) or [here](https://stackoverflow.com/questions/36120961/xtsible-object-looping-in-quantmod) – bretauv Mar 22 '20 at 15:46

1 Answers1

1

Here is a working version, some minor changes needed (see comments below).

Your input$stocks needs to match the inputId in the ui. It was missing an 's' in server.

You need auto.assign = FALSE in getSymbols, as by default data goes to parent.frame.

I added a default stock to textInput so you won't get an error on startup.

chartSeries references input$log but there's no input in ui that matches. Added a checkbox for that.

library(shiny)
library(quantmod)

server = function(input, output, session) {
  output$plot <- renderPlot({
    data <- getSymbols(input$stocks,  # needs to match textInput, missing s
                       from = input$date[1],
                       to = input$date[2],
                       auto.assign = FALSE   # getSymbols returns data to parent.frame by default
    )
    chartSeries(data, theme = chartTheme("white"),
                type = "line", log.scale = input$log, TA = NULL) # no input for "log", needs to be added to ui
  })
} # the server

ui = basicPage(
  h1("stock app"),
  textInput("stocks", "pick stock", "AAPL"),   # added default stock
  dateRangeInput("date", "date range ", start = "2013-01-01", end = "2020-03-15",min = "2007-01-01", max = "2020-03-15",format = "yyyy-mm-dd" ),
  checkboxInput(inputId = "log", label = "log y axis", value = FALSE),  # added "log" input
  plotOutput("plot")
) # the user interface

shinyApp(ui = ui, server = server) # perform app launch
Ben
  • 28,684
  • 5
  • 23
  • 45