Plot 如何在Rmarkdown shinny中绘制分层时间序列

Plot 如何在Rmarkdown shinny中绘制分层时间序列,plot,shiny,time-series,r-markdown,Plot,Shiny,Time Series,R Markdown,我希望按照下面的代码绘制包含在列表中的分层时间序列: 图书馆和数据创建 title: "prueba shiny" runtime: shiny output: html_document --- library(dplyr) library(tidyr) library(dygraphs) library(tseries) df <- data.frame(date = c(as.yearmon(2018,1),as.yearmon(2018,1),as.yearm

我希望按照下面的代码绘制包含在列表中的分层时间序列:
图书馆和数据创建

title: "prueba shiny"
runtime: shiny
output: html_document
---
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"))


我希望通过从selectInput中选择类别来创建这两个图。我试过这样的方法但没有成功:


selectInput("I", label = "category_I:",
           choices = unique(cat$I), selected = cat$I[1])
selectInput("II", label = "II", choices = unique(cat$II))

plotOutput(outputId = "dy")

data <- reactive({
  ts_I_II$input$II})

output$dy <- renderPlot({
   dygraph(data)


选择输入(“I”,label=“category\u I:”,
选项=唯一(类别$I),选中=类别$I[1])
选择输入(“II”,label=“II”,choices=unique(类别$II))
plotOutput(outputId=“dy”)
数据
aux <- with(cat,split(II,I))
ts_I_II <-  lapply(aux, function(x) ts[x])

catI_sales <- list()
for (s in unique(cat$I)){
  catI_sales[[s]] <- do.call(cbind,ts_I_II[[s]])
}
aux <- do.call(cbind,ts_I_II$drink)
dygraph(aux, main = "sales by cat_II") %>% dyOptions(colors = RColorBrewer::brewer.pal(5, "Set2"))
dygraph(ts_I_II$drink$cola, main = "sales by cat_II") %>% dyOptions(colors = RColorBrewer::brewer.pal(5, "Set2"))


selectInput("I", label = "category_I:",
           choices = unique(cat$I), selected = cat$I[1])
selectInput("II", label = "II", choices = unique(cat$II))

plotOutput(outputId = "dy")

data <- reactive({
  ts_I_II$input$II})

output$dy <- renderPlot({
   dygraph(data)

selectInput("II", label = "category_II:",
           choices = names(ts))


dygraphs::renderDygraph({
  dygraph(ts[[input$II]])
})
  
selectInput("I", label = "category_I:",
           choices = names(catI_sales))


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