Search code examples
rshinydygraphs

Putting multiple lines on dygraph in Shiny


Can someone explain why my code does not create a shiny app with two lines on the dygraph (when two options are selected), with the X axis as time? This seems to work correctly when I only allow one line on the dygraph.

#rm(list=ls())
library(shiny)
library(jsonlite)
library(dygraphs)
library(xts)
library(dplyr)
library(readr)

item.ids<-read_fwf('http://eve-files.com/chribba/typeid.txt',fwf_empty('http://eve-files.com/chribba/typeid.txt',skip = 2,col_names = c('typeID', 'typeName')),skip = 19)
item.ids$typeID<-as.numeric(item.ids$typeID)

ui<-fluidPage(
  titlePanel("EVE Mineral Price Graph"),

  sidebarLayout(
    sidebarPanel(
      helpText("EVE Mineral Price Graph"),

      #restricted to comparing 2 things because of dygraph
      selectizeInput('var.name', label = NULL, choices = item.ids$typeName, multiple=T,selected=item.ids$typeName[1],options = list(maxItems = 2,maxOptions=5)),

      selectInput("var.col", 
                  label = "Choose a variable to display",
                  choices = c("lowPrice","avgPrice","highPrice","volume","orders"),
                  selected = "lowPrice")
    ),
    mainPanel(dygraphOutput("map"))
))

##########################################################################################

server<-shinyServer(
  function(input, output) {

    getColumn<-reactive({
###this lapply takes in the var name from the text input, converts it to a number, retrieves the data from a website then returns the time series.      
      ans<-lapply(input$var.name,function(x){
        item.id<-item.ids$typeID[which(item.ids$typeName==x)] #user chedked text name, this converts to number
        eve.url<-paste0("http://eve-marketdata.com/api/item_history2.json?char_name=demo&region_ids=10000002&type_ids=",item.id,"&days=100")
        eve.data<-data.frame(fromJSON(txt=eve.url))$emd.row
        eve.data$date<-as.Date(eve.data$date)
        data<-as.vector(as.numeric(eve.data[,input$var.col]))
        xxx<-xts(data,order.by=eve.data$date)
        colnames(xxx)<-x
        xxx
      })
      ts(do.call(cbind, ans))
    })

    output$map <- renderDygraph({
      data<-getColumn()
      browser()
      #plot(stl(data, s.window="periodic"))
      #plot(decompose(data))      
      #plot(data)
      dygraph(data, main="Mineral Graph",xlab="day",ylab="item price") %>% dyRangeSelector()
    })
  }
)

#####
shinyApp(ui, server)

Solution

  • You have to remove the ts around the do.call... e.g. :

    server <- shinyServer(function(input, output) {
      getColumn <- reactive({
        ###this lapply takes in the var name from the text input, converts it to a number, retrieves the data from a website then returns the time series.
        ans <- lapply(input$var.name, function(x) {
          item.id <-
            item.ids$typeID[which(item.ids$typeName == x)] #user chedked text name, this converts to number
          eve.url <- paste0(
            "http://eve-marketdata.com/api/item_history2.json?char_name=demo&region_ids=10000002&type_ids=",
            item.id,
            "&days=100"
          )
          eve.data <- data.frame(fromJSON(txt = eve.url))$emd.row
          eve.data$date <- as.Date(eve.data$date)
          data <- as.vector(as.numeric(eve.data[, input$var.col]))
          xxx <- xts(data, order.by = eve.data$date)
          colnames(xxx) <- x
          xxx
        })
        do.call(cbind, ans)
      })
    
      output$map <- renderDygraph({
        data <- getColumn()
        # browser()
        #plot(stl(data, s.window="periodic"))
        #plot(decompose(data))
        #plot(data)
        dygraph(data,
                main = "Mineral Graph",
                xlab = "day",
                ylab = "item price") %>% dyRangeSelector()
      })
    })