dygraph 是空白的,但 xts 对象有一个观察

dygraph is blank but xts object has an observation

闪亮版问题(原题):

我正在绘制一个基于 xts 对象的 dygraph,该对象是基于 2 个输入过滤的结果:年龄和语言。

如果我移动年龄滑块,将下限和上限分别设置为 32 并在输入框中输入 "spanish",则绘图为空。但是,过滤后的 tibble 和过滤后的 xts 对象都显示 1 个观察值。这个观察结果应该出现在情节中但没有出现。

我觉得我在这里遗漏了一些非常基本的东西,但我不能指手画脚。

---
title: "test"
output: 
  flexdashboard::flex_dashboard:
    theme: bootstrap
runtime: shiny
---

```{r setup, include=FALSE}
  library(flexdashboard)
  library(tidyverse)
  library(tibbletime)
  library(dygraphs)
  library(magrittr)
  library(xts)
  library(DT)
```

```{r global, include=FALSE}
# generate data
  set.seed(1)
  dat <- data.frame(date = seq(as.Date("2018-01-01"), 
                               as.Date("2018-06-30"), 
                               "days"),
                    sex = sample(c("male", "female"), 181, replace=TRUE),
                    lang = sample(c("english", "spanish"), 181, replace=TRUE),
                    age = sample(20:35, 181, replace=TRUE))
  dat <- sample_n(dat, 80)

```

Sidebar {.sidebar}
=====================================

```{r}
sliderInput("agerange", label = "Age", 
              min = 20, 
              max = 35, 
              value = c(20, 35),
              step=1)

selectizeInput(
  'foo', label = NULL, 
  choices = c("english", "spanish", "other"),
  multiple = TRUE
)
```

Plot
=====================================

```{r}
# all
  filtered <- reactive({
  req((dat$lang %in% input$foo) | is.null(input$foo))
  dat %>%
    mutate(new = 1) %>%
    arrange(date) %>%
    filter(if(is.null(input$foo)) (new==1) else (lang %in% input$foo)) %>%
    filter(age >= input$agerange[1] & age <= input$agerange[2])
  })

  totals <- reactive({  
  filtered <- filtered()
  filtered %>%
  # time series analysis
  tibbletime::as_tbl_time(index = date) %>% # convert to tibble time object
    select(date, new) %>%
    tibbletime::collapse_by("1 week", side = "start", clean = TRUE) %>%
    group_by(date) %>%
    mutate(total = sum(new, na.rm = TRUE)) %>%
    distinct(date, .keep_all = TRUE) %>%
    ungroup() %>%
    # expand matrix to include weeks without data
    complete(
      date = seq(date[1], date[length(date)], by = "1 week"),
      fill = list(total = 0)
    )
  })

# convert to xts
  totals_ <- reactive({
    totals <- totals()
    xts(totals, order.by = totals$date)
  })

# plot
  renderDygraph({

  totals_ <- totals_()
  dygraph(totals_[, "total"]) %>%
    dyRangeSelector() %>%
    dyOptions(useDataTimezone = FALSE,
              stepPlot = TRUE,
              drawGrid = FALSE,
              fillGraph = TRUE) 
  })
```

Filtered Tibble
=====================================

```{r}
  DT::renderDataTable({
    filtered <- filtered()
      DT::datatable(filtered, 
                    options = list(bPaginate = TRUE))
  })
```

Filtered xts
=====================================

```{r}
  DT::renderDataTable({
    totals_ <- totals_()
      DT::datatable(totals_[, c("date", "total")], 
                    options = list(bPaginate = TRUE))
  })
```

非闪亮版本:

我将我的示例移出 shiny(我的实际用例)以隔离问题。

library(tidyverse)
library(tibbletime)
library(dygraphs)
library(magrittr)
library(xts)

set.seed(1)
dat <- data.frame(date = seq(as.Date("2018-01-01"), 
                             as.Date("2018-06-30"), 
                             "days"),
                  sex = sample(c("male", "female"), 181, replace=TRUE),
                  lang = sample(c("english", "spanish"), 181, replace=TRUE),
                  age = sample(20:35, 181, replace=TRUE))
dat <- sample_n(dat, 80)

totals <-
dat %>%
  mutate(new = 1) %>%
  arrange(date) %>%
  filter(lang=="spanish") %>% 
  filter(age>=32 & age<=32) %>%
  {. ->> filtered} %>%
  tibbletime::as_tbl_time(index = date) %>% # convert to tibble time object
  select(date, new) %>%
  tibbletime::collapse_by("1 week", side = "start", clean = TRUE) %>%
  group_by(date) %>%
  mutate(total = sum(new, na.rm = TRUE)) %>%
  distinct(date, .keep_all = TRUE) %>%
  ungroup() %>%
  # expand matrix to include weeks without data
  complete(
    date = seq(date[1], date[length(date)], by = "1 week"),
    fill = list(total = 0))

filtered

#        date  sex    lang age new
#1 2018-01-25 male spanish  32   1

# convert to xts
totals_ <- xts(totals, order.by = totals$date)

totals_

#           date         new total
#2018-01-21 "2018-01-21" "1" "1"  

# plot
dygraph(totals_[, "total"]) %>%
  dyRangeSelector() %>%
  dyOptions(useDataTimezone = FALSE,
            stepPlot = TRUE,
            drawGrid = FALSE,
            fillGraph = TRUE)

我认为根本问题是dygraph不会绘制由1行组成的xts对象。因此,每当通过闪亮输入(或静态过滤器调用)设置的过滤器将数据集减少到 1 个匹配项(xts 对象中的 1 行)时,绘图将为空。

(如果匹配项为零,R 在我的示例中的 tibbletime 步骤中抛出错误,因为没有行。)