简体   繁体   English

使用带光笔

[英]using dygraph with shiny

I recently started using dygraph and I quite enjoyed it so far. 我最近开始使用音笔,到目前为止我非常喜欢。 I have tried to use it then with shiny without much success. 我曾尝试将其用于光泽效果不佳的产品。 Although my script doesn't yield any errors, it also doesn't produce any graph! 尽管我的脚本不会产生任何错误,但也不会产生任何图形!
Any chance you could guide me in the right direction? 您是否有机会向正确的方向指引我?

here is a sample of my data: 这是我的数据样本:

> 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

Using the Variety and Count variables, I would like to graph the price over time. 使用Variety和Count变量,我想绘制一段时间内的价格。

Here is my ui.R 这是我的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"))
          )))

and the server side: 和服务器端:

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

I can feel I am having the wrong approach with dplyr and time series object ... when should I filter then? 我可以感觉到我对dplyr和时间序列对象使用了错误的方法...那么我什么时候应该过滤呢? I have tried many combination, but then I have always errors such as "not subsetable". 我尝试了许多组合,但是后来总会出现诸如“不可子集化”的错误。

Thanks in advance for any light / direction you can give me. 预先感谢您能给我的任何指导。

Since you need the input data in server.R (for the graph) and in ui.R (for the input list), I added a renderUI({...}) in server.R and an uiOutput(...) in ui.R 由于您需要在server.R (用于图形)和ui.R (用于输入列表)中输入数据, server.R我在server.RuiOutput(...)添加了renderUI({...}) 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))
  })
})

And

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

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

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

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

Now it works in shinyapps.io 现在可以在shinyapps.io中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM