Davide Lorino
Davide Lorino

Reputation: 915

RShiny - Same time last year

I am trying to display two plots side-by-side in a Shiny app.

In the first plot, data is displayed as the user chooses from the daterange selector. In the second plot, I would like it to display the exact same daterange selected by the user, but in the previous year.

The code I am using for filtering the data on daterange input is as follows:

reactive_data <- reactive({
   filter(data, between(date, input$dateRange[1], 
                              input$dateRange[2]))
})

Ideally I would like my result to be another reactive dataframe containing the same dateranges in the year prior - so if the user selects "01/01/2017 - 01/02/2017" then the reactive_data_year_prior would contain all data within the daterange "01/01/2016 - 01/02/2016".

Any help with this is greatly appreciated!

Upvotes: 0

Views: 31

Answers (1)

Davide Lorino
Davide Lorino

Reputation: 915

I've found "something that works" (i'll stop short of calling this a "solution" as my level of experience is low).

reactive_data <- reactive({
   filter(data, between(date, input$dateRange[1], 
                              input$dateRange[2]))
})

last_year <- reactive({
data_frame("dateFrom" = input$dateRange[1] - lubridate::years(1), "dateTo" = 
input$dateRange[2] - lubridate::years(1)
})

data_last_year <- reactive({
filter(data, between(date, last_year()$dateFrom, 
last_year()$dateTo)
})

Upvotes: 0

Related Questions