Spaces:
Build error
Build error
| 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) | |