Fred-LM
Fred-LM

Reputation: 330

Issue with inserting a dygraph in shiny app

I'm almost done building an app to explore data published in one of my papers, and thought that it would be nice to have something a little more interactive by adding a dygraph instead of a regular ggplot. Hence my problem... :)

Here is the code I have so far.

EDIT: Thanks to Waldi's comments below, I've slightly modified my code, and minimized it here to facilitate the process

library(shiny)
library(dygraphs)
library(xts)
library(tidyverse)
library(openxlsx)

Sys.setlocale("LC_TIME", "C")

data <- read.xlsx("https://www.bloomassociation.org/wp-content/uploads/2020/08/data.xlsx", sheet = 1) %>%
  mutate(date = as.Date(date, origin = "1899-12-30"))

# Define UI for application that draws a histogram
ui <- fluidPage(# Define filters 
                fluidRow(
                  
                  column(4,
                         selectInput("variableInput", label = h4("Show fisheries by:"), 
                                     unique(data$variable))),
                  column(4,
                         selectInput("unitInput", label = h4("Display data as:"), 
                                     unique(data$unit))),
                  column(4,
                         sliderInput("dateInput", label = h4("Select time range:"),
                                     min = as.Date("2000-01-01"), 
                                     max = as.Date("2017-12-31"), 
                                     value = c(as.Date("2000-01-01"), as.Date("2017-12-31")), 
                                     timeFormat = "%b %Y")
                  ),
                  # Display results
                  tabsetPanel(
                    tabPanel("Graphical view", withSpinner(dygraphOutput("distPlot"), type = getOption("spinner.type", default = 5), color = getOption("spinner.color", default = "#0A1D27"), size = getOption("spinner.size", default = 0.5))))
                ))

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  filtered_xts <- reactive({
    data_ <- data %>%
      filter(variable == input$variableInput,
             unit == input$unitInput,
             date >= input$dateInput[1],
             date <= input$dateInput[2]
      ) %>%
      select(-c(4:5)) %>%
      mutate(quantity = round(quantity, 1)) %>%
      spread(key = "category", value = "quantity") %>%
      replace(is.na(.), 0)
    # Debug the filtering // Solution provided by @Waldi; seems to fix most of my problem (see below)
    print(data_)
    data_ <- xts(data_, order.by = data_$date)
    # Debug the xts conversion step
    print(data_)
  })
  
  output$distPlot <- renderDygraph({
    dygraph(filtered_xts()) %>%
      dyOptions(fillGraph = TRUE, drawGrid = TRUE, stackedGraph = FALSE) #When stackedGraph = FALSE, everything works well, but I want it TRUE => it no longer works...
  }
  )
}

# Run the application 
shinyApp(ui = ui, server = server)

As you can see, everything works fine when stackedGraph = FALSEin the dyOptions() but it looks like only (part of) the first time-series is included when TRUE... what am I missing?

Upvotes: 2

Views: 277

Answers (1)

Waldi
Waldi

Reputation: 41210

Looks like filtered_xts() doesn't output any value.
Try:

  filtered_xts <- reactive({
    data_ <- data %>%
      filter(variable == input$variableInput,
             unit == input$unitInput,
             date >= input$dateInput[1],
             date <= input$dateInput[2]
      ) %>%
      select(-c(4:5)) %>%
      mutate(quantity = round(quantity, 1)) %>%
      spread(key = "category", value = "quantity") %>% 
      replace(is.na(.), 0)  %>% data.table::as.data.table()
  })

Following our discussion in comments, the conversion to data.table is more efficient than conversion to xts to be able to fully use dygraphs options.

Upvotes: 2

Related Questions