闪亮的股票图表错误:chartSeries 需要一个 xtsible 对象 [英] Shiny Stock Chart Error: chartSeries requires an xtsible object

查看:41
本文介绍了闪亮的股票图表错误:chartSeries 需要一个 xtsible 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码.我想输入有效的股票代码并渲染烛台图.我还想为用户提供在 20、50 和 200 天简单移动平均线之间切换的选项.但是,当我运行应用程序时,mainPanel 返回:

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. 

这是我的第一个 Shiny App 开发,我不知道为什么它不起作用.

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

推荐答案

在我的代码中发现了错误.删除了 lubridate 包,因为在 getSymbols 函数中不需要它.将 addSMA 函数移动到 moveAvg 反应语句中.当我开始这个项目时,代码就像我想象的那样工作.

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.

这篇关于闪亮的股票图表错误:chartSeries 需要一个 xtsible 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆