Skip to content

Commit

Permalink
vorerster commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gabst-tuhh committed Jul 27, 2024
1 parent 0012b37 commit 525a06e
Show file tree
Hide file tree
Showing 22 changed files with 8,803 additions and 0 deletions.
Binary file added content/01_journal/01_data/bikes.xlsx
Binary file not shown.
Binary file added content/01_journal/01_data/bikes_tbl.rds
Binary file not shown.
Binary file added content/01_journal/01_data/bikeshops.xlsx
Binary file not shown.
Binary file added content/01_journal/01_data/bikeshops_tbl.rds
Binary file not shown.
Binary file added content/01_journal/01_data/gadm36_DEU_1_sp.rds
Binary file not shown.
Binary file added content/01_journal/01_data/orderlines.xlsx
Binary file not shown.
Binary file added content/01_journal/01_data/orderlines_tbl.rds
Binary file not shown.
189 changes: 189 additions & 0 deletions content/01_journal/flex_dash.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
---
title: "Sales Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(flexdashboard)
# Core
library(tidyverse)
# Interactive Visualizations
library(plotly)
# Spatial Data
library(raster)
library(sf)
library(dplyr)
library(lubridate)
library(shinyWidgets)
```

```{r echo=FALSE}
format_to_euro <- function(x, suffix = " €") {
scales::dollar(x,
suffix = suffix,
prefix = "",
big.mark = ".",
decimal.mark = ",")
}
euro_format <- function(scale = 1,
prefix = "",
suffix = " €",
big.mark = ".",
decimal.mark = ",") {
scales::dollar_format(suffix = suffix,
prefix = prefix,
big.mark = big.mark,
decimal.mark = decimal.mark,
scale = scale)
}
```


```{r}
# Bike data
bikes_tbl <- readRDS("01_data/bikes_tbl.rds")
bikeshops_tbl <- readRDS("01_data/bikeshops_tbl.rds")
orderlines_tbl <- readRDS("01_data/orderlines_tbl.rds")
bike_orderlines_tbl <- orderlines_tbl %>%
left_join(bikes_tbl, by = c("product_id" = "bike_id")) %>%
left_join(bikeshops_tbl, by = c("customer_id" = "bikeshop_id")) %>%
mutate(total_price = price_euro * quantity)
# German spatial data
# German spatial data
# germany_sp <- getData('GADM', country='DE', level=1) broken link
germany_sp <- readRDS("01_data/gadm36_DEU_1_sp.rds")
germany_sf <- st_as_sf(germany_sp) %>%
# Add english names
mutate(VARNAME_1 = ifelse(is.na(VARNAME_1), NAME_1, VARNAME_1))
```


Column {.sidebar}
-----------------------------------------------------------------------

```{r}
DateSeq = seq(as.Date("2015-01-07"), Sys.Date(), "day")
dateRangeInput("dateRange", "Date Range", format = "yyyy-mm-dd", start=min(DateSeq), end=max(DateSeq))
```


```{r}
checkboxGroupButtons("bike_type", "Bike Type",
choices = c("All", "Road", "Mountain"),
selected = "All",
status = "primary")
```

```{r}
pickerInput("bike_family", "Bike Family",
choices = c("All", unique(bike_orderlines_tbl$category_2)),
selected = "All",
multiple = TRUE)
```


```{r}
actionButton("apply_filters", "Apply Filters", icon = icon("filter"))
```


Column {data-width=1000}
---------------------------------------------------------------

### By State

```{r}
# Reactive expression to filter data when the Apply button is clicked
filtered_data <- eventReactive(input$apply_filters, {
req(input$dateRange) # Ensure dateRange input is available
req(input$bike_type) # Ensure bike_type input is available
req(input$bike_family) # Ensure bike_family input is available
bike_orderlines_tbl %>%
filter(order_date >= input$dateRange[1] & order_date <= input$dateRange[2]) %>%
filter(if ("All" %in% input$bike_type || length(input$bike_type) == 0) TRUE else category_1 %in% input$bike_type) %>%
filter(if ("All" %in% input$bike_family || length(input$bike_family) == 0) TRUE else category_2 %in% input$bike_family) %>%
group_by(state) %>%
summarise(total_revenue = sum(total_price)) %>%
ungroup() %>%
right_join(germany_sf, by = c("state" = "VARNAME_1")) %>%
mutate(total_revenue = ifelse(is.na(total_revenue), 0, total_revenue)) %>%
mutate(label_text = str_glue("State: {state}
Revenue: {format_to_euro(total_revenue)}")) %>%
st_as_sf()
})
# Render the plot based on filtered data
output$geo_plot_tbl <- renderPlot({
data <- filtered_data()
plot_ly(
data = data,
type = 'choroplethmapbox',
geojson = geojsonio::geojson_list(geo_plot_tbl),
locations = ~state,
featureidkey = "properties.VARNAME_1",
color = ~total_revenue,
colors = "Blues",
hoverinfo = 'text',
text = ~label_text,
colorbar = list(title = "Total Revenue")
) %>%
layout(
mapbox = list(
center = list(lon = 10, lat = 51),
zoom = 4
)
)
})
```


```{r}
#geo_plot_tbl <- bike_orderlines_tbl %>%
# group_by(state) %>%
# summarise(total_revenue = sum(total_price)) %>%
# ungroup() %>%
# right_join(germany_sf, by = c("state" = "VARNAME_1")) %>%
# mutate(total_revenue = ifelse(is.na(total_revenue), 0, total_revenue)) %>%
# mutate(label_text = str_glue("State: {state}
# Revenue: {format_to_euro(total_revenue)}")) %>%
# st_as_sf()
```


```{r}
```

```{r}
plotOutput("geo_plot_tbl")
```


Column {data-width=1000}
---------------------------------------------------------------

### Over Time
3,944 changes: 3,944 additions & 0 deletions content/01_journal/flex_dash.html

Large diffs are not rendered by default.

192 changes: 192 additions & 0 deletions content/01_journal/flex_dash_2.rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(flexdashboard)
# Core
library(tidyverse)
# Interactive Visualizations
library(plotly)
# Spatial Data
library(raster)
library(sf)
library(dplyr)
library(lubridate)
library(shinyWidgets)
library(shiny)
library(dplyr)
library(ggplot2)
library(plotly)
library(sf)
library(shinythemes)
```

```{r}
# Bike data
bikes_tbl <- readRDS("01_data/bikes_tbl.rds")
bikeshops_tbl <- readRDS("01_data/bikeshops_tbl.rds")
orderlines_tbl <- readRDS("01_data/orderlines_tbl.rds")
bike_orderlines_tbl <- orderlines_tbl %>%
left_join(bikes_tbl, by = c("product_id" = "bike_id")) %>%
left_join(bikeshops_tbl, by = c("customer_id" = "bikeshop_id")) %>%
mutate(total_price = price_euro * quantity)
# German spatial data
# German spatial data
# germany_sp <- getData('GADM', country='DE', level=1) broken link
germany_sp <- readRDS("01_data/gadm36_DEU_1_sp.rds")
germany_sf <- st_as_sf(germany_sp) %>%
# Add english names
mutate(VARNAME_1 = ifelse(is.na(VARNAME_1), NAME_1, VARNAME_1))
```

```{r echo=False}
plot_all_sales <- function(data_tbl) {
plot_data_for_unit <- function(unit) {
data_tbl %>%
mutate(rounded_date = floor_date(order_date, unit = unit)) %>%
group_by(rounded_date) %>%
summarise(total_sales = sum(total_price), .groups = 'drop') %>%
ungroup() %>%
mutate(label_text = str_glue("Sales: {format(total_sales, big.mark = ',')} \nDate: {rounded_date %>% format('%B %Y')}"))
}
p <- plot_ly() %>%
add_lines(
data = plot_data_for_unit("day"),
x = ~rounded_date,
y = ~total_sales,
text = ~label_text,
hoverinfo = 'text',
line = list(shape = 'linear'),
) %>%
layout(
title = "Total Sales",
xaxis = list(title = "", type = "date"),
yaxis = list(title = "", tickformat = ".0f"),
updatemenus = list(
list(
type = "buttons",
x = 0.5, # Center horizontally
y = 1, # Above the plot (top)
buttons = list(
list(label = "Quarter", method = "restyle", args = list("x", list(plot_data_for_unit("quarter")$rounded_date),
"y", list(plot_data_for_unit("quarter")$total_sales),
"text", list(plot_data_for_unit("quarter")$label_text),
"name", "Quarter")),
list(label = "Month", method = "restyle", args = list("x", list(plot_data_for_unit("month")$rounded_date),
"y", list(plot_data_for_unit("month")$total_sales),
"text", list(plot_data_for_unit("month")$label_text),
"name", "Month")),
list(label = "Week", method = "restyle", args = list("x", list(plot_data_for_unit("week")$rounded_date),
"y", list(plot_data_for_unit("week")$total_sales),
"text", list(plot_data_for_unit("week")$label_text),
"name", "Week")),
list(label = "Day", method = "restyle", args = list("x", list(plot_data_for_unit("day")$rounded_date),
"y", list(plot_data_for_unit("day")$total_sales),
"text", list(plot_data_for_unit("day")$label_text),
"name", "Day"), active = TRUE)
),
direction = "right",
showactive = TRUE
)
)
)
# Return the plotly object
return(p)
}
```

<div style="width:1000px;height:1300px">
```{r}
# Define UI
ui <- fluidPage(
theme = shinytheme("cerulean"),
titlePanel("Sales Visualization"),
sidebarLayout(
sidebarPanel(
dateRangeInput("dateRange", "Date Range", format = "yyyy-mm-dd",
start="2015-01-10", end=max(DateSeq)),
pickerInput("bike_type", "Select Bike Type:",
choices = c("All", unique(bike_orderlines_tbl$category_1)),
selected = "All",
multiple = TRUE),
pickerInput("bike_family", "Select Bike Family:",
choices = c("All", unique(bike_orderlines_tbl$category_2)),
selected = "All",
multiple = TRUE),
width=3
)
,
mainPanel(
column(6, plotlyOutput("geo_plot_tbl", height="800px"), style = "height: 100vh;"),
column(6, plotlyOutput("timeseries_plot", height="800px"), style = "height: 100vh;"),
width=9)
),
style = "height: 100vh;",
)
# Define server logic
server <- function(input, output, session) {
# Reactive data filtering
filtered_data <- reactive({
bike_orderlines_tbl %>%
filter(order_date >= input$dateRange[1] & order_date <= input$dateRange[2]) %>%
filter(if ("All" %in% input$bike_type || length(input$bike_type) == 0) TRUE else category_1 %in% input$bike_type) %>%
filter(if ("All" %in% input$bike_family || length(input$bike_family) == 0) TRUE else category_2 %in% input$bike_family)
})
# Render geoplot
output$geo_plot_tbl <- renderPlotly({
geo_plot_tbl <- filtered_data() %>%
group_by(state) %>%
summarise(total_revenue = sum(total_price)) %>%
ungroup() %>%
right_join(germany_sf, by = c("state" = "VARNAME_1")) %>%
mutate(total_revenue = ifelse(is.na(total_revenue), 0, total_revenue)) %>%
mutate(label_text = str_glue("State: {state}
Revenue: {format_to_euro(total_revenue)}")) %>%
st_as_sf()
plot_ly(geo_plot_tbl,
split = ~NAME_1,
color = ~total_revenue,
colors = "Blues",
stroke = I("black"),
hoverinfo = 'text',
text = ~label_text,
hoveron = "fills",
showlegend = FALSE)
})
output$timeseries_plot <- renderPlotly({plot_all_sales(filtered_data())})
}
# Run the application
shinyApp(ui = ui, server = server, options = list(height = 1300))
```
</div>

Binary file added content/01_journal/img/logo_nit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 525a06e

Please sign in to comment.