使用chartSeries使用响应语句绘制多个符号

时间:2015-07-26 20:37:05

标签: r shiny xts quantmod

我是Shiny的新手,在这里完成了Shiny教程: http://shiny.rstudio.com/tutorial/

在第6课中,本教程向我们展示了如何创建一个应用程序,您可以在其中输入股票代码和日期范围,以在主面板上查看其图表。

我试图通过将应用程序更改为带有2个股票代码的应用程序并在同一图表上绘制它们来比较它们(在同一图表上重叠)。

我已将server.R修改为:

library(quantmod)

shinyServer(function(input, output) {



  dataInput <- reactive({   
        getSymbols(c(input$symb1, input$symb2), src = "yahoo", 
        from = input$dates[1],
        to = input$dates[2],
        auto.assign = TRUE)
  })


  output$plot <- renderPlot({
    chartSeries(dataInput(), theme = chartTheme("white"), 
        type = "line", log.scale = input$log, TA = NULL)
  }) 
})

和我的uiR:

library(shiny)

shinyUI(fluidPage(
  titlePanel("StockComp"),

  sidebarLayout(
    sidebarPanel(
      helpText("Select two stocks and a time frame to compare. 
        Information will be collected from yahoo finance."),

      textInput("symb1", "1st Stock Symbol", "GOOG"),
      textInput("symb2", "2nd Stock Symbol", "AAPL"),

      dateRangeInput("dates", 
        "Date range",
        start = "2012-01-01", 
        end = as.character(Sys.Date())),

      actionButton("get", "Compare Stocks"),

      br(),
      br(),

      checkboxInput("log", "Plot y axis on log scale", 
        value = FALSE)

    ),

    mainPanel(plotOutput("plot"))
  )
))

我得到了:

try.xts中的错误(x,error =“chartSeries需要xtsible对象”):   chartSeries需要一个xtsible对象

我试图将dataInput转换为XTS,但XTS和被动似乎对我造成了很多问题,因为我对最新情况的了解有限。

1 个答案:

答案 0 :(得分:0)

as @RHertel mentioned, this is not so easy to do. But just to illustrate, I've tweaked your server.R script a little bit so you can see how it would turn out. I've added some comments in the script so you can follow what was changed.

First, I've separated the two data sets because once you set auto.assign=TRUE you cannot call dataInput anymore hoping it would return the two data sets. what auto.assign=TRUE does is it automatically assigns your two data sets separately to the global environment. So there's a different way to call the data sets in that case. In order to avoid the hassle, separate the two data sets so you can set auto.assign=FALSE and call the data sets from `dataInput.

Next, I stored the two data sets in a list so you can call them separately later in the script, in output$plot.

Lastly, I added the TA argument as @RHertel explained above. I had to use the paste function to automate the process.

As you can see the output looks ugly and still needs some cleaning. But I just wanted to give you an idea of how it would look like. I'm not an expert in finance but I would almost never want to see multiple stock prices on one chart because they could vary significantly and you can get weird charts like the one below. In order to standardize the scale, usually the stock returns are plotted on a single chart because even though the prices may be different, the returns have a lower and an upper limit of 0-100%. So they would fall on the same scale.

library(quantmod)

shinyServer(function(input, output) {



  dataInput <- reactive({   
    data1 <- getSymbols(input$symb1, src = "yahoo",      #Seperated the data into two seperate data sets and set auto.assign=FALSE
               from = input$dates[1],
               to = input$dates[2],
               auto.assign = FALSE)
    data2 <- getSymbols(input$symb2, src = "yahoo",     #Seperated the data into two seperate data sets and set auto.assign=FALSE
                        from = input$dates[1],
                        to = input$dates[2],
                        auto.assign = FALSE)
    return (list(data1,data2))                          #Stored the data sets in a single list 
  })


  output$plot <- renderPlot({
    chartSeries(dataInput()[[1]], TA=paste0("addTA(",input$symb1,",on=1)"),theme = chartTheme("white"),    #added the TA argument with the paste helper function
                type = "line", log.scale = input$log)
    chartSeries(dataInput()[[2]], TA=paste0("addTA(",input$symb2,",on=1)"),theme = chartTheme("white"), 
                type = "line", log.scale = input$log)
  }) 
})