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

时间:2020-08-31 09:20:57

标签: r shiny dygraphs

我几乎已经完成了构建应用程序以浏览我的一篇论文中发布的数据的工作,并且认为通过添加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)

如您所见,当dyOptions()中的stackedGraph = FALSE时一切正常,但是当TRUE时,仅包括第一个时间序列(的一部分)……我想念什么?

1 个答案:

答案 0 :(得分:2)

看起来像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选项。

相关问题