Plotting multiple symbols with reactive instruction using chartSeries

I'm new to Shiny and ended up with a brilliant tutorial here: http://shiny.rstudio.com/tutorial/

Lesson 6 in the tutorial shows you how to create an application where you enter a stock symbol and a date range to see its chart in the main panel.

I'm trying to go a little further by changing my app to one that takes 2 stock symbols and draws them on one chart, comparing them over time (overlapping on one chart).

I changed server.R to be:

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

      

and my 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"))
  )
))

      

I get:

Error in try.xts (x, error = "chartSeries requires xtsible"): chartSeries requires xxtable

I tried to convert dataInput to XTS, but XTS and react seems to be causing a lot of trouble for me with my limited understanding of what is going on.

+3


source to share


1 answer


as @RHertel mentioned it is not that easy to do. But to illustrate this, I modified your server.R

script a bit so you can see how it turns out. I added some comments to the script so you can follow what has changed.

First, I split the two datasets because once installed, auto.assign=TRUE

you can no longer call dataInput

, hoping it will return two datasets. what it does auto.assign=TRUE

, it automatically assigns your two datasets separately to the global environment. So there is another way to call datasets in this case. To avoid problems, separate the two datasets so you can set auto.assign=FALSE

and call datasets from `dataInput.

I then saved the two datasets in a list so you can split them later in the script, in output$plot

.



Finally, I added an argument TA

as described by @RHertel above. I had to use a function paste

to automate the process.

As you can see, the result looks ugly and needs some cleaning. But I just wanted to give you an idea of ​​what it would look like. I'm not a finance expert, but I would almost never want to see multiple stock prices on the same chart because they can vary significantly and you can end up with strange charts like the one below. To standardize the scale, values ​​are usually displayedon the same chart, because although prices may be different, the yield has a lower and upper limit of 0-100%. Thus, they will 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)
  }) 
})

      

0


source







All Articles