Shiny Stock Chart Error: chartSeries requires an xtsible object

喜欢而已 提交于 2021-01-29 13:08:04

问题


I have the following code. I want to take the input of a valid stock symbol and render a candlestick plot. I want to give the user the option to switch between 20, 50, and 200 day simple moving averages on the plot as well. However, when I run the app, the mainPanel returns:

Error: chartSeries requires an xtsible object. 

This is my first Shiny App development and I am at a loss as to why it is not working.

library(shiny)
library(quantmod)
library(lubridate)

shinyUI(fluidPage(
    titlePanel("Candlestick Stock Charts"),
    sidebarLayout(
        sidebarPanel(
            textInput("symb", "Input a Valid Stock Symbol", "AAPL"),

            radioButtons("radioMoveAvg",
                         "Moving Averages",
                         c("20-day" = "twentyAvg",
                           "50-day" = "fiftyAvg", 
                           "200-day" = "twohundAvg"))
        ),

        mainPanel(
            plotOutput("candleStick")
        )
    )
))

library(shiny)
library(quantmod)
library(lubridate)

shinyServer(function(input, output) {
    cs<-reactive({
        getSymbols(input$symb,
                   src = "yahoo",
                   from = Sys.Date()-years(2),
                   to = Sys.Date(),
                   auto.assign = FALSE)[, 4]

    })

    moveAvg<-reactive({
        if(input$radioMoveAvg=="twentyAvg"){
            x = 20
            col = "blue"
        }
        if(input$radioMoveAvg=="fiftyAvg"){
            x = 50
            col = "green"
        }
        if(input$radioMoveAvg=="twohundAvg"){
            x = 200
            col = "pink"
        }
    })

    output$candleStick <- renderPlot({
        candleChart(cs, up.col = "black", dn.col = "red", theme = "white", subset = "2019-01-01/")
        addSMA(moveAvg)
    })
})

回答1:


Found the errors in my code. Removed the lubridate package because it was not needed in the getSymbols function. Moved the addSMA function into the moveAvg reactive statement. The code is working as I pictured when I began the project.



来源:https://stackoverflow.com/questions/56229787/shiny-stock-chart-error-chartseries-requires-an-xtsible-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!