library(readr)
library(ggplot2)
library(shiny)
library(plotly)
library(DT)
library(rsconnect)
library(dplyr)
library(rsconnect)
library(lubridate)
nissan <- read_csv("nissan_updated.csv")
test_predict_new <- read_csv("time_series_prediction_new.csv")
test_predict_used<- read_csv("time_series_prediction_used.csv")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Hello - A Nissan Dealership"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
dateRangeInput(
inputId = "daterange",
label = "Select A Date Range:",
start = "2019-01-01",
end = today()-1
),
radioButtons("select_type", "Select One Type:",
c("USED" = "USED",
"NEW" = "NEW"
),
selected = "USED"
),
uiOutput("select_make")
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Overal Stock Status",plotlyOutput("dashboard")),
tabPanel("Top Best Selling Makes/Models", plotlyOutput("dashboard_chart1")),
tabPanel("Details of models",DT::dataTableOutput("models"))
),
tabsetPanel(type = "tabs",
tabPanel("Sales Summary", plotlyOutput("sales_plot2")),
tabPanel("Sales Comparison", plotlyOutput("sales_plot1")),
tabPanel("Predicted Sales", plotlyOutput("sales_plot3"))
)
)
)
)
server <- function(input, output) {
dataset1 <- reactive({ nissan %>% filter(SOLD_DATE >= input$daterange[1] & SOLD_DATE <= input$daterange[2] )})
#dataset2 <- reactive({ dplyr::filter(nissan, between(SOLD_DATE,input$daterange[1] - 365,input$daterange[2] - 365 ))})
dataset2 <- reactive({ nissan %>% filter(SOLD_DATE >= input$daterange[1] - 365 & SOLD_DATE <= input$daterange[2] - 365 )})
output$select_make <- renderUI({
choices_make <- reactive ({ nissan %>% filter(SOLD_DATE >= input$daterange[1] & SOLD_DATE <= input$daterange[2] & TYPE ==input$select_type ) %>% pull(MAKE)%>% as.character() })
selectizeInput("select_make",
"Select One Make:",
choices =choices_make(),
selected = choices_make()=="NISSAN",
multiple = FALSE
)
})
output$dashboard <- renderPlotly({
status_summary<- filter(dataset1(), TYPE == input$select_type) %>% group_by(STATUS) %>% summarise(Sum_status =n())
graph_title <- paste("Stock Status from", as.character(input$daterange[1]), "to", as.character(input$daterange[2]))
p <- plot_ly(status_summary, labels = ~STATUS, values = ~Sum_status, type = 'pie') %>%
layout(title = graph_title,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
})
output$dashboard_chart1 <- renderPlotly({
graph_title_2 <- paste("Top 5 Best - Selling Makes", as.character(input$daterange[1]), "To", as.character(input$daterange[2]))
if (input$select_type =="USED") {
make_sum <- filter(dataset1(), TYPE =="USED") %>% group_by(MAKE) %>% summarise(count = n())
make_sum$MAKE <- make_sum$MAKE %>% factor(levels = make_sum$MAKE[order(-make_sum$count)])
make_sum1 <- make_sum %>% arrange(desc(count)) %>% slice(1:5)
pie_used_1 <- ggplot(make_sum1,aes(x = MAKE, y = count, fill = MAKE)) + geom_bar(stat="identity") +
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9", "#CD5C5C", "#FFA07A", "#800000")) +
labs(title = graph_title_2) +theme(legend.position = "none") + ylab("COUNT") + theme(plot.title = element_text(hjust = 0.5))
pie_used_1 + geom_text(aes(label= count), vjust=1.6, color="black", size=4, position = position_dodge(0.9))
} else {
graph_title_3 <- paste("Top 5 Best - Selling Nissan models", as.character(input$daterange[1]), "To", as.character(input$daterange[2]))
make_sum_2 <- filter(dataset1(), TYPE =="NEW") %>% group_by(MODEL) %>% summarise(count = n())
make_sum_2$MODEL <- make_sum_2$MODEL %>% factor(levels = make_sum_2$MODEL[order(-make_sum_2$count)])
make_sum2 <- make_sum_2 %>% arrange(desc(count)) %>% slice(1:5)
pie_new_2 <- ggplot(make_sum2,aes(x = MODEL, y = count, fill = MODEL)) + geom_bar(stat="identity") +
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9", "#CD5C5C", "#FFA07A", "#800000")) +
labs(title = graph_title_3) +theme(legend.position = "none") + ylab("COUNT") + theme(plot.title = element_text(hjust = 0.5)) + geom_text(aes(label= count), vjust=1.6, color="black", size=3.5, position = position_dodge(0.9))
pie_new_2
}
})
output$models <- DT::renderDataTable({
bymakes <- dplyr::filter(dataset1(), TYPE %in% input$select_type & MAKE %in% input$select_make)
bydetail <- bymakes %>% group_by(MAKE, MODEL, VARIANT) %>% summarise(Count = n(),Sold = sum(STATUS=="Sold"), "In stock" = sum(STATUS=="In stock"))
bydetail <- bydetail[, c(2,3,4,5,6)]
},
option= list(lengthMenu = c(10, 50, 100),pageLength = 10, searching=TRUE)
)
output$sales_plot1 <- renderPlotly({
month_sales <- dplyr::filter(dataset1(), STATUS == "Sold")
month_sales1 <- month_sales %>% group_by(YEAR, MONTH) %>% summarise(sum = sum(SOLD_PRICE, na.rm = TRUE)/1000)
month_sales1$MONTH <- factor(month_sales1$MONTH,
levels = c("January","February","March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"))
previous_month_sales <- dplyr::filter(dataset2(), STATUS == "Sold")
previous_month_sales1 <- previous_month_sales %>% group_by(YEAR, MONTH) %>% summarise(sum = sum(SOLD_PRICE, na.rm = TRUE)/1000)
previous_month_sales1$MONTH <- factor(previous_month_sales1$MONTH,
levels = c("January","February","March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"))
colnames(month_sales1) <- c("YEAR", "MONTH", "SUM")
colnames(previous_month_sales1) <- c("YEAR", "MONTH", "SUM")
sales_merge <- bind_rows(month_sales1, previous_month_sales1)
sales_merge$YEAR <- as.character(sales_merge$YEAR)
p <- ggplot(sales_merge) +
geom_bar(mapping = aes(x = MONTH, y = SUM, fill = YEAR), stat = "identity", position = "dodge")
p <- p + ggtitle("Sales in Comparison With Previous Year") + ylab("Sales in AUD thousand")
p + theme(plot.title = element_text(hjust = 0.5)) + theme(axis.text.x=element_text(angle=45,hjust=1))
})
output$sales_plot3 <- renderPlotly({
if (input$select_type =="USED") {
test_predict_used$DATE <- factor(test_predict_used$DATE,
levels = c("January, 2019", "February, 2019", "March, 2019", "April, 2019", "May, 2019",
"June, 2019", "July, 2019", "August, 2019", "September, 2019", "October, 2019",
"November, 2019", "December, 2019", "January, 2020", "February, 2020", "March, 2020",
"April, 2020", "May, 2020", "June, 2020", "July, 2020", "August, 2020", "September, 2020")
)
pt2 <- ggplot2::ggplot(data = test_predict_used, aes(x = DATE, y = Sum_sales, colour = YEAR)) + geom_line(group = 2, size = 1) + geom_point(size = 1.5)+ theme(axis.text.x=element_text(angle=45,hjust=1))
pt2 + geom_vline(xintercept = 9, linetype='dashed', colour = 'red', size =1) + theme(legend.position = "none") + xlab("") + ylab("Sales in AUD thousand") + theme(plot.title = element_text(hjust = 0.5))
}
else
{
test_predict_new$DATE <- factor(test_predict_new$DATE,
levels = c("January, 2019", "February, 2019", "March, 2019", "April, 2019", "May, 2019",
"June, 2019", "July, 2019", "August, 2019", "September, 2019", "October, 2019",
"November, 2019", "December, 2019", "January, 2020", "February, 2020", "March, 2020",
"April, 2020", "May, 2020", "June, 2020", "July, 2020", "August, 2020", "September, 2020")
)
pt1 <- ggplot2::ggplot(data = test_predict_new, aes(x = DATE, y = Sum_sales, colour = YEAR)) + geom_line(group = 2, size = 1) + geom_point(size = 1.5)+ theme(axis.text.x=element_text(angle=45,hjust=1))
pt1 + geom_vline(xintercept = 9, linetype='dashed', colour = 'red', size = 1) + theme(legend.position = "none") + xlab("") + ylab("Sales in AUD thousand") + theme(plot.title = element_text(hjust = 0.5))
}
})
output$sales_plot2 <- renderPlotly({
month_sales <- dplyr::filter(dataset1(), STATUS == "Sold")
month_sales1 <- month_sales %>% group_by(YEAR, MONTH_NO) %>% summarise(sum = sum(SOLD_PRICE, na.rm = TRUE)/1000)
colnames(month_sales1) <- c("YEAR", "MONTH", "SUM")
graph_title_sales <- paste("Total sales from", as.character(input$daterange[1]), "to", as.character(input$daterange[2]))
month_sales1$DATE <- paste(as.character(month_sales1$YEAR), "-", as.character(month_sales1$MONTH))
p2 <- ggplot2::ggplot(data = month_sales1, aes(x = DATE, y = SUM)) + geom_line(size = 1, group = 1) + geom_point(size = 1.5)+ theme(axis.text.x=element_text(angle=45,hjust=1))
p2 <- p2 + labs(title = graph_title_sales , y ="Sales in AUD thousand", x = "MONTH")
p2 + theme(plot.title = element_text(hjust = 0.5)) + theme(axis.text.x=element_text(angle=45,hjust=1))
})
}
shinyApp(ui = ui, server = server)
```