Spaces:
Build error
Build error
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,58 +1,127 @@
|
|
| 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$
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
shinyApp(ui, server)
|
|
|
|
| 1 |
library(shiny)
|
|
|
|
| 2 |
library(dplyr)
|
| 3 |
+
library(shinythemes)
|
| 4 |
+
library(plotly)
|
| 5 |
+
|
| 6 |
+
# Generate synthetic data
|
| 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 |
+
price_factor <- -1.7 * (data$pricePoint - 70)^2
|
| 24 |
+
foot_traffic_effect <- 2.5 * (data$footTraffic - 200) + 80
|
| 25 |
+
stock_effect <- 0.80 * (data$stockAvailability - 70) + 50
|
| 26 |
+
|
| 27 |
+
data$sales <- round(
|
| 28 |
+
foot_traffic_effect + data$adSpend * 0.2 + data$discountPercent * 60 +
|
| 29 |
+
data$socialMediaEngagement * 0.5 + data$competitorDistance * 150 +
|
| 30 |
+
data$websiteVisits * 0.15 + price_factor + stock_effect + runif(days, -40, 40)
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
data$sales <- pmax(data$sales, 0)
|
| 34 |
+
return(data)
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
store_data <- generate_complex_data()
|
| 38 |
+
|
| 39 |
+
# UI
|
| 40 |
+
ui <- fluidPage(
|
| 41 |
+
theme = shinytheme("cosmo"),
|
| 42 |
+
titlePanel("Fashion Sales Simulator - Plotly Edition"),
|
| 43 |
+
tabsetPanel(
|
| 44 |
+
tabPanel("Guided Analysis",
|
| 45 |
+
sidebarLayout(
|
| 46 |
+
sidebarPanel(
|
| 47 |
+
selectInput("selected_metric", "Select a Variable:",
|
| 48 |
+
choices = names(store_data)[-c(1,2,10)],
|
| 49 |
+
selected = "footTraffic"),
|
| 50 |
+
checkboxInput("show_trend", "Show Linear Trend Line", FALSE)
|
| 51 |
+
),
|
| 52 |
+
mainPanel(
|
| 53 |
+
plotlyOutput("guided_plot"),
|
| 54 |
+
verbatimTextOutput("correlation")
|
| 55 |
+
)
|
| 56 |
+
)
|
| 57 |
),
|
| 58 |
+
|
| 59 |
+
tabPanel("Free Exploration",
|
| 60 |
+
sidebarLayout(
|
| 61 |
+
sidebarPanel(
|
| 62 |
+
selectInput("x_var", "Select X Variable:", choices = names(store_data)[-c(1,2,10)]),
|
| 63 |
+
selectInput("y_var", "Select Y Variable:", choices = names(store_data)[-c(1,2,10)]),
|
| 64 |
+
checkboxInput("show_trend_explore", "Show Linear Trend Line", FALSE),
|
| 65 |
+
actionButton("compute_corr", "Compute Correlation")
|
| 66 |
+
),
|
| 67 |
+
mainPanel(
|
| 68 |
+
plotlyOutput("free_explore_plot"),
|
| 69 |
+
verbatimTextOutput("free_corr_output")
|
| 70 |
+
)
|
| 71 |
+
)
|
| 72 |
+
)
|
| 73 |
+
)
|
| 74 |
)
|
| 75 |
|
| 76 |
+
# Server
|
| 77 |
server <- function(input, output, session) {
|
| 78 |
+
compute_correlation <- function(x, y) {
|
| 79 |
+
cor(store_data[[x]], store_data[[y]], use = "complete.obs")
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
output$guided_plot <- renderPlotly({
|
| 83 |
+
p <- plot_ly(store_data, x = ~get(input$selected_metric), y = ~sales, type = 'scatter', mode = 'markers',
|
| 84 |
+
marker = list(color = 'blue')) %>%
|
| 85 |
+
layout(title = paste("Sales vs", input$selected_metric),
|
| 86 |
+
xaxis = list(title = input$selected_metric),
|
| 87 |
+
yaxis = list(title = "Sales"))
|
| 88 |
+
|
| 89 |
+
if (input$show_trend) {
|
| 90 |
+
model <- lm(sales ~ get(input$selected_metric), data = store_data)
|
| 91 |
+
trend_line <- data.frame(x = store_data[[input$selected_metric]], y = predict(model))
|
| 92 |
+
trend_line <- trend_line[order(trend_line$x), ]
|
| 93 |
+
p <- p %>% add_lines(x = ~trend_line$x, y = ~trend_line$y, name = "Trend Line", line = list(color = 'red'))
|
| 94 |
+
}
|
| 95 |
+
p
|
| 96 |
})
|
| 97 |
|
| 98 |
+
output$correlation <- renderText({
|
| 99 |
+
corr_value <- compute_correlation(input$selected_metric, "sales")
|
| 100 |
+
paste("Correlation with Sales:", round(corr_value, 3))
|
| 101 |
+
})
|
| 102 |
+
|
| 103 |
+
output$free_explore_plot <- renderPlotly({
|
| 104 |
+
p <- plot_ly(store_data, x = ~get(input$x_var), y = ~get(input$y_var), type = 'scatter', mode = 'markers',
|
| 105 |
+
marker = list(color = 'green4')) %>%
|
| 106 |
+
layout(title = paste(input$x_var, "vs", input$y_var),
|
| 107 |
+
xaxis = list(title = input$x_var),
|
| 108 |
+
yaxis = list(title = input$y_var))
|
| 109 |
+
|
| 110 |
+
if (input$show_trend_explore) {
|
| 111 |
+
model <- lm(get(input$y_var) ~ get(input$x_var), data = store_data)
|
| 112 |
+
trend_line <- data.frame(x = store_data[[input$x_var]], y = predict(model))
|
| 113 |
+
trend_line <- trend_line[order(trend_line$x), ]
|
| 114 |
+
p <- p %>% add_lines(x = ~trend_line$x, y = ~trend_line$y, name = "Trend Line", line = list(color = 'red'))
|
| 115 |
+
}
|
| 116 |
+
p
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
observeEvent(input$compute_corr, {
|
| 120 |
+
output$free_corr_output <- renderText({
|
| 121 |
+
corr_value <- compute_correlation(input$x_var, input$y_var)
|
| 122 |
+
paste("Correlation between", input$x_var, "and", input$y_var, ":", round(corr_value, 3))
|
| 123 |
+
})
|
| 124 |
+
})
|
| 125 |
}
|
| 126 |
|
| 127 |
shinyApp(ui, server)
|