I have hotel reservation dataset which consists of "book_date", "check_in", "check_out" and "revenue". I would like to draw 2 charts - "revenue by booking date" vs "revenue by check-in date"; which I could highlight/zoom in particular booking period (says Jan 2018) to understand revenue for corresponding check-in period (says Feb - Apr 2018).
I try to achieve this by incorporating Shiny "brush" in R Flexdashboard. The idea is that, when nothing is "brush" on "revenue by booking date", "revenue by check-in date" will show a default chart (i.e. revenue for all check-in date). If user zoom in a period in "revenue by booking date", "revenue by check-in date" will react accordingly. However, the default display for "revenue by check-in date" is not working as expected.
Below are my codes:
library(flexdashboard)
library(shiny)
library(tidyverse)
library(lubridate)
library(timetk)
library(scales)
library(plotly)
- revenue by booking date
shiny::plotOutput(outputId = "revenue_book", brush = brushOpts(id = "brush", direction = "x"))
plot time series
output$revenue_book <- shiny::renderPlot(
df %>% filter(book_year == 2018) %>%
group_by(book_date) %>%
summarise(revenue = sum(price)) %>%
ungroup() %>%
ggplot(aes(book_date, revenue)) + geom_line() + geom_point(color = "red") + theme_bw()
)
- revenue by check-in date
shiny::renderPlot({
if (is.null(input$brush$xmin)) {
p <- df %>% filter(book_date == 2018) %>%
group_by(check_in) %>% summarise(revenue = sum(price)) %>% ungroup()
p %>% ggplot(aes(check_in, revenue)) + geom_line() + geom_point(color = "blue") + theme_bw()
}
else {
q <- df %>% filter(between(book_date, input$brush$xmin, input$brush$xmax)) %>%
group_by(check_in) %>% summarise(revenue = sum(price)) %>% ungroup()q %>% ggplot(aes(check_in, revenue)) + geom_line() + geom_point(color = "blue") + theme_bw()
}
})
Default chart is not displayed...
Please advise me what could be the issue.