日期范围不会更新数据

时间:2018-12-03 15:36:46

标签: r shiny flexdashboard

当我更改日期范围输入(从02/01到01/01的“到”日期)时,数据不会更改。

---
title: "Untitled"
author: "George"
date: "12/3/2018"
output:
    flexdashboard::flex_dashboard
runtime: shiny
---

```{r global, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(flexdashboard)
library(dplyr)
library(lubridate)
library(ggplot2)


dates <- c("01/01/18 1:00:00 PM" ,"01/01/18 1:01:00 PM",
           "01/01/18 1:02:00 PM" ,"01/01/18 1:03:00 PM",
           "01/01/18 1:04:00 PM" ,"01/01/18 1:05:00 PM",
           "02/01/18 1:06:00 PM" ,"02/01/18 1:07:00 PM",
           "02/01/18 1:08:00 PM" ,"02/01/18 1:09:00 PM",
           "02/01/18 1:10:00 PM" ,"02/01/18 1:11:00 PM")

x <- c(1,2,3,4,5,6,7,8,9,10,11,12)
y <- c(11,22,33,44,55,66,77,88,99,100,111,112)

datfr <- data.frame(dates, x, y)

datfr$dates <- dmy_hms(datfr$dates)
```

Intro
========================================================================

Inputs {.sidebar}
--------------------------------------------------------------------
```{r}
dateRangeInput('dateSelect', 'Date Range', start ="2018-01-01",
               end = "2018-01-02", min = "2018-01-01",
               max = "2018-01-02", format = "yyyy-mm-dd")
```

Outputs
-----------------------------------------------------------------------

### Chart


```{r}
dateRange <- reactive({
  datfr %>%
  filter(dates >= input$dateSelect[1] & dates <= input$dateSelect[2])
})

renderPlot({
  ggplot(dateRange(), aes(datfr$x, datfr$y)) +
    geom_point()
})

```

2 个答案:

答案 0 :(得分:1)

仅使用aes(x, y)而不是aes(datfr$x, datfr$y),否则您将不使用反应性dateRange对象的值,而是始终使用原始datfr对象的值。

答案 1 :(得分:0)

尝试:

dateRange <- reactive({
  datfr %>%
    mutate(dates = as.Date(as.character(dates))) %>%
    filter(dates >= input$dateSelect[1] & dates <= input$dateSelect[2])
})

还有aes(x, y),如此处其他答复所建议。