在Shiny中将多行放在dygraph上

时间:2016-10-30 00:40:13

标签: r shiny dygraphs

有人可以解释为什么我的代码不会在dygraph上创建一个有两行的闪亮应用程序(选择两个选项时),X轴是时间吗?当我只在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)

1 个答案:

答案 0 :(得分:0)

您必须删除ts周围的do.call ... :

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