Spaces:
Build error
Build error
File size: 4,712 Bytes
a018e61 2dc2eaa 1dda657 a018e61 1dda657 2dc2eaa 1dda657 2dc2eaa 1dda657 2dc2eaa 1dda657 2dc2eaa a018e61 2dc2eaa 1dda657 2dc2eaa 1dda657 2dc2eaa 1dda657 2dc2eaa a018e61 1dda657 2dc2eaa 1dda657 2dc2eaa 1dda657 2dc2eaa a018e61 2dc2eaa a018e61 2dc2eaa 1dda657 2dc2eaa 1dda657 2dc2eaa 1dda657 2dc2eaa 1dda657 2dc2eaa a018e61 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | library(shiny)
library(dplyr)
library(shinythemes)
library(plotly)
# Generate synthetic data
generate_complex_data <- function() {
set.seed(123)
days <- 30
data <- data.frame(
day = 1:days,
date = seq.Date(from = as.Date("2024-01-01"), by = "day", length.out = days),
footTraffic = sample(200:600, days, replace = TRUE),
adSpend = sample(400:1200, days, replace = TRUE),
discountPercent = sample(0:40, days, replace = TRUE),
socialMediaEngagement = sample(200:1200, days, replace = TRUE),
competitorDistance = sample(1:15, days, replace = TRUE),
websiteVisits = sample(400:1000, days, replace = TRUE),
pricePoint = sample(40:100, days, replace = TRUE),
stockAvailability = sample(40:100, days, replace = TRUE)
)
price_factor <- -1.7 * (data$pricePoint - 70)^2
foot_traffic_effect <- 2.5 * (data$footTraffic - 200) + 80
stock_effect <- 0.80 * (data$stockAvailability - 70) + 50
data$sales <- round(
foot_traffic_effect + data$adSpend * 0.2 + data$discountPercent * 60 +
data$socialMediaEngagement * 0.5 + data$competitorDistance * 150 +
data$websiteVisits * 0.15 + price_factor + stock_effect + runif(days, -40, 40)
)
data$sales <- pmax(data$sales, 0)
return(data)
}
store_data <- generate_complex_data()
# UI
ui <- fluidPage(
theme = shinytheme("cosmo"),
titlePanel("Fashion Sales Simulator - Plotly Edition"),
tabsetPanel(
tabPanel("Guided Analysis",
sidebarLayout(
sidebarPanel(
selectInput("selected_metric", "Select a Variable:",
choices = names(store_data)[-c(1,2,10)],
selected = "footTraffic"),
checkboxInput("show_trend", "Show Linear Trend Line", FALSE)
),
mainPanel(
plotlyOutput("guided_plot"),
verbatimTextOutput("correlation")
)
)
),
tabPanel("Free Exploration",
sidebarLayout(
sidebarPanel(
selectInput("x_var", "Select X Variable:", choices = names(store_data)[-c(1,2,10)]),
selectInput("y_var", "Select Y Variable:", choices = names(store_data)[-c(1,2,10)]),
checkboxInput("show_trend_explore", "Show Linear Trend Line", FALSE),
actionButton("compute_corr", "Compute Correlation")
),
mainPanel(
plotlyOutput("free_explore_plot"),
verbatimTextOutput("free_corr_output")
)
)
)
)
)
# Server
server <- function(input, output, session) {
compute_correlation <- function(x, y) {
cor(store_data[[x]], store_data[[y]], use = "complete.obs")
}
output$guided_plot <- renderPlotly({
p <- plot_ly(store_data, x = ~get(input$selected_metric), y = ~sales, type = 'scatter', mode = 'markers',
marker = list(color = 'blue')) %>%
layout(title = paste("Sales vs", input$selected_metric),
xaxis = list(title = input$selected_metric),
yaxis = list(title = "Sales"))
if (input$show_trend) {
model <- lm(sales ~ get(input$selected_metric), data = store_data)
trend_line <- data.frame(x = store_data[[input$selected_metric]], y = predict(model))
trend_line <- trend_line[order(trend_line$x), ]
p <- p %>% add_lines(x = ~trend_line$x, y = ~trend_line$y, name = "Trend Line", line = list(color = 'red'))
}
p
})
output$correlation <- renderText({
corr_value <- compute_correlation(input$selected_metric, "sales")
paste("Correlation with Sales:", round(corr_value, 3))
})
output$free_explore_plot <- renderPlotly({
p <- plot_ly(store_data, x = ~get(input$x_var), y = ~get(input$y_var), type = 'scatter', mode = 'markers',
marker = list(color = 'green4')) %>%
layout(title = paste(input$x_var, "vs", input$y_var),
xaxis = list(title = input$x_var),
yaxis = list(title = input$y_var))
if (input$show_trend_explore) {
model <- lm(get(input$y_var) ~ get(input$x_var), data = store_data)
trend_line <- data.frame(x = store_data[[input$x_var]], y = predict(model))
trend_line <- trend_line[order(trend_line$x), ]
p <- p %>% add_lines(x = ~trend_line$x, y = ~trend_line$y, name = "Trend Line", line = list(color = 'red'))
}
p
})
observeEvent(input$compute_corr, {
output$free_corr_output <- renderText({
corr_value <- compute_correlation(input$x_var, input$y_var)
paste("Correlation between", input$x_var, "and", input$y_var, ":", round(corr_value, 3))
})
})
}
shinyApp(ui, server)
|