简体   繁体   中英

I have a multicolumn ts in R and I want to plot 2 of them using dygraph

I´ve got a multicolumn ts in R and I want to create a shiny app that allows me to choose which of these columns should be displayed on the dygraph plot.

I´ve read something about the visibility mode, but I´m not familiar with JS and i couldn´t find a way to use it in R. I´ve also tried to subset my ts usind something like:

 `new.ts <- as.data.frame(my.ts)
   new.ts <- my.ts$input$model
   new.ts <- ts(new.ts, start= (...), freq=...)
dygraph(new.ts)`

`library(shiny)
library(dygraphs)

ui <- fluidPage(

  # Application title
  titlePanel("Title"),

  # Selecionar modelo
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "model",
                  label = "Choose the model to be plotted",
                  choices  = c("...") 
                  selected = "x")),
    mainPanel(
      dygraphOutput(outputId = "dygraph")
    )
  )
  server <- function(input, output) {
    output$dygraph <- renderDygraph({
      dygraph(my.ts, main =glue("Forecast vs. Realizado {nome.modelo}")) %>%
        dySeries("realizado", drawPoints = TRUE, pointSize = 3,color = "red") %>%
        dySeries(input$model, drawPoints = TRUE,pointSize = 3,color = "blue")%>%
        dyRangeSelector()

    })
  }
  shinyApp(ui = ui, server = server)` 

The code above renders me a plot with all the columns of the ts

I´ve found a way to fix it, though I´m not sure it is the most effective one. I´ve created a reactive expression that subsets my df. After doing this, I´ve coerced the df to a ts again.

dataInput <- reactive({
    n <- which(colnames(base)==input$modelo_escolhido)
    x <- a[,c(n,13),drop=FALSE] 
    x <- ts(x,start=start(base),frequency = frequency(base))
  })
  output$dygraph <- renderDygraph({
    dygraph(dataInput(),
            main = "Forecast vs. Realizado") %>% 
      dyOptions(drawPoints = TRUE, pointSize = 3,colors = RColorBrewer::brewer.pal(3, "Set1")) %>%
      dyRangeSelector()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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