在闪亮的应用程序中插入 dygraph 的问题

Issue with inserting a dygraph in shiny app

几乎 完成了构建一个应用程序来探索我的一篇论文中发表的数据,并且认为通过添加一个更具交互性的东西会很好dygraph 而不是常规的 ggplot。因此我的问题... :)

这是我目前的代码。

编辑:感谢 Waldi 在下面的评论,我稍微修改了我的代码,并在此处将其最小化以促进流程

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)

如您所见,当 stackedGraph = FALSE 在 dyOptions() 中时,一切正常,但当 TRUE 时,看起来只有(部分)第一个时间序列被包含...我错过了什么?

看起来filtered_xts()没有输出任何值。
尝试:

  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()
  })

根据我们在评论中的讨论,转换为 data.table 比转换为 xts 更有效,能够充分利用 dygraphs 选项。