2

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hmmm... is it working on your side? Not on mine, which is quite frustrating. I've fixed a tiny mistake thanks to your suggestion in the post above (filtered_xtspart), but I still get the same error, although dygraph works perfectly on its own :( By the way, the automatic convertion to xts does not seem to work on my computer, as I get the following error: Error in dygraph(filtered_xts): Unsupported type passed to argument 'data'
@Fred-LM, I didn't try to make it work because the code is not minimal (centered only on the problem). However, I use dyGraph quite a lot and to the best of my knowledge, my answer goes in the right direction (but difficult to be 100%sure without running the whole code).
After checking, automatic conversion to xts applies only when the first column is a datetime on a data.table ( doesn't work on a simple data.frame), I didn't realize this as I work most of the time with data.table. See my edit
Thanks for your help Waldi. I've minimized my post above. Didn't think it would be an issue, but my post is much clearer now. Good rule :) Your solution almost works perfectly, except that stackedGraph = TRUE does not work. I'm looking into it, but don't hesitate to let me know if you find the solution before me :) Thanks a lot.
I don't know exactly why, but dygraphs really prefers data.table : see my edit, as.data.table makes stackedGraph=T work...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.