michelerussoAA commited on
Commit
ea1e82b
·
verified ·
1 Parent(s): fb1b150

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +116 -47
app.R CHANGED
@@ -1,58 +1,127 @@
1
  library(shiny)
2
- library(bslib)
3
  library(dplyr)
4
- library(ggplot2)
5
-
6
- df <- readr::read_csv("penguins.csv")
7
- # Find subset of columns that are suitable for scatter plot
8
- df_num <- df |> select(where(is.numeric), -Year)
9
-
10
- ui <- page_sidebar(
11
- theme = bs_theme(bootswatch = "minty"),
12
- title = "Penguins explorer",
13
- sidebar = sidebar(
14
- varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
15
- varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
16
- checkboxGroupInput("species", "Filter by species",
17
- choices = unique(df$Species), selected = unique(df$Species)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  ),
19
- hr(), # Add a horizontal rule
20
- checkboxInput("by_species", "Show species", TRUE),
21
- checkboxInput("show_margins", "Show marginal plots", TRUE),
22
- checkboxInput("smooth", "Add smoother"),
23
- ),
24
- plotOutput("scatter")
 
 
 
 
 
 
 
 
 
 
25
  )
26
 
 
27
  server <- function(input, output, session) {
28
- subsetted <- reactive({
29
- req(input$species)
30
- df |> filter(Species %in% input$species)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(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)