Spaces:
Build error
Build error
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,58 +1,163 @@
|
|
| 1 |
library(shiny)
|
| 2 |
-
library(bslib)
|
| 3 |
library(dplyr)
|
| 4 |
-
library(
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
),
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
|
|
|
| 27 |
server <- function(input, output, session) {
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
})
|
| 32 |
-
|
| 33 |
-
output$scatter <- renderPlot(
|
| 34 |
-
{
|
| 35 |
-
p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) +
|
| 36 |
-
theme_light() +
|
| 37 |
-
list(
|
| 38 |
-
theme(legend.position = "bottom"),
|
| 39 |
-
if (input$by_species) aes(color = Species),
|
| 40 |
-
geom_point(),
|
| 41 |
-
if (input$smooth) geom_smooth()
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
if (input$show_margins) {
|
| 45 |
-
margin_type <- if (input$by_species) "density" else "histogram"
|
| 46 |
-
p <- p |> ggExtra::ggMarginal(
|
| 47 |
-
type = margin_type, margins = "both",
|
| 48 |
-
size = 8, groupColour = input$by_species, groupFill = input$by_species
|
| 49 |
-
)
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
p
|
| 53 |
-
},
|
| 54 |
-
res = 100
|
| 55 |
-
)
|
| 56 |
}
|
| 57 |
|
|
|
|
| 58 |
shinyApp(ui, server)
|
|
|
|
| 1 |
library(shiny)
|
|
|
|
| 2 |
library(dplyr)
|
| 3 |
+
library(shinythemes)
|
| 4 |
+
library(highcharter)
|
| 5 |
|
| 6 |
+
# Generate synthetic data with U-shaped & inverted U-shaped effects
|
| 7 |
+
generate_complex_data <- function() {
|
| 8 |
+
set.seed(123)
|
| 9 |
+
days <- 30
|
| 10 |
+
data <- data.frame(
|
| 11 |
+
day = 1:days,
|
| 12 |
+
date = seq.Date(from = as.Date("2024-01-01"), by = "day", length.out = days),
|
| 13 |
+
footTraffic = sample(200:600, days, replace = TRUE),
|
| 14 |
+
adSpend = sample(400:1200, days, replace = TRUE),
|
| 15 |
+
discountPercent = sample(0:40, days, replace = TRUE),
|
| 16 |
+
socialMediaEngagement = sample(200:1200, days, replace = TRUE),
|
| 17 |
+
competitorDistance = sample(1:15, days, replace = TRUE),
|
| 18 |
+
websiteVisits = sample(400:1000, days, replace = TRUE),
|
| 19 |
+
pricePoint = sample(40:100, days, replace = TRUE),
|
| 20 |
+
stockAvailability = sample(40:100, days, replace = TRUE)
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# U-shaped effect on pricePoint
|
| 24 |
+
price_factor <- -0.7 * (data$pricePoint - 70)^2
|
| 25 |
+
|
| 26 |
+
# Inverted U-shaped effect on foot traffic
|
| 27 |
+
foot_traffic_effect <- 2.5 * (data$footTraffic - 400) + 80
|
| 28 |
+
|
| 29 |
+
# Realistic effect of stock availability (moderate stock works better)
|
| 30 |
+
stock_effect <- 0.60 * (data$stockAvailability - 70) + 50
|
| 31 |
+
|
| 32 |
+
# Final sales equation
|
| 33 |
+
data$sales <- round(
|
| 34 |
+
foot_traffic_effect + # Inverted U-shaped
|
| 35 |
+
data$adSpend * 0.2 +
|
| 36 |
+
data$discountPercent * 10 +
|
| 37 |
+
data$socialMediaEngagement * 0.1 +
|
| 38 |
+
(data$competitorDistance) * 45 +
|
| 39 |
+
data$websiteVisits * 0.15 +
|
| 40 |
+
price_factor + # U-shaped
|
| 41 |
+
stock_effect + # Realistic stock effect
|
| 42 |
+
runif(days, -100, 100) # Random noise
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
data$sales <- pmax(data$sales, 0) # Ensure non-negative sales
|
| 46 |
+
return(data)
|
| 47 |
+
}
|
| 48 |
|
| 49 |
+
store_data <- generate_complex_data()
|
| 50 |
+
|
| 51 |
+
# UI
|
| 52 |
+
ui <- fluidPage(
|
| 53 |
+
theme = shinytheme("cosmo"),
|
| 54 |
+
|
| 55 |
+
titlePanel("Fashion Sales Simulator - Highcharter Edition"),
|
| 56 |
+
|
| 57 |
+
tabsetPanel(
|
| 58 |
+
tabPanel("Guided Analysis",
|
| 59 |
+
sidebarLayout(
|
| 60 |
+
sidebarPanel(
|
| 61 |
+
selectInput("selected_metric", "Select a Variable:",
|
| 62 |
+
choices = names(store_data)[-c(1,2,10)],
|
| 63 |
+
selected = "footTraffic")
|
| 64 |
+
),
|
| 65 |
+
mainPanel(
|
| 66 |
+
highchartOutput("guided_plot"),
|
| 67 |
+
verbatimTextOutput("correlation")
|
| 68 |
+
)
|
| 69 |
+
)
|
| 70 |
),
|
| 71 |
+
|
| 72 |
+
tabPanel("Free Exploration",
|
| 73 |
+
sidebarLayout(
|
| 74 |
+
sidebarPanel(
|
| 75 |
+
selectInput("x_var", "Select X Variable:", choices = names(store_data)[-c(1,2,10)]),
|
| 76 |
+
selectInput("y_var", "Select Y Variable:", choices = names(store_data)[-c(1,2,10)]),
|
| 77 |
+
actionButton("compute_corr", "Compute Correlation")
|
| 78 |
+
),
|
| 79 |
+
mainPanel(
|
| 80 |
+
highchartOutput("free_explore_plot"),
|
| 81 |
+
verbatimTextOutput("free_corr_output")
|
| 82 |
+
)
|
| 83 |
+
)
|
| 84 |
+
)
|
| 85 |
+
)
|
| 86 |
)
|
| 87 |
|
| 88 |
+
# Server
|
| 89 |
server <- function(input, output, session) {
|
| 90 |
+
|
| 91 |
+
# Compute correlation dynamically
|
| 92 |
+
compute_correlation <- function(x, y) {
|
| 93 |
+
cor(store_data[[x]], store_data[[y]], use = "complete.obs")
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
# Guided Analysis Highchart Plot
|
| 97 |
+
output$guided_plot <- renderHighchart({
|
| 98 |
+
data_points <- lapply(1:nrow(store_data), function(i) {
|
| 99 |
+
list(store_data[[input$selected_metric]][i], store_data$sales[i])
|
| 100 |
+
})
|
| 101 |
+
|
| 102 |
+
highchart() %>%
|
| 103 |
+
hc_chart(type = "scatter") %>%
|
| 104 |
+
hc_title(text = paste("Sales vs", input$selected_metric)) %>%
|
| 105 |
+
hc_xAxis(title = list(text = input$selected_metric)) %>%
|
| 106 |
+
hc_yAxis(title = list(text = "Sales")) %>%
|
| 107 |
+
hc_add_series(
|
| 108 |
+
data = data_points,
|
| 109 |
+
type = "scatter",
|
| 110 |
+
name = "Data Points",
|
| 111 |
+
marker = list(radius = 4),
|
| 112 |
+
color = "blue"
|
| 113 |
+
) %>%
|
| 114 |
+
hc_tooltip(pointFormat = paste0(
|
| 115 |
+
"<b>", input$selected_metric, ":</b> {point.x}<br>",
|
| 116 |
+
"<b>Sales:</b> {point.y}"
|
| 117 |
+
)) %>%
|
| 118 |
+
hc_exporting(enabled = TRUE)
|
| 119 |
+
})
|
| 120 |
+
|
| 121 |
+
output$correlation <- renderText({
|
| 122 |
+
corr_value <- compute_correlation(input$selected_metric, "sales")
|
| 123 |
+
paste("Correlation with Sales:", round(corr_value, 3))
|
| 124 |
+
})
|
| 125 |
+
|
| 126 |
+
# Free Exploration Scatter Plot with Highcharter
|
| 127 |
+
output$free_explore_plot <- renderHighchart({
|
| 128 |
+
req(input$x_var, input$y_var)
|
| 129 |
+
|
| 130 |
+
data_points <- lapply(1:nrow(store_data), function(i) {
|
| 131 |
+
list(store_data[[input$x_var]][i], store_data[[input$y_var]][i])
|
| 132 |
+
})
|
| 133 |
+
|
| 134 |
+
highchart() %>%
|
| 135 |
+
hc_chart(type = "scatter") %>%
|
| 136 |
+
hc_title(text = paste(input$x_var, "vs", input$y_var)) %>%
|
| 137 |
+
hc_xAxis(title = list(text = input$x_var)) %>%
|
| 138 |
+
hc_yAxis(title = list(text = input$y_var)) %>%
|
| 139 |
+
hc_add_series(
|
| 140 |
+
data = data_points,
|
| 141 |
+
type = "scatter",
|
| 142 |
+
name = "Data Points",
|
| 143 |
+
marker = list(radius = 4),
|
| 144 |
+
color = "red"
|
| 145 |
+
) %>%
|
| 146 |
+
hc_tooltip(pointFormat = paste0(
|
| 147 |
+
"<b>", input$x_var, ":</b> {point.x}<br>",
|
| 148 |
+
"<b>", input$y_var, ":</b> {point.y}"
|
| 149 |
+
)) %>%
|
| 150 |
+
hc_exporting(enabled = TRUE)
|
| 151 |
+
})
|
| 152 |
+
|
| 153 |
+
# Compute correlation in Free Exploration
|
| 154 |
+
observeEvent(input$compute_corr, {
|
| 155 |
+
output$free_corr_output <- renderText({
|
| 156 |
+
corr_value <- compute_correlation(input$x_var, input$y_var)
|
| 157 |
+
paste("Correlation between", input$x_var, "and", input$y_var, ":", round(corr_value, 3))
|
| 158 |
+
})
|
| 159 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
}
|
| 161 |
|
| 162 |
+
# Run App
|
| 163 |
shinyApp(ui, server)
|