Lucas
Lucas

Reputation: 599

can´t plot dygraph on markdown

I wish to plot a time series with dygraph inside a markdown document. I can select the time series from a list and plot it with plot() function but it does not work on the same way with dygraph function


library(dplyr)
library(tidyr)
library(dygraphs)
library(tseries)

df <- data.frame(date = c(as.yearmon(2018,1),as.yearmon(2018,1),as.yearmon(2018,2),as.yearmon(2018,2),
                          as.yearmon(2018,1),as.yearmon(2018,1),as.yearmon(2018,2),as.yearmon(2018,2)),                sales = c(1,2,3,4),
                 cat_I = c("drink","drink","food","food","drink","drink","food","food"),
                 cat_II = c("cola","fanta","tomatoes","bananas","cola","fanta","tomatoes","bananas"))

cat <- data.frame(I = c("drink","drink","food","food"),
                  II = c("cola","fanta","tomatoes","bananas"))

ts <- list()

for(s in unique(cat$II)){
  aux <- df %>%  filter(cat_II==s) %>%  
    as.data.frame()
  ts[[s]] <- ts(aux$sales,start=c(2018,1),frequency = 12)
}


selectInput("I", label = "category_I:",
           choices = names(ts))



renderPlot({
  plot(ts[[input$I]])
  
})
  

This works fine, but it doesn´t work when I try to plot with dygraph()

renderPlot({
  dygraph(ts[[input$I]])
  
})

Upvotes: 1

Views: 109

Answers (1)

Waldi
Waldi

Reputation: 41260

You should use dygraphs::renderDygraph instead of renderPlot

dygraphs::renderDygraph({
  dygraph(ts[[input$I]])
})

Upvotes: 1

Related Questions