使用闪亮的 dygraph

Fra*_*nky 2 r dygraphs shiny

我最近开始使用 dygraph,到目前为止我非常喜欢它。我曾尝试使用它闪亮,但没有取得太大成功。虽然我的脚本没有产生任何错误,但它也没有产生任何图形!
你有没有机会指导我朝着正确的方向前进?

这是我的数据示例:

> head(df2)
        date     Variety   Count Price Value Quantity TotalKg
1 2014-11-06 CRIPPS PINK   80-90   204  3670       18     333
2 2014-11-06 CRIPPS PINK 120-135   181 10150       56    1036
3 2014-11-06  CRIPPS RED   80-90   221 26910      122    2257
4 2014-11-06  CRIPPS RED 100-110   205 22910      112    2072
5 2014-11-06  CRIPPS RED 120-135   193 58950      306    5661
6 2014-11-06      TOPRED   80-90   167  7350       44     814
Run Code Online (Sandbox Code Playgroud)

使用 Variety 和 Count 变量,我想绘制价格随时间变化的图表。

这是我的 ui.R

library(dygraphs)
library(shiny)

shinyUI(fluidPage(
  titlePanel("Apples Prices"),

  sidebarLayout(
          sidebarPanel(
            selectInput("productname", "Select your product",
                        choices = levels(df2$Variety)),
            selectInput("count",  "Select your size",
                        choices = levels(df2$Count))),

            mainPanel(
              dygraphOutput("applesgraph"))
          )))
Run Code Online (Sandbox Code Playgroud)

和服务器端:

library(shiny)
library(dygraphs)
library(dplyr)
library(xts)

shinyServer(function(input, output) {

  dfa <- reactive({df2 %>% filter(Variety == input$productname & 
                                    Count == input$count )})

#the first graph which is price over time (input: variety, count, date)
  output$applesgraph <- renderDygraph({
   xts(dfa()$Price, order.by = dfa()$date) %>% dygraph()
  })
})
Run Code Online (Sandbox Code Playgroud)

我能感觉到我对 dplyr 和时间序列对象有错误的方法......我应该什么时候过滤?我尝试了很多组合,但是我总是出现诸如“不可子集化”之类的错误。

预先感谢您可以给我的任何光线/方向。

fai*_*ard 5

由于您需要输入数据 in server.R(对于图表)和 in ui.R(对于输入列表),我添加了一个renderUI({...})inserver.R和一个uiOutput(...)inui.R

# server.R
library(shiny)
library(dygraphs)
library(dplyr)
library(xts)

shinyServer(function(input, output) {
  data <- read.csv("cleanApples.csv") %>%
    filter(Quantity > 10)

  #the first graph which is price over time (input: variety, count, date)
  output$applesgraph <- renderDygraph({
    if (is.null(input$productname) || is.null(input$count)) return(NULL)
    filtered <- filter(data,
                       Variety == input$productname,
                       Count == input$count )
    xts(filtered$Price, as.Date(filtered$date, format = "%Y-%m-%d")) %>%
      dygraph()
  })

  output$productnames <- renderUI({
    selectInput("productname", "Select your product",
                choices = levels(data$Variety))
  })

  output$counts <- renderUI({
    selectInput("count",  "Select your size",
                choices = levels(data$Count))
  })
})
Run Code Online (Sandbox Code Playgroud)

# ui.R
library(dygraphs)
library(shiny)

shinyUI(fluidPage(
  titlePanel("Apples Prices"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("productnames"),
      uiOutput("counts")
    ),

    mainPanel(
      dygraphOutput("applesgraph"))
  )))
Run Code Online (Sandbox Code Playgroud)

现在它适用于shinyapps.io