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.
Here is my 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? I have tried many combination, but then I have always errors such as "not subsetable".
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
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