【问题标题】:Putting multiple lines on dygraph in Shiny在 Shiny 中在 dygraph 上放置多行
【发布时间】:2017-03-12 12:35:17
【问题描述】:

有人可以解释为什么我的代码没有创建一个闪亮的应用程序,在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)

【问题讨论】:

    标签: r shiny dygraphs


    【解决方案1】:

    您必须删除 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()
      })
    })
    

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 2017-10-06
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2017-01-23
      • 1970-01-01
      • 2015-12-06
      相关资源
      最近更新 更多