michelerussoAA commited on
Commit
1dda657
·
verified ·
1 Parent(s): 412c9c8

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +51 -87
app.R CHANGED
@@ -1,9 +1,9 @@
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
@@ -19,30 +19,18 @@ generate_complex_data <- function() {
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
 
@@ -51,33 +39,33 @@ store_data <- generate_complex_data()
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
  )
@@ -87,70 +75,47 @@ ui <- fluidPage(
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)
@@ -159,5 +124,4 @@ server <- function(input, output, session) {
159
  })
160
  }
161
 
162
- # Run App
163
  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
 
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
 
 
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
  )
 
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)
 
124
  })
125
  }
126
 
 
127
  shinyApp(ui, server)