使用闪亮的

时间:2015-05-11 19:41:38

标签: r shiny dygraphs

我最近开始使用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

使用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"))
          )))

和服务器端:

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

我可以感觉到我对dplyr和时间序列对象有错误的方法......我什么时候应该过滤呢?我尝试了很多组合,但后来我总是出现“不可订购”这样的错误。

提前感谢你能给我的任何光线/方向。

1 个答案:

答案 0 :(得分:4)

由于您需要ui.R(针对图表)和renderUI({...})(针对输入列表)中的输入数据,我在server.R中添加了uiOutput(...)并且ui.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)) }) })
# ui.R
library(dygraphs)
library(shiny)

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

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

    mainPanel(
      dygraphOutput("applesgraph"))
  )))

{{1}}

现在它有效in shinyapps.io